上门做睫毛哪个网站,网页设计设计一个网站首页,免费的行情网站app网页,医院网站建设的资料使用spring认证登录#xff0c;登录之后#xff0c;一般还需要进行其他处理#xff0c;例如#xff1a;保存登录时间、登录ip到数据库#xff0c;缓存用户信息到redis数据库等等#xff0c;这些操作可以通过自定义一个登录成功处理器来处理。
自定义认证成功处理器
只需…使用spring认证登录登录之后一般还需要进行其他处理例如保存登录时间、登录ip到数据库缓存用户信息到redis数据库等等这些操作可以通过自定义一个登录成功处理器来处理。
自定义认证成功处理器
只需要继承AuthenticationSucdessHandler即可实现自定义处理器
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {Overridepublic void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,Authentication authentication) throws IOException, ServletException {request.getRequestDispatcher(/login).forward(request, response);}}
我自定义的处理器很简单直接转发请求到/login这是因为我在使用spring security 之前就已经写了一个登录功能这样不用修改代码就可以运行之前的登录逻辑了。
当然spring-security.xml文件也要做小小的修改 httpintercept-url pattern/user/** accesshasRole(USER) /intercept-url pattern/admin/** accesshasRole(ADMIN) /form-login login-page/loginlogin-processing-url/loginauthentication-failure-url/login?errorusername-parameterphonepassword-parameterpassword authentication-success-handler-refauthSuccess/logout invalidate-sessiontruelogout-urlloginoutlogout-success-url/login//http !-- 自定义认证成功处理器 --beans:bean idauthSuccess classcom.huanle.utils.security.AuthenticationSuccessHandlerImpl/beans:bean上面的代码中我们定义了一个AuthenticationSuccessHandlerImpl的bean
然后在form-login标签中添加了authentication-success-handler-refauthSuccess删除了default-target-url属性因为自定义处理器之后default-target-url就失效了。