自定义认证成功处理器

代码实现

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);
       }
    }
}
Logo

Authing 是一款以开发者为中心的全场景身份云产品,集成了所有主流身份认证协议,为企业和开发者提供完善安全的用户认证和访问管理服务

更多推荐