-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Simplify MVC-based authentication #12985
Comments
NOTE: It could also return an |
I love this idea. I'm curious about the Also, if I am using What would be the proposal if someone wants to receive the credentials as JSON (which is pretty common)? Does In the case of |
I don't think that it would necessarily invoke an
Correct, since it would replace the
I'm not sure just yet; my initial impression is I'd hope this would be addressed upstream by Spring MVC. That is, I'd rather lean on Spring MVC primitives for content negotiation.
I think it would be one or the other. We'll need to take some care though as |
hi Have you considered providing simplified solutions for emailCode, capticatCode, and phoneCode? |
I'm excited to see this! It's depressing having to relearn things the Spring Security way that I can do easily in Spring MVC. However I think extracting username and password from the request is also among those "things that Spring MVC was built for" (I don't know about other authentication mechanisms). Does the proposal also allow the following? (I'm developing an SPA)
However I'm probably not "the primary audience for this feature". I want to use the Things I want to do in my login method:
|
@SentretC, thanks for the feedback.
Correct, you would not be required to use the
Spring Security has several authentication providers and I think it is worth adding more. It does not have one that does Captcha, for example, but I think that would be great to add.
Yes, you could still call
This is the idea of the
Yes, if I'm understanding you correctly, this would just be standard Spring MVC, Spring Security wouldn't need to get in the way. |
Thanks for the ideas, @BingChunMoLi. I'm not sure I've got your idea just yet, but at first sight, they sound more like authentication providers. Please consider providing more detail in a separate ticket. |
It's common in an application to use Spring MVC to publish a custom login page, for example like so:
And it's quite reasonable that the same application may want to do the same with customizing the subsequent POST:
In this case, the application needs to remember to do several things, like store the context in
SecurityContextHolderStrategy
andSecurityContextRepository
and invoke the set ofSessionAuthenticationStrategy
s.Often, the easiest thing is for the application to configure a custom filter that extends
AbstractAuthenticationProcessingFilter
instead. This can lead to some disjointedness in the application since, in this brief example, theGET
is in Spring MVC, but thePOST
is in theSecurityFilterChain
. It also forces the application down a path of using Spring-Security-specific components for things that Spring MVC was built for.It would be nice if those using Spring MVC for processing logins didn't have to remember so much boilerplate. In addition to making it simpler, this will make it more secure as well for circumstances like session fixation protection.
One way to do this would be to introduce addtional method argument resolvers, like so:
The
@FormLogin
annotation would exercise Spring Security components to read the request and formulate the correctAuthentication
instance. TheAuthenticationResultProcessor
would be prepared to perform the needed Spring Security actions to complete authentication, like storing theSecurityContextRepository
.This approach lends itself nicely to allowing the application to use Spring MVC primitives instead of needing to learn and understand filters, authentication success/failure handlers, and the like.
The pattern could be repeated for other authentication mechanisms, as in
@OAuth2Login
and@Saml2Login
. TheAuthenticationResultProcessor
instances that are prepared could be different for each one. Consider the following:In this case, the argument resolver uses the correct
AuthenticationConverter
to prepare aSaml2AuthenticationToken
that includes the<saml2:Response>
, theRelyingPartyRegistration
, etc. And the response updates the security context repository, holder, and runs any session authentication strategies.If the controller method throws an exception, then the
ExceptionTranslationFilter
will take effect. Or, if that needs to be customized as well, then the application could use a Spring MVC@ExceptionHandler
instead. As this is already simple, no work is proposed here to simplify that.This ticket proposes the following work:
AuthenticationResultProcessor
interface with a#process(Authentication)
methodAuthenticationConverter
s to produce anAuthentication
instance corresponding to the appropriate annotationAuthenticationResultProcessor
instance corresponding to the appropriate annotation. It would mitigate session fixation, update the security context repository, and the other Spring Security boilerplate. Since it relies on Spring MVC for the rest, it would not perform any redirection or forwardingNote that this proposal does not reuse
AuthenticationSuccessHandler
as that has at least two drawbacks. The first is that it makes the@Controller
method signature more complex by requiring theHttpServletRequest
andHttpServletResponse
objects. The second is that implementations would fundamentally differ in their relationship to existing implementations to the point of mutual exclusion. For example, existingAuthenticationSuccessHandler
s primarily redirect and forward, but these new ones would do everything but that. ExistingAuthenticationSuccessHandler
s do not manage theSecurityContextRepository
,SecurityContextHolderStrategy
, orSessionAuthenticationStrategy
set, but these new ones would.Future optimizations could possibly include having
@Controller
methods return anAuthentication
instance for processing. In that case, the result handler would invoke theAuthenticationResultProcessor
as well as any globally configuredAuthenticationSuccess/FailureHandler
, for example:The text was updated successfully, but these errors were encountered: