spring security是针对spring项目的安全框架,也是spring boot底层安全模块默认的技术选型。它可以实现强大的web安全控制。对于安全控制,我们仅需引入spring-boot-starter-security模块,进行少量配置,即可实现强大的安全管理。

  • 应用程序的两个主要区域是认证和授权。这两个主要区域是spring security的两个目标。
  • 认证(Authentication),建立一个它声明主体的过程。
  • 授权(Authorization)指确定一个主体是否允许在你的应用程序执行一个动作的过程。

1、引入spring security的starter。
2、编写spring security的配置类。
3、控制请求的访问权限。

示例:
实现登陆、认证、授权、权限控制、注销、记住我、定制登陆页面功能。
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.qublog</groupId><artifactId>springboot13_security</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot13_security</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.3.0.RELEASE</spring-boot.version></properties><dependencies><dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-springsecurity5</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></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><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

pages/level1

<!--1.html-->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><a th:href="@{/}">返回</a><h1>罗汉拳</h1><p>罗汉拳站当央,打起来不要慌</p>
</body>
</html><!--2.html-->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><a th:href="@{/}">返回</a><h1>武当长拳</h1><p>长一点在长一点</p>
</body>
</html><!--3.html-->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><a th:href="@{/}">返回</a><h1>全真剑法</h1><p>全都是真的</p>
</body>
</html>

pages/level2

<!--1.html-->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><a th:href="@{/}">返回</a><h1>太极拳</h1><p>一个西瓜圆又圆 劈它一刀成两半 你一半来 给你你不要 给他他不收 那就不给 把两人撵走 他们不走你走 走啦,一挥手,伤自尊不买西瓜别缠我,缓慢纠缠様 两人缠我赖皮,手慢动作左右挥动 看我厉害,转头缓步拍苍蝇状 拍死了,手抱西瓜状+奥特曼十字手+广播操准备运动的站立</p>
</body>
</html><!--2.html-->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><a th:href="@{/}">返回</a><h1>七伤拳</h1><p>练这拳的人全都死了</p>
</body>
</html><!--3.html-->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><a th:href="@{/}">返回</a><h1>梯云纵</h1><p>踩自己的脚往上跳</p>
</body>
</html>

pages/level3

<!--1.html-->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><a th:href="@{/}">返回</a><h1>葵花宝典</h1><p>欲练神功,挥刀自宫</p>
</body>
</html><!--2.html-->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><a th:href="@{/}">返回</a><h1>龟派气功</h1><p>龟-派-气-功-波</p>
</body>
</html><!--3.html-->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><a th:href="@{/}">返回</a><h1>独孤九剑</h1><p>欲练此剑,必先犯贱</p>
</body>
</html>

login.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body><h1 align="center">欢迎登陆武林秘籍管理系统</h1><hr><div align="center"><form th:action="@{/userlogin}" method="post">用户名:<input name="user"/><br>密码:<input name="pwd"><br/><input type="checkbox" name="remeber">记住我<br/><input type="submit" value="登陆"></form></div>
</body>
</html>

welcome.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 align="center">欢迎光临武林秘籍管理系统</h1>
<div sec:authorize="!isAuthenticated()"><h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/userlogin}">请登录</a></h2>
</div><div sec:authorize="isAuthenticated()"><h2><span sec:authentication="name"></span>,您好,您的角色有:<span sec:authentication="principal.authorities"></span></h2><form th:action="@{/logout}" method="post"><input type="submit" value="注销"/></form>
</div>
<hr><div sec:authorize="hasRole('VIP1')"><h3>普通武功秘籍</h3><ul><li><a th:href="@{/level1/1}">罗汉拳</a></li><li><a th:href="@{/level1/2}">武当长拳</a></li><li><a th:href="@{/level1/3}">全真剑法</a></li></ul>
</div><div sec:authorize="hasRole('VIP2')"><h3>高级武功秘籍</h3><ul><li><a th:href="@{/level2/1}">太极拳</a></li><li><a th:href="@{/level2/2}">七伤拳</a></li><li><a th:href="@{/level2/3}">梯云纵</a></li></ul>
</div><div sec:authorize="hasRole('VIP3')"><h3>绝世武功秘籍</h3><ul><li><a th:href="@{/level3/1}">葵花宝典</a></li><li><a th:href="@{/level3/2}">龟派气功</a></li><li><a th:href="@{/level3/3}">独孤九剑</a></li></ul>
</div></body>
</html>

