SpringSecurity(六)注销登录

注销登录

SpringScurity中提供了默认的注销页面,当然我们也可以根据自己的需求对注销登录进行定制。

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
	@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and().formLogin()
                .and()
                .logout()
                .logoutUrl("/logout")
                .invalidateHttpSession(true)
                .clearAuthentication(true)
                .logoutSuccessUrl("/login.html")
                .permitAll()
                .and()
                .csrf().disable();
    }
}

(1)、logout:开启注销登录配置

(2)、logoutUrl:指定了注销登录的请求地址,默认是GET,路径为/logout

(3)、invalidateHttpSession:表示是否使session失效,默认为true

(4)、clearAuthentication:表示是否清楚认证信息,默认为true

(5)、logoutSuccessUrl:表示注销登录之后的跳转地址。


配置完成后,再次启动项目,登录成功之后,在浏览器中输入http://localhost:8080/logout就可以发起注销登录请求了。注销登录成功后,会自动跳转到login.html页面。

如果项目有需要,开发者也可以配置多个注销登录的请求,同时还可以指定请求的方法:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and().formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/doLogin")
                .successHandler(new MyAuthenticationSuccessHandler())
                .failureHandler(new MyAuthenticationFailureHandler())
                .usernameParameter("uname")
                .passwordParameter("passwd")
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/logout")
//                .invalidateHttpSession(true)
                .logoutRequestMatcher(
                        new OrRequestMatcher(
                                new AntPathRequestMatcher("/logout1", "GET"),
                                new AntPathRequestMatcher("/logout2", "POST")
                        )
                )
                .clearAuthentication(true)
                .logoutSuccessUrl("/login.html")
                .and()
                .csrf().disable();
    }

上面配置表示注销登录路径有两个:

  • 第一个是/logout1,请求方法是GET
  • 第二个是/logout2,请求方法是POST

使用任意一个请求都可以完成登录注销。

如果是前后的分离的架构,注销登录成功后就不需要页面跳转了,只需要将注销成功的信息返回给前端即可,此时我们额可以自定义返回内容。

 @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and().formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/doLogin")
                .successHandler(new MyAuthenticationSuccessHandler())
                .failureHandler(new MyAuthenticationFailureHandler())
                .usernameParameter("uname")
                .passwordParameter("passwd")
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/logout")
//                .invalidateHttpSession(true)
                .logoutRequestMatcher(
                        new OrRequestMatcher(
                                new AntPathRequestMatcher("/logout1", "GET"),
                                new AntPathRequestMatcher("/logout2", "POST")
                        )
                )
                .logoutSuccessHandler((req,res,aut)->{
                    res.setContentType("application/json;charset=utf-8");
                    HashMap<String,Object> resp = new HashMap<>();
                    resp.put("status",200);
                    resp.put("msg","注销成功!");
                    ObjectMapper om = new ObjectMapper();
                    final String writeValueAsString = om.writeValueAsString(resp);
                    res.getWriter().write(writeValueAsString);
                })
                .clearAuthentication(true)
                .and()
                .csrf().disable();
    }

配置logoutSuccessHandler和logoutSuccessUrl类似前面所介绍的successHandler和defaultSuccessUrl之间的关系,只是类不同而已,这里就不在细说了,有兴趣的朋友可以参考一下前面两篇文章。


配置完成后,启动项目,登录成功后去注销登录,无论是使用/logout1还是/logout2进行注销,只要注销成功,就会返回一段JSON字符串,如果我们希望为不同的注销地址返回不同的结果,可以参考如下:

 @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and().formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/doLogin")
                .successHandler(new MyAuthenticationSuccessHandler())
                .failureHandler(new MyAuthenticationFailureHandler())
                .usernameParameter("uname")
                .passwordParameter("passwd")
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/logout")
//                .invalidateHttpSession(true)
                .logoutRequestMatcher(
                        new OrRequestMatcher(
                                new AntPathRequestMatcher("/logout1", "GET"),
                                new AntPathRequestMatcher("/logout2", "POST")
                        )
                )
                .defaultLogoutSuccessHandlerFor((req,res,auth)->{
                    res.setContentType("application/json;charset=utf-8");
                    HashMap<String,Object> resp = new HashMap<>();
                    resp.put("status",200);
                    resp.put("msg","logout1注销成功!");
                    ObjectMapper om = new ObjectMapper();
                    final String writeValueAsString = om.writeValueAsString(resp);
                    res.getWriter().write(writeValueAsString);
                },new AntPathRequestMatcher("/logout1","GET"))
                .defaultLogoutSuccessHandlerFor((req,res,auth)->{
                    res.setContentType("application/json;charset=utf-8");
                    HashMap<String,Object> resp = new HashMap<>();
                    resp.put("status",200);
                    resp.put("msg","logout2注销成功!");
                    ObjectMapper om = new ObjectMapper();
                    final String writeValueAsString = om.writeValueAsString(resp);
                    res.getWriter().write(writeValueAsString);
                },new AntPathRequestMatcher("/logout2","POST"))
                .clearAuthentication(true)
                .logoutSuccessUrl("/login.html")
                .and()
                .csrf().disable();
    }

通过defaultLogoutSuccessHandlerFor方法可以注册多个不同的注销成功回调函数,该方法第一个参数是注销成功回调,第二个参数是具体的注销请求。当用户注销成功后,使用了哪个注销请求,就会给出对象的相应。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈橙橙丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值