springboot+springsecurity+thymeleaf

时间:2021-05-18 07:08:55

来源:听秦疆老师的课笔记

springsecurity是一个权限管理框架,用来授权,认证,加密等等......类似的工具还有shiro

1.整合

  我用的是springboot2.2.0版本,导入以下依赖。

  spring和security整合包我用的版本是thymeleaf-extras-springsecurity5,
  老师用的是thymeleaf-extras-springsecurity4
  如果使用thymeleaf-extras-springsecurity4,需要将springboot的版本调低至2.0.9及以下
  springboot和springsecurity版本不匹配,会产生thymeleaf和security联合使用不生效问题
 <dependencies>

         <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- thymeleaf和Security整合依赖 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.3.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

2.配置使用

  使用方式十分简单,用一个类继承WebSecurityConfigurerAdapter并重写方法即可

   不要忘记使用@EnableWebSecurity开启服务,交给spring管理

    

 /**
* @author Silent
* @date 2019/11/13 17:12:38
* @description @EnableWebSecurity 开启服务
* 1.授权
* 2.认证
*/
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/**
*
* @param http
* @throws Exception
* 授权:首页所有人可以访问,功能页只有对应有权限的人才能访问
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
// 请求授权的规则~
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
// 没有权限默认会到登录页面(security内置的登录页面),需要开启登录页面
//定制登录页面loginPage("/toLogin")
//默认表单name用户名是username,密码是password,自己定义需要usernameParameter("username").passwordParameter("password")
//loginProcessingUrl("/login");参数“login”与登录表单的action保持一致
/**
* 如果只配置loginPage而不配置loginProcessingUrl的话
* 那么loginProcessingUrl默认就是loginPage
* 你配置的loginPage("/toLogin") ,那么loginProcessingUrl就是"/toLogin",相应的action也改为“/toLogin”
*/
http.formLogin().loginPage("/toLogin")
.usernameParameter("username").passwordParameter("password")
.loginProcessingUrl("/login");
//防止网站攻击 csrf,阻止get,
http.csrf().disable();//关闭csrf功能,解决登录失败
//开启注销功能,跳到首页
http.logout().logoutSuccessUrl("/");
//开启记住我功能,cookies默认保存两周,自定义接受前端参数
http.rememberMe().rememberMeParameter("remember"); } //认证, springboot 2.1.x可以直接使用 @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//正常的话,这些数据应该从数据库读取 这里写入内存
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("silent").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
.and()
.withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
} }

3.在thymeleaf中使用springsecurity

  sec:authorize :判断信息是否存在

    sec:authentication:取出相应的值

 <!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>首页</title>
<!--semantic-ui-->
<link href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
<link th:href="@{/qinjiang/css/qinstyle.css}" rel="stylesheet">
</head>
<body> <!--主容器-->
<div class="ui container"> <div class="ui segment" id="index-header-nav" th:fragment="nav-menu">
<div class="ui secondary menu">
<a class="item" th:href="@{/index}">首页</a> <!--登录注销-->
<div class="right menu">
<!--未登录-->
<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>
</div>
<div sec:authorize="isAuthenticated()">
<a class="item" th:href="@{/logout}" >
<i class="sign-out icon"></i> 注销
</a>
</div> </div>
</div>
</div> <div class="ui segment" style="text-align: center">
<h3>Spring Security Study by 秦疆</h3>
</div> <div>
<br>
<div class="ui three column stackable grid">
<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> <div class="column" sec:authorize="hasRole('vip2')">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level 2</h5>
<hr>
<div><a th:href="@{/level2/1}"><i class="bullhorn icon"></i> Level-2-1</a></div>
<div><a th:href="@{/level2/2}"><i class="bullhorn icon"></i> Level-2-2</a></div>
<div><a th:href="@{/level2/3}"><i class="bullhorn icon"></i> Level-2-3</a></div>
</div>
</div>
</div>
</div> <div class="column" sec:authorize="hasRole('vip3')">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level 3</h5>
<hr>
<div><a th:href="@{/level3/1}"><i class="bullhorn icon"></i> Level-3-1</a></div>
<div><a th:href="@{/level3/2}"><i class="bullhorn icon"></i> Level-3-2</a></div>
<div><a th:href="@{/level3/3}"><i class="bullhorn icon"></i> Level-3-3</a></div>
</div>
</div>
</div>
</div> </div>
</div> </div> <script th:src="@{/qinjiang/js/jquery-3.1.1.min.js}"></script>
<script th:src="@{/qinjiang/js/semantic.min.js}"></script> </body>
</html>

springboot+springsecurity+thymeleaf

https://github.com/Sevenwsq/springsecurity-demo/tree/springsecurity-demo 项目地址