MySecurityConfig类:

package com.qublog.springboot13_security.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;@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {//super.configure(http);//定制请求的授权规则http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/level1/**").hasRole("VIP1").antMatchers("/level2/**").hasRole("VIP2").antMatchers("/level3/**").hasRole("VIP3");//开启自动配置的登陆功能,如果没有权限就会来到登陆页面//1、/login请求来到登陆页//2、重定向到/login?error表示登陆失败//3、默认post形式的/login代表处理登陆//4、一旦定制loginpage,那么loginpage的post请求就是登陆http.formLogin().usernameParameter("user").passwordParameter("pwd").loginPage("/userlogin");//开启自动配置的注销功能//1、访问/logout表示用户注销,并清空session//2、注销成功会返回/login?logout//3、注销成功来到首页http.logout().logoutSuccessUrl("/");//开启记住我功能//登陆成功以后,将cookie发给浏览器保存,以后访问页面带上这个cookie,只要通过检查就可以免登录//点击注销会删除cookiehttp.rememberMe().rememberMeParameter("remeber");}//定义认证规则@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {//super.configure(auth);auth.inMemoryAuthentication().withUser("zhangsan").password("{noop}123456").roles("VIP1","VIP2").and().withUser("lisi").password("{noop}123456").roles("VIP2","VIP3").and().withUser("wangwu").password("{noop}123456").roles("VIP1","VIP3");}
}

KungfuController类:

package com.qublog.springboot13_security.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;@Controller
public class KungfuController {private final String PREFIX = "pages/";/*** 欢迎页* @return*/@GetMapping("/")public String index() {return "welcome";}/*** 登陆页* @return*/@GetMapping("/userlogin")public String loginPage() {return PREFIX+"login";}/*** level1页面映射* @param path* @return*/@GetMapping("/level1/{path}")public String level1(@PathVariable("path")String path) {return PREFIX+"level1/"+path;}/*** level2页面映射* @param path* @return*/@GetMapping("/level2/{path}")public String level2(@PathVariable("path")String path) {return PREFIX+"level2/"+path;}/*** level3页面映射* @param path* @return*/@GetMapping("/level3/{path}")public String level3(@PathVariable("path")String path) {return PREFIX+"level3/"+path;}}

Springboot13SecurityApplication类:

package com.qublog.springboot13_security;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Springboot13SecurityApplication {public static void main(String[] args) {SpringApplication.run(Springboot13SecurityApplication.class, args);}}

