我想跟踪用户登录我的应用程序的时间。我有一些代码,我希望在用户通过身份验证后立即执行。问题是,我不知道应该在哪里调用它。spring-security有没有办法在身份验证后调用方法?
发布于 2012-08-31 20:10:51
可能会对某些人有用...在Spring 3的情况下,配置安全性:
<security:http use-expressions="true" auto-config="true">
<security:intercept-url pattern="..."/>
<security:form-login
authentication-failure-handler-ref="authFailureHandler"
authentication-success-handler-ref="authSuccessHandler"/>
<security:logout success-handler-ref="logoutSuccessHandler"
invalidate-session="true"/>
<security:session-management session-fixation-protection="newSession"/>
</security:http>
<bean id="authFailureHandler" class="mine.AuthenticationFailureHandlerImpl"/>
<bean id="authSuccessHandler" class="mine.AuthenticationSuccessHandlerImpl"/>
<bean id="logoutSuccessHandler" class="mine.LogoutSuccessHandlerImpl"/>并实现一个适当的类:
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//do what you want with
response.getOutputStream().write("success".getBytes());
}
}您可以通过该xml配置链接资源。
发布于 2012-05-25 10:47:18
最好的方法是创建一个应用程序侦听器,并将其注册到spring安全上下文。
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent;
public class AuthenticationSuccessListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {
@Override
public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
System.out.println("User Logged In");
}
}确保将上述类spring-security.xml作为bean添加。还有许多其他类型的安全事件侦听器可以侦听,请检查类型层次结构,以获得您可以侦听的所有类型的安全事件的列表。
发布于 2013-08-01 14:58:48
如果您想继续使用默认行为,但只是在其间执行您自己的业务逻辑,那么您可以在返回之前使用extend SimpleUrlAuthenticationSuccessHandler并调用super.onAuthenticationSuccess(request, response, authentication);。更多详细信息请参阅https://stackoverflow.com/a/6770785/418439
https://stackoverflow.com/questions/2579431
复制相似问题