SpringSecurity注销设置的方法
作者:搞钱自律 发布时间:2023-08-05 20:46:15
Spring Security中也提供了默认的注销配置,在开发时也可以按照自己需求对注销进行个性化定制
开启注销 默认开启
package com.example.config;
import com.example.handler.MyAuthenticationFailureHandler;
import com.example.handler.MyAuthenticationSuccessHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
//【注意事项】放行资源要放在前面,认证的放在后面
http.authorizeRequests()
.mvcMatchers("/index").permitAll() //代表放行index的所有请求
.mvcMatchers("/loginHtml").permitAll() //放行loginHtml请求
.anyRequest().authenticated()//代表其他请求需要认证
.and()
.formLogin()//表示其他需要认证的请求通过表单认证
//loginPage 一旦你自定义了这个登录页面,那你必须要明确告诉SpringSecurity日后哪个url处理你的登录请求
.loginPage("/loginHtml")//用来指定自定义登录界面,不使用SpringSecurity默认登录界面 注意:一旦自定义登录页面,必须指定登录url
//loginProcessingUrl 这个doLogin请求本身是没有的,因为我们只需要明确告诉SpringSecurity,日后只要前端发起的是一个doLogin这样的请求,
//那SpringSecurity应该把你username和password给捕获到
.loginProcessingUrl("/doLogin")//指定处理登录的请求url
.usernameParameter("uname") //指定登录界面用户名文本框的name值,如果没有指定,默认属性名必须为username
.passwordParameter("passwd")//指定登录界面密码密码框的name值,如果没有指定,默认属性名必须为password
// .successForwardUrl("/index")//认证成功 forward 跳转路径,forward代表服务器内部的跳转之后,地址栏不变 始终在认证成功之后跳转到指定请求
// .defaultSuccessUrl("/index")//认证成功 之后跳转,重定向 redirect 跳转后,地址会发生改变 根据上一保存请求进行成功跳转
.successHandler(new MyAuthenticationSuccessHandler()) //认证成功时处理 前后端分离解决方案
// .failureForwardUrl("/loginHtml")//认证失败之后 forward 跳转
// .failureUrl("/login.html")//认证失败之后 redirect 跳转
.failureHandler(new MyAuthenticationFailureHandler())//用来自定义认证失败之后处理 前后端分离解决方案
.and()
.logout()
.logoutUrl("/logout") //指定注销登录url 默认请求方式必须:GET
.invalidateHttpSession(true) //会话失效 默认值为true,可不写
.clearAuthentication(true) //清除认证标记 默认值为true,可不写
.logoutSuccessUrl("/loginHtml") //注销成功之后跳转页面
.and()
.csrf().disable(); //禁止csrf 跨站请求保护
}
}
通过logout()方法开启注销配置
logoutUrl指定退出登录请求地址,默认是GET请求,路径为/logout
invalidateHttpSession 退出时是否是session失效,默认值为true
clearAuthentication 退出时是否清除认证信息,默认值为true
logoutSuccessUrl 退出登录时跳转地址
测试
先访问http://localhost:8080/hello,会自动跳转到http://localhost:8080/loginHtml登录界面,输入用户名和密码,地址栏会变化为:http://localhost:8080/doLogin,然后重新访问http://localhost:8080/hello,此时可以看到hello的数据,表示登录认证
成功然后访问http://localhost:8080/logout,会自动跳转到http://localhost:8080/loginHtml登录页面,然后在访问http://localhost:8080/hello,发现会跳转到http://localhost:8080/loginHtml登录界面,表示后端认定你未登录了,需要你登录认证
配置多个注销登录请求
如果项目中也有需要,开发者还可以配置多个注销登录的请求,同时还可以指定请求的方法
新增退出controller
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class LogoutController {
@RequestMapping("/logoutHtml")
public String logout(){
return "logout";
}
}
新增退出界面
logout.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/" lang="en">
<head>
<meta charset="UTF-8">
<title>注销</title>
</head>
<body>
<h1>注销</h1>
<form th:action="@{/bb}" method="post">
<input type="submit" value="注销">
</form>
</body>
</html>
修改配置信息
package com.example.config;
import com.example.handler.MyAuthenticationFailureHandler;
import com.example.handler.MyAuthenticationSuccessHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
//【注意事项】放行资源要放在前面,认证的放在后面
http.authorizeRequests()
.mvcMatchers("/index").permitAll() //代表放行index的所有请求
.mvcMatchers("/loginHtml").permitAll() //放行loginHtml请求
.anyRequest().authenticated()//代表其他请求需要认证
.and()
.formLogin()//表示其他需要认证的请求通过表单认证
//loginPage 一旦你自定义了这个登录页面,那你必须要明确告诉SpringSecurity日后哪个url处理你的登录请求
.loginPage("/loginHtml")//用来指定自定义登录界面,不使用SpringSecurity默认登录界面 注意:一旦自定义登录页面,必须指定登录url
//loginProcessingUrl 这个doLogin请求本身是没有的,因为我们只需要明确告诉SpringSecurity,日后只要前端发起的是一个doLogin这样的请求,
//那SpringSecurity应该把你username和password给捕获到
.loginProcessingUrl("/doLogin")//指定处理登录的请求url
.usernameParameter("uname") //指定登录界面用户名文本框的name值,如果没有指定,默认属性名必须为username
.passwordParameter("passwd")//指定登录界面密码密码框的name值,如果没有指定,默认属性名必须为password
// .successForwardUrl("/index")//认证成功 forward 跳转路径,forward代表服务器内部的跳转之后,地址栏不变 始终在认证成功之后跳转到指定请求
// .defaultSuccessUrl("/index")//认证成功 之后跳转,重定向 redirect 跳转后,地址会发生改变 根据上一保存请求进行成功跳转
.successHandler(new MyAuthenticationSuccessHandler()) //认证成功时处理 前后端分离解决方案
// .failureForwardUrl("/loginHtml")//认证失败之后 forward 跳转
// .failureUrl("/login.html")//认证失败之后 redirect 跳转
.failureHandler(new MyAuthenticationFailureHandler())//用来自定义认证失败之后处理 前后端分离解决方案
.and()
.logout()
// .logoutUrl("/logout") //指定注销登录url 默认请求方式必须:GET
.logoutRequestMatcher(new OrRequestMatcher(
new AntPathRequestMatcher("/aa","GET"),
new AntPathRequestMatcher("/bb","POST")
)
)
.invalidateHttpSession(true) //会话失效 默认值为true,可不写
.clearAuthentication(true) //清除认证标记 默认值为true,可不写
.logoutSuccessUrl("/loginHtml") //注销成功之后跳转页面
.and()
.csrf().disable(); //禁止csrf 跨站请求保护
}
}
测试
GET /aa 跟上面测试方法一样
POST /bb
先访问http://localhost:8080/hello,会自动跳转到http://localhost:8080/loginHtml登录界面,输入用户名和密码,地址栏会变化为:http://localhost:8080/doLogin,然后重新访问http://localhost:8080/hello,此时可以看到hello的数据,表示登录认证成功
然后访问http://localhost:8080/logoutHtml,会跳出注销页面,点击“注销”按钮,会返回到http://localhost:8080/loginHtml登录界面,再重新访问http://localhost:8080/hello,发现会跳转到http://localhost:8080/loginHtml登录界面,表示注销成功,需要重新登录。
前后端分离注销配置
如果是前后端分离开发,注销成功之后就不需要页面跳转了,只需要将注销成功的信息返回前端即可,此时我们可以通过自定义LogoutSuccessHandler实现来返回内容注销之后信息
添加handler
package com.example.handler;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* 自定义注销成功之后处理
*/
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
Map<String,Object> result = new HashMap<>();
result.put("msg","注销成功,当前认证对象为:"+authentication);
result.put("status",200);
result.put("authentication",authentication);
response.setContentType("application/json;charset=UTF-8");
String s = new ObjectMapper().writeValueAsString(result);
response.getWriter().println(s);
}
}
修改配置信息
logoutSuccessHandler
package com.example.config;
import com.example.handler.MyAuthenticationFailureHandler;
import com.example.handler.MyAuthenticationSuccessHandler;
import com.example.handler.MyLogoutSuccessHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
//【注意事项】放行资源要放在前面,认证的放在后面
http.authorizeRequests()
.mvcMatchers("/index").permitAll() //代表放行index的所有请求
.mvcMatchers("/loginHtml").permitAll() //放行loginHtml请求
.anyRequest().authenticated()//代表其他请求需要认证
.and()
.formLogin()//表示其他需要认证的请求通过表单认证
//loginPage 一旦你自定义了这个登录页面,那你必须要明确告诉SpringSecurity日后哪个url处理你的登录请求
.loginPage("/loginHtml")//用来指定自定义登录界面,不使用SpringSecurity默认登录界面 注意:一旦自定义登录页面,必须指定登录url
//loginProcessingUrl 这个doLogin请求本身是没有的,因为我们只需要明确告诉SpringSecurity,日后只要前端发起的是一个doLogin这样的请求,
//那SpringSecurity应该把你username和password给捕获到
.loginProcessingUrl("/doLogin")//指定处理登录的请求url
.usernameParameter("uname") //指定登录界面用户名文本框的name值,如果没有指定,默认属性名必须为username
.passwordParameter("passwd")//指定登录界面密码密码框的name值,如果没有指定,默认属性名必须为password
// .successForwardUrl("/index")//认证成功 forward 跳转路径,forward代表服务器内部的跳转之后,地址栏不变 始终在认证成功之后跳转到指定请求
// .defaultSuccessUrl("/index")//认证成功 之后跳转,重定向 redirect 跳转后,地址会发生改变 根据上一保存请求进行成功跳转
.successHandler(new MyAuthenticationSuccessHandler()) //认证成功时处理 前后端分离解决方案
// .failureForwardUrl("/loginHtml")//认证失败之后 forward 跳转
// .failureUrl("/login.html")//认证失败之后 redirect 跳转
.failureHandler(new MyAuthenticationFailureHandler())//用来自定义认证失败之后处理 前后端分离解决方案
.and()
.logout()
// .logoutUrl("/logout") //指定注销登录url 默认请求方式必须:GET
.logoutRequestMatcher(new OrRequestMatcher(
new AntPathRequestMatcher("/aa","GET"),
new AntPathRequestMatcher("/bb","POST")
)
)
.invalidateHttpSession(true) //会话失效 默认值为true,可不写
.clearAuthentication(true) //清除认证标记 默认值为true,可不写
// .logoutSuccessUrl("/loginHtml") //注销成功之后跳转页面
.logoutSuccessHandler(new MyLogoutSuccessHandler()) //注销成功之后处理 前后端分离解决方案
.and()
.csrf().disable(); //禁止csrf 跨站请求保护
}
}
测试
跟第一个测试方法是一样的
来源:https://blog.csdn.net/weixin_43472934/article/details/124566778


猜你喜欢
- 在java程序开发中,ftp用的比较多,经常打交道,比如说向FTP服务器上传文件、下载文件,本文给大家介绍如何利用jakarta commo
- 本文实例为大家分享了SpringBoot实现分页功能的具体代码,供大家参考,具体内容如下新建demo\src\main\java\com\e
- 将Object类转换为实体类问题描述在用SpringBoot写controller的时候,需要接受一个map的Object,之后要把Obje
- 这篇文章主要介绍了Spring MVC处理方法返回值过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 本文实例为大家分享了flutter实现底部导航栏切换的具体代码,供大家参考,具体内容如下思路:MaterialApp是提供了bottomna
- 本文实例讲述了C#格式化json字符串的方法。分享给大家供大家参考,具体如下:将Json字符串转化成格式化表示的方法: 字符串反序列化为对象
- 引言在前面的内容中,我们先是一一介绍了Collection集合中都有哪些种类的集合,并且详细地讲解了List集合中的相关知识,那么今天我们来
- springboot项目启动的时候参数无效今天启动一个springboot项目发现启动的时候输入的参数都是不能生效,但是yaml文件的配置却
- 一、MyBatis的逆向⼯程(1)所谓的逆向⼯程是:根据数据库表逆向⽣成Java的pojo类,SqlMapper.xml⽂件,以及Mappe
- 树概念及结构树是一种 非线性 的数据结构,它是由 n ( n>=0 )个有限结点组成一个具有层次关系的集合把它叫做树是因 为它看起来像
- springboot启动是通过一个main方法启动的,代码如下@SpringBootApplicationpublic class Appl
- 一、背景在上一篇文章中,我们使用Seata整合了SpringBoot,在这篇文章中我们使用Seata整合SpringCloud。同时有了上一
- 这篇文章主要介绍了SpringBoot登录判断代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 本文汇集36个Android开发常用经典代码片段,包括拨打电话、发送短信、唤醒屏幕并解锁、是否有网络连接、动态显示或者是隐藏软键盘等,希望对
- 很多程序员都以自己写的代码的行数作为自己程序员阅历的一个标志,如何统计呢,以下是具体内容。小编,已经快学了两年编程了。昨天突发奇想,想统计下
- 前言大家都知道类的继承规则:1、派生类自动包含基类的所有成员。但对于基类的私有成员,派生类虽然继承了,但是不能在派生类中访问。2、所有的类都
- 自定义缓存 - ehcacheEhcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器1.导包&l
- 可能导致问题的原因:1.nacos中的配置文件名不规范,官网有命名规则:“前缀”-&ldqu
- 一.相关知识:Java多线程程序设计到的知识:(一)对同一个数量进行操作(二)对同一个对象进行操作(三)回调方法使用(四)线程同步,死锁问题
- spring boot 使用profile来分区配置很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时