
SpringSecurity自定义认证成功处理器
自定义认证成功处理器代码实现1.实现AuthenticationSuccessHandler接口第一步:实现 AuthenticationSuccessHandler@Component("customAuthenticationSuccessHandler")public class CustomAuthenticationSuccessHandler implements Authentica
·
自定义认证成功处理器
代码实现
1.实现AuthenticationSuccessHandler接口
第一步:实现 AuthenticationSuccessHandler
@Component("customAuthenticationSuccessHandler")
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication){
//以返回JSON数据为例
Result result = Result.ok("认证成功");
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(result.toJsonString());
}
}
2.配置SpringScurry
第二步:配置SpringScurry
@Autowired
private AuthenticationSuccessHandler customAuthenticationSuccessHandler;
@Override
//前后代码省略
protected void configure(HttpSecurity http) throws Exception {
http
//前后代码省略
.successHandler(customAuthenticationSuccessHandler)
}
跳转到上次访问页面
当我们要实现页面跳转时,我们只需要继承AuthenticationSuccessHandler
的实现类SavedRequestAwareAuthenticationSuccessHandler
然后调用父类的方法
@Component("customAuthenticationSuccessHandler")
public class CustomAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//
super.onAuthenticationSuccess(request,response,authentication);
}
}
}
更多推荐
所有评论(0)