【Spring Boot】011
【Spring Boot】011
最新更新:2020年9月22日08:16:4
Spring Security 是 Spring 家族中的一个安全管理框架,实际上,在 Spring Boot 出现之前,Spring Security 就已经发展了多年了,但是使用的并不多,安全管理这个领域,一直是 Shiro 的天下。
相对于 Shiro,在 SSM/SSH 中整合 Spring Security 都是比较麻烦的操作,所以,Spring Security 虽然功能比 Shiro 强大,但是使用反而没有 Shiro 多(Shiro 虽然功能没有 Spring Security 多,但是对于大部分项目而言,Shiro 也够用了)。
自从有了 Spring Boot 之后,Spring Boot 对于 Spring Security 提供了 自动化配置方案,可以零配置使用 Spring Security。
因此,一般来说,常见的安全管理技术栈的组合是这样的:
- SSM + Shiro
- Spring Boot/Spring Cloud + Spring Security
注意,这只是一个推荐的组合而已,如果单纯从技术上来说,无论怎么组合,都是可以运行的。
1、创建项目
第一步:配置项目相关信息
第二步:勾选web模块,创建即可
2、引入相关坐标
thymeleaf模板引擎:
代码语言:javascript代码运行次数:0运行复制 <!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
、导入静态资源
资源下载地址:
链接: 提取码:zibo
4、设置配置文件application.properties
代码语言:javascript代码运行次数:0运行复制=false
5、编写RouterController类
代码语言:javascript代码运行次数:0运行复制package com.;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RouterController {
@RequestMapping({"/","index","index.html"})
public String index(){
return "index";
}
@RequestMapping("/toLogin")
public String toLogin(){
return "views/login";
}
@RequestMapping("/level1/{id}")
public String level1(@PathVariable("id") int id){
return "views/level1/" + id;
}
@RequestMapping("/level2/{id}")
public String level2(@PathVariable("id") int id){
return "views/level2/" + id;
}
@RequestMapping("/level/{id}")
public String level(@PathVariable("id") int id){
return "views/level/" + id;
}
}
6、运行测试
http://localhost:8080/
1、用户授权
第一步:导入security依赖坐标
代码语言:javascript代码运行次数:0运行复制 <!--security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
第二步:创建SecurityConfig类
代码语言:javascript代码运行次数:0运行复制package com.;
import org.springframework.annotation.web.builders.HttpSecurity;
import org.springframework.annotation.EnableWebSecurity;
import org.springframework.annotation.WebSecurityConfigurerAdapter;
//AOP:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,里面的功能页只能有权限的人才能访问
//链式编程
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level/**").hasRole("vip");
}
}
第三步:运行测试
首页可以正常访问:
其他页面无法访问:
第四步:设置若没有权限跳转到登陆页面
修改Config类:
代码语言:javascript代码运行次数:0运行复制package com.;
import org.springframework.annotation.web.builders.HttpSecurity;
import org.springframework.annotation.EnableWebSecurity;
import org.springframework.annotation.WebSecurityConfigurerAdapter;
//AOP:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,里面的功能页只能有权限的人才能访问
//链式编程
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level/**").hasRole("vip");
//没有权限,默认调到登陆页面
http.formLogin();
}
}
测试结果:
源代码注释:
2、用户认证
第一步:修改Config类:
代码语言:javascript代码运行次数:0运行复制package com.;
import org.springframework.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.annotation.web.builders.HttpSecurity;
import org.springframework.annotation.EnableWebSecurity;
import org.springframework.annotation.WebSecurityConfigurerAdapter;
//AOP:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,里面的功能页只能有权限的人才能访问
//链式编程
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level/**").hasRole("vip");
//没有权限,默认调到登陆页面
http.formLogin();
}
//认证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//这些数据正常情况下应该从数据库中读取
auth.inMemoryAuthentication()
.withUser("zibo").password("12").roles("vip2","vip")
.and()
.withUser("zb").password("12").roles("vip1","vip");
}
}
第二步:测试结果:
报错,因为密码未加密!
第三步:密码加密:
代码语言:javascript代码运行次数:0运行复制package com.;
import org.springframework.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.annotation.web.builders.HttpSecurity;
import org.springframework.annotation.EnableWebSecurity;
import org.springframework.annotation.WebSecurityConfigurerAdapter;
import org.springframework.bcrypt.BCryptPasswordEncoder;
//AOP:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,里面的功能页只能有权限的人才能访问
//链式编程
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level/**").hasRole("vip");
//没有权限,默认调到登陆页面
http.formLogin();
}
//认证
//PasswordEncoder 密码编码(加密)
//在Spring Security5.0+中,新增了很多加密方法
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//这些数据正常情况下应该从数据库中读取
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("zibo").password(new BCryptPasswordEncoder().encode("12")).roles("vip2","vip")
.and()
.withUser("zb").password(new BCryptPasswordEncoder().encode("12")).roles("vip1","vip");
}
}
第四步:测试结果:(成功登陆)
第五步:可正常访问相关权限内页面:
1、注销
第一步:修改Config类
代码语言:javascript代码运行次数:0运行复制package com.;
import org.springframework.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.annotation.web.builders.HttpSecurity;
import org.springframework.annotation.EnableWebSecurity;
import org.springframework.annotation.WebSecurityConfigurerAdapter;
import org.springframework.bcrypt.BCryptPasswordEncoder;
//AOP:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,里面的功能页只能有权限的人才能访问
//链式编程
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level/**").hasRole("vip");
//没有权限,默认调到登陆页面
http.formLogin();
//★★★注销
http.logout();
}
//认证
//PasswordEncoder 密码编码(加密)
//在Spring Security5.0+中,新增了很多加密方法
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//这些数据正常情况下应该从数据库中读取
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("zibo").password(new BCryptPasswordEncoder().encode("12")).roles("vip2","vip")
.and()
.withUser("zb").password(new BCryptPasswordEncoder().encode("12")).roles("vip1","vip");
}
}
第二步:修改index.html页面
代码语言:javascript代码运行次数:0运行复制<!DOCTYPE html>
<html lang="en" xmlns:th=";>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>首页</title>
<!--semantic-ui-->
<link href=".4.1/" rel="stylesheet">
<link th:href="@{/qinjiang/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">
<!--未登录-->
<a class="item" th:href="@{/toLogin}">
<i class="address card icon"></i> 登录
</a>
<a class="item" th:href="@{/logout}">
<i class="address card icon"></i> 注销
</a>
<!--已登录
<a th:href="@{/usr/toUserCenter}">
<i class="address card icon"></i> admin
</a>
-->
</div>
</div>
</div>
<div class="ui segment" style="text-align: center">
<h>Spring Security Study by 秦疆</h>
</div>
<div>
<br>
<div class="ui three column stackable grid">
<div class="column">
<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/}"><i class="bullhorn icon"></i> Level-1-</a></div>
</div>
</div>
</div>
</div>
<div class="column">
<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/}"><i class="bullhorn icon"></i> Level-2-</a></div>
</div>
</div>
</div>
</div>
<div class="column">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level </h5>
<hr>
<div><a th:href="@{/level/1}"><i class="bullhorn icon"></i> Level--1</a></div>
<div><a th:href="@{/level/2}"><i class="bullhorn icon"></i> Level--2</a></div>
<div><a th:href="@{/level/}"><i class="bullhorn icon"></i> Level--</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script th:src="@{/qinjiang/js/jquery-.1.js}"></script>
<script th:src="@{/qinjiang/js/js}"></script>
</body>
</html>
第三步:测试结果
第四步:修改代码,使其跳转到首页
代码语言:javascript代码运行次数:0运行复制package com.;
import org.springframework.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.annotation.web.builders.HttpSecurity;
import org.springframework.annotation.EnableWebSecurity;
import org.springframework.annotation.WebSecurityConfigurerAdapter;
import org.springframework.bcrypt.BCryptPasswordEncoder;
//AOP:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,里面的功能页只能有权限的人才能访问
//链式编程
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level/**").hasRole("vip");
//没有权限,默认调到登陆页面
http.formLogin();
//★★★注销
http.logout().logoutSuccessUrl("/");
}
//认证
//PasswordEncoder 密码编码(加密)
//在Spring Security5.0+中,新增了很多加密方法
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//这些数据正常情况下应该从数据库中读取
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("zibo").password(new BCryptPasswordEncoder().encode("12")).roles("vip2","vip")
.and()
.withUser("zb").password(new BCryptPasswordEncoder().encode("12")).roles("vip1","vip");
}
}
第五步:测试结果
2、权限控制
第一步:导入坐标
代码语言:javascript代码运行次数:0运行复制<!-- ./thymeleaf-extras-springsecurity4 -->
<dependency>
<groupId></groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>.0.4.RELEASE</version>
</dependency>
第二步:修改index.html页面
代码语言:javascript代码运行次数:0运行复制<!DOCTYPE html>
<html lang="en" xmlns:th=";
xmlns:sec=";>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>首页</title>
<!--semantic-ui-->
<link href=".4.1/" rel="stylesheet">
<link th:href="@{/qinjiang/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.getAuthorities()"></span>
</a>
<a class="item" th:href="@{/logout}">
<i class="address card icon"></i> 注销
</a>
</div>
<!--已登录:显示用户名和注销按钮-->
<!-- <a th:href="@{/usr/toUserCenter}">-->
<!-- <i class="address card icon"></i> admin-->
<!-- </a>-->
</div>
</div>
</div>
<div class="ui segment" style="text-align: center">
<h>Spring Security Study by 秦疆</h>
</div>
<div>
<br>
<div class="ui three column stackable grid">
<div class="column">
<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/}"><i class="bullhorn icon"></i> Level-1-</a></div>
</div>
</div>
</div>
</div>
<div class="column">
<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/}"><i class="bullhorn icon"></i> Level-2-</a></div>
</div>
</div>
</div>
</div>
<div class="column">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level </h5>
<hr>
<div><a th:href="@{/level/1}"><i class="bullhorn icon"></i> Level--1</a></div>
<div><a th:href="@{/level/2}"><i class="bullhorn icon"></i> Level--2</a></div>
<div><a th:href="@{/level/}"><i class="bullhorn icon"></i> Level--</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script th:src="@{/qinjiang/js/jquery-.1.js}"></script>
<script th:src="@{/qinjiang/js/js}"></script>
</body>
</html>
第三步:修改spring boot版本为2.0.7,因为2..springboot 2.1.x版本以上不兼容部分标签,如
代码语言:javascript代码运行次数:0运行复制sec:authorize="isAuthenticated()" 和 sec:authorize="isAnonymous()"
第四步:运行测试
备注:
版本兼容问题是一大坨问题,修改版本之后以前的写法可能呈现的结果又不同,具体使用的时候需要细细研究;
修改SecurityConfig类:
代码语言:javascript代码运行次数:0运行复制package com.;
import org.springframework.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.annotation.web.builders.HttpSecurity;
import org.springframework.annotation.EnableWebSecurity;
import org.springframework.annotation.WebSecurityConfigurerAdapter;
import org.springframework.bcrypt.BCryptPasswordEncoder;
//AOP:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,里面的功能页只能有权限的人才能访问
//链式编程
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level/**").hasRole("vip");
//没有权限,默认调到登陆页面
http.formLogin();
//注销
http.logout().logoutSuccessUrl("/");
//★★★记住我
http.rememberMe();
}
//认证
//PasswordEncoder 密码编码(加密)
//在Spring Security5.0+中,新增了很多加密方法
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//这些数据正常情况下应该从数据库中读取
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("zibo").password(new BCryptPasswordEncoder().encode("12")).roles("vip2","vip")
.and()
.withUser("zb").password(new BCryptPasswordEncoder().encode("12")).roles("vip1","vip");
}
}
1、说明
Spring Security目前所学习内容,尚不够用,用时应根据需要细细研究;
2、注意最佳实践结合:
- SSM + Shiro
- Spring Boot/Spring Cloud + Spring Security
链接: 提取码:zibo
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2025-01-06,如有侵权请联系 cloudcommunity@tencent 删除加密权限spring安全boot#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上一篇:【Spring Boot】012
下一篇:【Spring Boot】010
推荐阅读
留言与评论(共有 15 条评论) |
本站网友 重庆职业培训 | 30分钟前 发表 |
4 一 | |
本站网友 什么是假声 | 13分钟前 发表 |
fragment="nav-menu"> <div class="ui secondary menu"> <a class="item" th | |
本站网友 国泰君安下载 | 3分钟前 发表 |
"index.html"}) public String index(){ return "index"; } @RequestMapping("/toLogin") public String toLogin(){ return "views/login"; } @RequestMapping("/level1/{id}") public String level1(@PathVariable("id") int id){ return "views/level1/" + id; } @RequestMapping("/level2/{id}") public String level2(@PathVariable("id") int id){ return "views/level2/" + id; } @RequestMapping("/level/{id}") public String level(@PathVariable("id") int id){ return "views/level/" + id; } }6 | |
本站网友 赣州房产 | 5分钟前 发表 |
里面的功能页只能有权限的人才能访问 //链式编程 http.authorizeRequests() .antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level/**").hasRole("vip"); } }第三步:运行测试首页可以正常访问:其他页面无法访问:第四步:设置若没有权限跳转到登陆页面修改Config类:代码语言:javascript代码运行次数:0运行复制package com.; import org.springframework.annotation.web.builders.HttpSecurity; import org.springframework.annotation.EnableWebSecurity; import org.springframework.annotation.WebSecurityConfigurerAdapter; //AOP: @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { //首页所有人可以访问 | |
本站网友 aibao | 16分钟前 发表 |
新增了很多加密方法 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //这些数据正常情况下应该从数据库中读取 auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser("zibo").password(new BCryptPasswordEncoder().encode("12")).roles("vip2" | |
本站网友 甲苯胺蓝 | 27分钟前 发表 |
引入相关坐标thymeleaf模板引擎:代码语言:javascript代码运行次数:0运行复制 <!--thymeleaf--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId></groupId> <artifactId>thymeleaf-extras-java8time</artifactId> </dependency> | |
本站网友 邱县新闻 | 28分钟前 发表 |
常见的安全管理技术栈的组合是这样的:SSM + ShiroSpring Boot/Spring Cloud + Spring Security注意 | |
本站网友 柠檬汁的功效 | 9分钟前 发表 |
href="@{/logout}"> <i class="address card icon"></i> 注销 </a> </div> <!--已登录:显示用户名和注销按钮--> <!-- <a th | |
本站网友 金地未未来房价 | 17分钟前 发表 |
里面的功能页只能有权限的人才能访问 //链式编程 http.authorizeRequests() .antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level/**").hasRole("vip"); //没有权限 | |
本站网友 宁波少儿培训 | 7分钟前 发表 |
//localhost | |
本站网友 董卿老公密春雷 | 23分钟前 发表 |
这只是一个推荐的组合而已 | |
本站网友 胀气 | 20分钟前 发表 |
8080/三 | |
本站网友 绿意盎然的意思 | 8分钟前 发表 |
href="@{/level/1}"><i class="bullhorn icon"></i> Level--1</a></div> <div><a th | |
本站网友 xcode9 | 3分钟前 发表 |
新增了很多加密方法 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //这些数据正常情况下应该从数据库中读取 auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser("zibo").password(new BCryptPasswordEncoder().encode("12")).roles("vip2" |