You had almost everything!
if(authService.isValid(userName,password)) { List<GrantedAuthority> grantedAuthorityList = new ArrayList<GrantedAuthority>(); grantedAuthorityList.add(new SimpleGrantedAuthority("ROLE_USER")); MyObject myObj = new MyObject(userName, password, otherInfo); return new UsernamePasswordAuthenticationToken(mjObj,"", grantedAuthorityList); }
The first argument to UsernamePasswordAuthenticationToken is the principle. A principle is an object in a system that represents a person (or thing) who has just entered the system.
Prior to authentication, the principle is simply a name (String), because all the information that you have at that moment. After logging in, you can collect other information to go with the user.
Spring offers interfaces: User , UserDetails and UserDetailsService to help manage users and do things with users with Springy, so if you make MyObject implement UserDetails , you can get some additional benefits from the Spring environment, but not necessarily, you can only use MyObject
In your controllers (in Spring 4), you can use @AuthenticationPrincipal to enter a custom object in calls, for example:
@RequestMapping(method = RequestMethod.GET, value = "/foo/{bar}") public SomeObject myCommand(@AuthenticationPrincipal MyObject user, @PathVariable String bar);
source share