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

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>

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

springboot+springsecurity+thymeleaf的更多相关文章

  1. SpringBoot+SpringSecurity+Thymeleaf认证失败返回错误信息踩坑记录

    Spring boot +Spring Security + Thymeleaf认证失败返回错误信息踩坑记录 步入8102年,现在企业开发追求快速,Springboot以多种优秀特性引领潮流,在众多使 ...

  2. SpringBoot + SpringSecurity + Quartz + Layui实现系统权限控制和定时任务

    1. 简介   Spring Security是一个功能强大且易于扩展的安全框架,主要用于为Java程序提供用户认证(Authentication)和用户授权(Authorization)功能.    ...

  3. Springboot+JPA+Thymeleaf 校园博客完整小网站

    本文所属[知识林]:http://www.zslin.com/web/article/detail/35 此项目是一个比较简易的校园博客.麻雀虽小五脏俱全,虽然是比较简易的但是涉及的知识点还是比较全面 ...

  4. 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础授权权限

    上一篇<[原]无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限>介绍了实现Shiro的基础认证.本篇谈谈实现 ...

  5. 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限

    开发环境搭建参见<[原]无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页> 需求: ① 除了登录页面,在地址栏直接访问其他 ...

  6. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

  7. 从.Net到Java学习第九篇——SpringBoot下Thymeleaf

    从.Net到Java学习系列目录 Thymeleaf概述 Thymeleaf 是一个流行的模板引擎,该模板引擎采用java语言开发.模板引擎是一个技术名称,是跨领域平台的概念,在java语言体系下有模 ...

  8. 从.Net到Java学习第六篇——SpringBoot+mongodb&Thymeleaf&模型验证

    SpringBoot系列目录 SpringBoot整合mongodb MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.如果你没用过Mong ...

  9. SpringBoot 之Thymeleaf模板.

    一.前言 Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1.JSP 最明显的问题在于它看起来像HTML或X ...

随机推荐

  1. 自定义alert 确定、取消功能

    以删除为例,首先新建html <table border="1" cellpadding="0" cellspacing="0" id ...

  2. centos7构建kylo-0.10.1

      构建服务器使用centos7,8G内存.建议使用8G内存,因为内存不够失败了好几次. 系统需要提前安装一下组件: yum install -y gcc bzip2 rpm-build rpmdev ...

  3. Dll注入技术之APC注入

    APC注入的原理是利用当线程被唤醒时APC中的注册函数会被执行的机制,并以此去执行我们的DLL加载代码,进而完成DLL注入的目的,其具体流程如下:     1)当EXE里某个线程执行到SleepEx( ...

  4. Java-Class-C:org.springframework.http.HttpHeaders

    ylbtech-Java-Class-C:org.springframework.http.HttpHeaders 1.返回顶部 1.1. import org.springframework.htt ...

  5. python爬取文件时,内容为空

    解决方式: img_res = requests.get(src,headers=header)在header中加上referer防盗链加上防盗链header的例子: header = {" ...

  6. 利用DOM节点找对象和直接在标签属性中调函数传值this的书写区别

    同样的功能,不同的书写格式. 1.个人觉得比较繁琐的写法,但是比较常见,特别是在大项目的时候常用的就是这种方法: <div id="mouse" onmouseover=&q ...

  7. C++之运算符_算数运算符

    运算符 **作用:**用于执行代码的运算 | **运算符类型** | **作用** || -------------- | -------------------------------------- ...

  8. 1-电脑C盘(系统盘)清理

    推荐,亲测有效! 转自: https://baijiahao.baidu.com/s?id=1612762644229315967&wfr=spider&for=pc

  9. swt java 内嵌ActiveX控件

    这里用的是SWT/JFace开发application中SWT自带的org.eclipse.swt.ole.win32 包可以支持内嵌OLE和ActiveX. 具体用法如下: //创建一个OleFra ...

  10. HTML_表单标签

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...