yeb02 - SpringSecurity自定义成功处理器与失败处理器

学习ing,一起成长…

在这里插入图片描述

需求:登录成功后跳转到百度页面

  • 在SpringSecurity的配置类中配置successForwardUrl(“http://www.baidu.com”)
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public BCryptPasswordEncoder getBCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginProcessingUrl("/login")  //当发现/login时认为是登录,必须和表单提交地址一样
                .loginPage("/login.html")  //自定义登录的页面
                .successForwardUrl("http://www.baidu.com")
                .failureForwardUrl("/ToError");

        http.authorizeRequests()
                .antMatchers("/login.html","error.html").permitAll()  //login.html不需要认证
                .anyRequest().authenticated();   //所有请求都必须认证

        http.csrf().disable(); //关闭csrf认证
    }
}

但结果好像不是想象的那么样,出现404错误
在这里插入图片描述
我们可以到其源码中看
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述通过查看源码我们可以知道默认使用的登录成功处理器是ForwardAuthenticationSuccessHandler,而方法onAuthenticationSuccess()是转发,但是转发的特性之一:只能转发给当前web应用的资源。所以这里我们需要自定义成功处理器使用重定向可以重定向到任何资源

自定义成功处理器

public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    private String url;

    public MyAuthenticationSuccessHandler(String url) {
        this.url = url;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
         User user = (User) authentication.getPrincipal();  //获取登录信息
        user.getUsername();  //用户名
        user.getPassword(); //密码,为null安全考虑
        user.getAuthorities();  //权限
        httpServletResponse.sendRedirect(url);  //重定向
    }
}

使用

successForwardUrl与successHandler不能一起使用,会冲突

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginProcessingUrl("/login")  //当发现/login时认为是登录,必须和表单提交地址一样
                .loginPage("/login.html")  //自定义登录的页面
                //.successForwardUrl("http://www.baidu.com")
                .successHandler(new MyAuthenticationSuccessHandler("http://www.baidu.com"))
                .failureForwardUrl("/ToError");

        http.authorizeRequests()
                .antMatchers("/login.html","error.html").permitAll()  //login.html不需要认证
                .anyRequest().authenticated();   //所有请求都必须认证

        http.csrf().disable(); //关闭csrf认证
    }

自定义失败处理器

public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
    private String url;

    public MyAuthenticationFailureHandler(String url) {
        this.url = url;
    }

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.sendRedirect(url);
    }

 @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginProcessingUrl("/login")  //当发现/login时认为是登录,必须和表单提交地址一样
                .loginPage("/login.html")  //自定义登录的页面
                //.successForwardUrl("http://www.baidu.com")
                .successHandler(new MyAuthenticationSuccessHandler("http://www.baidu.com"))
                //.failureForwardUrl("/ToError");
                .failureHandler(new MyAuthenticationFailureHandler("/error.html"));
        http.authorizeRequests()
                .antMatchers("/login.html","error.html").permitAll()  //login.html不需要认证
                .anyRequest().authenticated();   //所有请求都必须认证

        http.csrf().disable(); //关闭csrf认证
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值