【安全】整合spring security相关推荐

  1. spring boot整合spring security笔记

    最近自己做了一个小项目,正在进行springboot和spring Security的整合,有一丢丢的感悟,在这里分享一下: 首先,spring boot整合spring security最好是使用T ...

  2. 八、springboot整合Spring Security

    springboot整合Spring Security 简介 Spring Security是一个功能强大且可高度自定义的身份验证和访问控制框架.它是保护基于Spring的应用程序的事实标准. Spr ...

  3. springboot2 war页面放在那_Spring Boot2 系列教程(三十三)整合 Spring Security

    Spring Security 是 Spring 家族中的一个安全管理框架,实际上,在 Spring Boot 出现之前,Spring Security 就已经发展了多年了,但是使用的并不多,安全管理 ...

  4. Spring boot 整合Spring Security Jwt

    记录学习Spring boot 整合Spring Security Jwt 学习参考 – 慢慢的干货 https://shimo.im/docs/OnZDwoxFFL8bnP1c/read 首先创建S ...

  5. 认证与授权流程与spring boot整合 spring security(1)

    一   spring security 1.1 spring security的作用 Spring Security所解决的问题就是安全访问控制,而安全访问控制功能其实就是对所有进入系统的请求进行拦截 ...

  6. 31 | SpringBoot安全之整合Spring Security

    一.安全 应用程序的两个主要区域是"认证"和"授权"(或者访问控制),这两个主要区域是安全的两个目标. 身份验证意味着确认您自己的身份,而授权意味着授予对系统的 ...

  7. springBoot整合spring security+JWT实现单点登录与权限管理前后端分离

    在前一篇文章当中,我们介绍了springBoot整合spring security单体应用版,在这篇文章当中,我将介绍springBoot整合spring secury+JWT实现单点登录与权限管理. ...

  8. springBoot整合spring security+JWT实现单点登录与权限管理前后端分离--筑基中期

    写在前面 在前一篇文章当中,我们介绍了springBoot整合spring security单体应用版,在这篇文章当中,我将介绍springBoot整合spring secury+JWT实现单点登录与 ...

  9. SpringBoot 整合 Spring Security 实现安全认证【SpringBoot系列9】

    SpringCloud 大型系列课程正在制作中,欢迎大家关注与提意见. 程序员每天的CV 与 板砖,也要知其所以然,本系列课程可以帮助初学者学习 SpringBooot 项目开发 与 SpringCl ...

  10. SpringBoot整合Spring Security【超详细教程】

    好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/Lee/DayDayUP,欢迎Star,更多文章请前往:目录导航 前言 Spring Security是一个 ...

最新文章

  1. 北邮OJ 1021. 16校赛-Stone Game
  2. 测试core :: demangled_name
  3. HDU4367(线段相交与斐波那契数列)
  4. 谈谈dpdk应用层包处理程序的多进程和多线程模型选择时的若干考虑
  5. 记录解决二次编码问题
  6. Union/find--不相交集类(并查集)
  7. 联想内部工程师 Vista自学手册
  8. android屏蔽按键精灵,安卓按键精灵的,关闭app程序问题!
  9. 【转】正则表达式–零宽断言详解
  10. 计算机论文要多少字,期刊论文一般多少字
  11. qtableview 鼠标划过单元格弹出标签显示单元格内容
  12. Windows 禁用U盘的程序,不用注册表方式。
  13. 最完整的PS快捷键大全(绝对经典)
  14. photoshop修色圣典 第5版pdf
  15. 面试 | 3.5 万字总结 Java 后台研发常见面试题
  16. 高中数学之向量外积的运用
  17. 骑士amp;魔法 java_程序员穿越异世界,骑士与魔法!
  18. 大小不一样的图片随着盒子大小自适应
  19. 相随与欢-彩色泡泡机的设计与实现
  20. “老赖”王思聪、罗永浩、贾跃亭挡住“发币”诱惑

热门文章

  1. 视频教程-大型ERP实战项目教程-Java
  2. 如何实现微信公众号加人数量实时统计?
  3. std list/vector sort 排序就这么简单
  4. C语言售货系统,自动售货机报告c语言课程设计.docx
  5. Nokia S60系统问题:正常接收手机短信、但无法接收ISP短信(10086、携程、招行)
  6. PHPBB3的用户密码
  7. Rosalind Java|Open Reading Frames
  8. java+代码实现+流星雨,js代码实现流星雨 - osc_zls6dx9i的个人空间 - OSCHINA - 中文开源技术交流社区...
  9. 解决小程序Failed to load localimage resource/pages/index/index/in.jpgtheserverrespondedwithastatusof500问题
  10. erp系统选型需满足哪些条件?一位erp选型专家分享经验