简体   繁体   English

使用Spring,如何手动触发AuthenticationSuccessHandler

[英]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 我已经在PreAuthenticationFilter类中处理了对用户的身份验证,但是随后我需要触发我的

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? 如何从预AuthenticationSuccessHandler过滤器中启动AuthenticationSuccessHandler I tried setting the authentication of the SecurityContextHolder thinking it would automatically fire off the AuthenticationSuccessHandler but it does not. 我尝试设置SecurityContextHolder的身份验证,认为它会自动触发AuthenticationSuccessHandler但事实并非如此。 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. 因此,我需要知道如何从预AuthenticationSuccessHandler过滤器中启动AuthenticationSuccessHandler

You could probably be inspired of how the RememberMeAuthenthicationFilter works in spring-security. 您可能会因为RememberMeAuthenthicationFilter如何在春季安全性中工作而受到启发。 It has a reference to the AuthenticationSuccessHandler , and calls onAuthenticationSuccess manually. 它具有对AuthenticationSuccessHandler的引用,并手动调用onAuthenticationSuccess

You have to extend the PreAuthenticationFilter and overwrite the method successfulAuthentication. 您必须扩展PreAuthenticationFilter并覆盖成功的方法。 There you can trigger your injected success handler. 在那里,您可以触发注入的成功处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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