-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SEC-3139: Anonymous authentication token not passed to Controller #3338
Comments
Rob Winch said: The reason this happens is because Spring MVC resolves anything of type Principal to the HttpServletRequest.getUserPrincipal() result. Spring Security makes this null if the user is not authenticated to conform with the Servlet specification. A workaround is to use something like this: @RequestMapping("/index")
public String index(
@Value("#{T(org.springframework.security.core.context.SecurityContextHolder).context.authentication}") Authentication auth,
ModelMap model,
HttpServletRequest req,
HttpServletResponse resp)
{
SecurityContext ctx = SecurityContextHolder.getContext();
Authentication auth2 = ctx.getAuthentication(); // <-- always non-null (correct)
...
} This is not ideal, but if I recall correctly, Spring Security cannot register a cc [~rstoya05-aop] |
James Garrison said: I understand. This probably deserves a paragraph in the documentation. |
Note that as of Security 5.2, you can use |
James Garrison (Migrated from SEC-3139) said:
Consider the following RequestMapping in a SpringMVC controller:
I got this (use of an
Authentication
controller parameter) from here.If the user is "really" authenticated (i.e. logged in) then this works as advertised. The problem is that when anonymous authentication is allowed and no user is logged in the value of
auth
is null when it should be anAnonymousAuthenticationToken
.The alternate method of going through
ctx.getAuthentication()
returns the correctAnonymousAuthenticationToken
regardless of whether the user is anonymous or a "real" logged in user.What's interesting is that
Authentication
is not included in the list of Supported Method Argument Types. If it just didn't work at all I'd say Baeldung was just mistaken, but the fact that it works for "real" authentication but not for anonymous makes me wonder where the problem is.I believe this may be a bug that has been around for a while (see a similar unanswered question).
If it should work as described by Baeldung then it needs to be fixed, and the documentation updated to include
Authentication
as an allowable parameter type.Here's my Spring Security configuration:
The text was updated successfully, but these errors were encountered: