太原网站建设公司大全,wordpress 梦月酱,凡客东西网,哈尔滨道里区目录
一、是什么
二、实战测试
2.1 认识
2.2 认证和授权
2.3 权限控制和注销
2.4 记住我 一、是什么
Spring Security是一个框架#xff0c;侧重于为java应用程序提供身份验证和授权。
Web应用的安全性主要分为两个部分#xff1a;
认证#xff08;Authentication侧重于为java应用程序提供身份验证和授权。
Web应用的安全性主要分为两个部分
认证Authentication和授权Authorization “认证”Authentication验证用户的凭证如用户名和密码等“授权”Authorization在系统验证用户身份后会授予访问资源的权限 二、实战测试
准备工作
1、新建一个springboot项目web模块导入thymeleaf依赖
2、导入静态资源
3、编写controller 4、测试 2.1 认识
Spring Security是针对Spring项目的安全框架我们仅需要引入spring-boot-starter-security模块进行少量配置即可实现强大的安全管理
其中的重要类有
WebSecurityConfigurerAdapter自定义Security的策略AuthenticationManagerBuilder自定义认证策略EnableWebSecurity开启WebSecurity模式
2.2 认证和授权
1、引入Spring Security 模块
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-security/artifactId
/dependency
2、编写Spring Security 配置类
①授权重写configure(HttpSecurity http)方法 ②认证重写configure(AuthenticationManagerBuilder auth)方法 注意如果不将前端传来的密码进行某种方式加密就会报错There is no PasswordEncoder mapped for the id “null”
spring security 官方推荐的是使用bcrypt加密方式
3、测试
2.3 权限控制和注销 1、开启自动配置的注销功能 2、前端增加一个注销的按钮index.html 导航栏中 3、默认注销完毕后跳转到登录页面若我们想实现注销完跳转到首页 4、测试 需求
用户没有登录时导航栏只显示登录按钮 用户登录后导航栏显示登录的用户信息及注销按钮以及只显示用户有打开权限的功能
1、需要结合thymeleaf实现所以要导入thymeleaf和spring security整合的依赖
!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 --
dependencygroupIdorg.thymeleaf.extras/groupIdartifactIdthymeleaf-extras-springsecurity5/artifactIdversion3.0.4.RELEASE/version
/dependency 2、前端
首先导入命名空间
xmlns:thhttp://www.thymeleaf.orgxmlns:sechttp://www.thymeleaf.org/thymeleaf-extras-springsecurity5
增加认证判断 3、测试
2.4 记住我 实现功能登录时点击“记住我”这样登录之后关闭浏览器再打开不需要重新登陆
1、开启记住我功能本质就是保存了cookie 2、前端 3、测试