Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。
我们使用的还是springboot的环境,所以还是先建立一个springboot项目,只需要勾选spring web与thymeleaf 模块。
一、导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
二、自定义Config
自定义一个Config配置类,我起名为SecurityConfig。继承WebSecurityConfigurerAdapter 并加上@EnableWebSecurity注解,表明开启了Security,交给springboot托管。
package com.xyh.Config;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}
需要继承两个类 configure(HttpSecurity http) (授权) 与 configure(AuthenticationManagerBuilder auth) (认证)
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人都可以访问,功能页只有对应的人才可以访问
//请求授权的规则
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")//每个等级只能到对应的level中
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//没有权限,会跳转到登录界面,需要开启登录界面(Security自带的login页面,若没有登录则跳抓到登录页面,这条代码就是表示开启登录界面)
http.formLogin();
}
//认证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())//需要密码的加密
.withUser("xss").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")//注册每一个用户的账号,密码,以及权限
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3");
}
注销用户
spring Security 也帮我们封装了一个方法,我们直接调用就可以
//注销
http.logout().logoutSuccessUrl("/");
logoutSuccessUrl("/")的作用是注销后跳转到哪个界面
三、thymeleaf集成spring Security
有时候,在前端我们想要让用户只能看到自己权限之内的东西,之前用th:if来判断,但现在我们可以集成spring Security来更好的实现这一目的
一、导入依赖
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
注意:新版的springboot需要的是thymeleaf-extras-springsecurity5,导入其他版本的可能会无法使用
二、添加约束
在要使用的前端页面的头部添加约束
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
三、使用
我们现在的想法是让如果没有登录,显示登录二字,如果已经登录,则显示登陆人的用户名,权限以及注销二字
<!--如果未登录-->
<div sec:authorize="!isAuthenticated()">
<a class="item" th:href="@{/toLogin}">
<i class="address card icon"></i> 登录
</a>
</div>
<!--如果已登录-->
<div sec:authorize="isAuthenticated()">
<a class="item">
用户名:<span sec:authentication="name"></span>
角色:<span sec:authentication="principal.authorities"></span>
</a>
<a class="item" th:href="@{/logout}">
<i class="sign-out icon"></i> 注销
</a>
</div>
想让权限vip1只能看到vip1的level,则判断Role里是否有“vip1”
<div class="column" sec:authorize="hasRole('vip1')">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level 1</h5>
<hr>
<div><a th:href="@{/level1/1}"><i class="bullhorn icon"></i> Level-1-1</a></div>
<div><a th:href="@{/level1/2}"><i class="bullhorn icon"></i> Level-1-2</a></div>
<div><a th:href="@{/level1/3}"><i class="bullhorn icon"></i> Level-1-3</a></div>
</div>
</div>
</div>
</div>
登录中经常有“记住我”这个选项,所以springsecurity也存在这个配置
在configure(HttpSecurity http)中配置
//开启记住我功能 保存cookie,默认保存2周
http.rememberMe().rememberMeParameter("remember");
则表示开启了“记住我”功能,后面的rememberMeParameter("remember");表示在前端页面上组件的name为“remember”。
上面说过当我们没有登录时,开启http.formLogin(),springsecurity会自动跳到自己内部就有的login页面,当然我们也可以自己写一个登录界面,很简单,就是在http.formLogin()后面添加一个链式模块
//没有权限,会跳转到登录界面,需要开启登录界面
http.formLogin().loginPage("/toLogin").loginProcessingUrl("/lll");
loginPage表示我们自己的登录页面的链接,loginProcessingUrl表示前端页面要登录时所提交的路径。
本文介绍了如何在Spring Boot项目中集成Spring Security,并使用Thymeleaf进行权限控制。首先,通过引入相关依赖并创建自定义的SecurityConfig配置类来实现安全控制。然后,详细讲解了用户注销功能的设置。接着,讨论了Thymeleaf与Spring Security的集成,包括导入依赖、添加页面约束和权限判断的使用。最后,提到了Spring Security的“记住我”功能及其配置方式。
1058

被折叠的 条评论
为什么被折叠?



