If I understand your problem correctly, I can suggest the following way.
First of all, you need to implement a class that will contain information about the user. This class should be inherited from org.springframework.security.core.userdetails.User :
public class CustomUserDetails extends User { public CustomUserDetails(String username, String password, Collection<? extends GrantedAuthority> authorities) { super(username, password, authorities); }
In the next step, you created your own implementation of the org.springframework.security.core.userdetails.UserDetailsService interface:
@Service public class CustomUserDetailService implements UserDetailsService{ @Override public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException{ if(StringUtils.isEmpty(userName)) throw new UsernameNotFoundException("User name is empty");
As you can see, this class has only one method in which you can load and configure user user data. Note that I marked this class with the @Service annotation. But you can register it in your Java-config or XML context.
Now, in order to access your user data after successful authentication, you can use the following approach when Spring automatically passes the principal in the controller method:
@Controller public class MyController{ @RequestMapping("/mapping") public String myMethod(Principal principal, ModelMap model){ CustomUserDetails userDetails = (CustomUserDetails)principal; model.addAttribute("firstName", userDetails.getFirstName()); model.addAttribute("lastName", userDetails.getLastName()); } }
Or another way:
@Controller public class MyController{ @RequestMapping("/mapping") public String myMethod(ModelMap model){ Authentication auth = SecurityContextHolder.getContext().getAuthentication(); CustomUserDetails userDetails = (CustomUserDetails)auth.getPrincipal(); model.addAttribute("firstName", userDetails.getFirstName()); model.addAttribute("lastName", userDetails.getLastName()); } }
This method can be used in other places where Spring does not automatically convey basic information.
To go to a specific address after successful authentication, you can use SimpleUrlAuthenticationSuccessHandler . Just create it in your configuration:
@Bean public SavedRequestAwareAuthenticationSuccessHandler successHandler() { SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("/succeslogin"); return successHandler; }
and use it in your configuration:
http.formLogin() .loginProcessingUrl("/login") .permitAll() .usernameParameter("email") .passwordParameter("pass") .successHandler(successHandler())
after that you can create a controller that will send a response from a special URL:
@Controller @RequestMapping("/sucesslogin") public class SuccessLoginController{ @RequestMapping(method = RequestMethod.POST) public String index(ModelMap model, Principal principal){
Because of this, you can return not only the view, but also the JSON response (using the @ResponseBody annotation) or something else, it is up to you. Hope this will be helpful.