Skip to content

Get the authenticated user profiles

LELEU Jérôme edited this page Oct 22, 2018 · 8 revisions

1) Manually retrieve the user profile(s) thanks to the J2EContext and ProfileManager

You can get the profile of the authenticated user using profileManager.get(true) (false not to use the session, but only the current HTTP request). You can test if the user is authenticated using profileManager.isAuthenticated(). You can get all the profiles of the authenticated user (if ever multiple ones are kept) using profileManager.getAll(true).

Example:

WebContext context = new J2EContext(request, response);
ProfileManager manager = new ProfileManager(context);
Optional<CommonProfile> profile = manager.get(true);

The retrieved profile is at least a CommonProfile, from which you can retrieve the most common attributes that all profiles share. But you can also cast the user profile to the appropriate profile according to the provider used for authentication. For example, after a Facebook authentication:

FacebookProfile facebookProfile = (FacebookProfile) commonProfile;

2) Automatically retrieve the user profile(s) thanks to the ProfileManager

First, you must register the components:

@ComponentScan(basePackages = "org.pac4j.springframework.component")

or

@Import(ComponentConfig.class)

Then, you can inject the ProfileManager (and for other needs the J2EContext):

@Autowired
private ProfileManager profileManager;

and retrieve the user profile(s) using this component:

  • Optional<CommonProfile> getProfile()
  • List<CommonProfile> getProfiles().
Clone this wiki locally