简体   繁体   中英

Using Spring, how can I manually fire AuthenticationSuccessHandler

I want to provide the user an alternative means of logging in with a token. I already handle authenticating the user in the PreAuthenticationFilter class but then I need to fire my

AuthenticationSuccessHandler.onAuthenticationSuccess()

which does a

super.onAuthenticationSuccess()

which fires the method in it's base class of

SavedRequestAwareAuthenticationSuccessHandler.

How can I fire the AuthenticationSuccessHandler from my preauthentication filter? I tried setting the authentication of the SecurityContextHolder thinking it would automatically fire off the AuthenticationSuccessHandler but it does not. This is how I tried to do that:

Authentication authentication = new UsernamePasswordAuthenticationToken(Username, Password);
SecurityContextHolder.getContext().setAuthentication(authentication);

Unfortunately, that doesn't work though. So I need to know how to fire the AuthenticationSuccessHandler from a preauthentication filter.

You could probably be inspired of how the RememberMeAuthenthicationFilter works in spring-security. It has a reference to the AuthenticationSuccessHandler , and calls onAuthenticationSuccess manually.

You have to extend the PreAuthenticationFilter and overwrite the method successfulAuthentication. There you can trigger your injected success handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM