After user is registered successfully, i am trying to redirect user to after login page and trying to get the Active user from SecurityContextHolder.getContext().getAuthentication(); which is returning null.
Here is how i am setting context when user is successfully registered
UsernamePasswordAuthenticationToken authenticatedUser = new UsernamePasswordAuthenticationToken(userDetails.getUsername(), userDetails.getPassword(), userDetails.getAuthorities());
authenticatedUser.setDetails(userDetails);
if(authenticatedUser.isAuthenticated()) {
SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
}
Then i am redirecting user to x page using response.sendRedirect(url);
In the controller method i am trying to the current user as
@RequestMapping("/<pattern>")
public @ResponseBody <method_name>(HttpServletRequest req,HttpServletResponse resp){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if(auth==null){
System.out.println("this is null");
}
else{
// do something
}
}
I am seeing the text in console as "this is null".
How to get retain the currentuser/Principal from SecurityContext() through out the user period till user logs out.
I got the solution.
The problem was with the pattern attribute for the tag for that specific URI group.
Earlier it was /path/* and it was failing. I changed it to /path/** and it worked.
It would be helpful if someone can explain between these two pattern matching(/* and /**).