您当前的位置: 首页 >  陈橙橙丶 spring

SpringSecurity(六)注销登录

陈橙橙丶 发布时间:2022-03-11 17:53:21 ,浏览量:5

注销登录

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

关注
打赏
1688896170
查看更多评论

陈橙橙丶

暂无认证

  • 5浏览

    0关注

    60博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0500s