springboot security 安全
spring security几个概念
“认证”(Authentication) 是建立一个他声明的主体的过程(一个“主体”一般是指用户,设备或一些可以在你的应用程序中执行动作的其他系统) 。
“授权”(Authorization)指确定一个主体是否允许在你的应用程序执行一个动作的过程。为了抵达需要授权的店,主体的身份已经有认证过程建立。
这个概念是通用的, 而不仅仅在Spring Security中 , 在Shiro中也是一样的.
Spring Security的 Web&安全几个关键点
1. 登陆/注销
HttpSecurity配置登陆、注销功能
2. Thymeleaf提供的SpringSecurity标签支持
需要引入thymeleaf-extras-springsecurity4
sec:authentication=“name” 获得当前用户的用户名
sec:authorize=“hasRole(‘ADMIN’)” 当前用户必须拥有ADMIN权限时才会显示标签内容
3. remember me
表单添加remember-me的checkbox
配置启用remember-me功能
4. CSRF(Cross-site request forgery)跨站请求伪造
HttpSecurity启用csrf功能,会为表单添加_csrf的值,提交携带来预防CSRF;
我们仅需引入spring-boot-starter-security模块,进行少量的配置,即可实现强大的安全管理。
security中几个重要的类如下:
WebSecurityConfigurerAdapter:自定义Security策略 AuthenticationManagerBuilder:自定义认证策略 @EnableWebSecurity:开启WebSecurity模式 (在@Controller注解的类上追加)
SpringSecurity在SpringBoot中使用
springsecurity在什么都不配的情况下,默认帐号是user, 密码在启动日志中随机生成uuid,如下形式
2019-06-04 09:44:52.852 INFO 33512 --- [ main] b.a.s.AuthenticationManagerConfiguration : Using default security password: bc4c813c-b8f9-4cdb-9ca9-b406d3492da9
pom.xml添加依赖
<!--在thymeleaf中使用认证标签需要的额外依赖--><dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-springsecurity4</artifactId><version>3.0.2.RELEASE</version></dependency>
配置MySecurityConfig.java
官方说明自定义SecurityConfig替换默认配置参考链接: https://docs.spring.io/spring-security/site/docs/5.2.0.BUILD-SNAPSHOT/reference/htmlsingle/#oauth2resourceserver-sansboot
protected void configure(HttpSecurity http) {http.authorizeRequests().anyRequest().authenticated().and().oauth2ResourceServer().jwt(); }
自定义MySecurityConfig.java
@EnableWebSecurity //该注解本身就包含@Configuration 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");}}
配置自动生成的登录页
开启自动配置的 /login 登录页面,如果不配置, 那么无授权时会报403 Access is denied错误,且页面也不知道跳哪,因为还没有开启自动配置的login登录页面
,默认使用的是/login 和 /login?error, 现在改成/userlogin,会经过KungfuController的 @GetMapping("/userlogin")注解的方法.
也可以配置登录页及登录时的帐号密码字段是什么等.
http.formLogin().loginPage("/login").usernameParameter("username").passwordParameter("password");
配置登出界面
开启自动配置的注销功能,默认访问/logout表示注销,会清空session及cookie,注销成功后返回/login?logout页面
http.logout().logoutSuccessUrl("/");//自定义注销成功后跳转到/页面
配置认证规则
/*** 定义认证规则,管理帐号/密码** @param auth* @throws Exception*/ @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception {//auth.jdbcAuthentication(); //一般用jdbc//学习用内存认证 auth.inMemoryAuthentication().withUser("bobo").password("123456").roles("VIP1").and().withUser("sisi").password("123456").roles("VIP1", "VIP2").and().withUser("xixi").password("123456").roles("VIP1", "VIP2", "VIP3"); }
在pom.xml中引入thymeleaf依赖的springSecurity标签的插件
<!--在thymeleaf中使用认证标签需要的额外依赖--> <dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-springsecurity4</artifactId><version>3.0.2.RELEASE</version> </dependency>
html标签体中引入security规范
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
thymeleaf中显示用户信息
<div sec:authorize="isAuthenticated()"><h2> 您好 , <span sec:authentication="name"></span> 您的角色是 <span sec:authentication="principal.authorities"></span></h2> </div>
thymeleaf中显示判断是否有权限
<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>
开启记住我功能
开启记住我功能登录成功,会从服务器返回添加名为remember-me的cookie指令, 以后访问页面都会带上该cookie, 只要服务器通过检查就可以免登录了,默认14天后失效
http.rememberMe().rememberMeParameter("remember-me");
此时使用/logout会一并清除名为remember-me的cookie , 因为/logout请求在header头中携带了Max-Age=0参数
自定义登录页,使用/userlogin
//默认使用的是/login 和 /login?error, 现在改成/userlogin,会经过KungfuController的 @GetMapping("/userlogin")注解的方法http.formLogin().loginPage("/userlogin");//.usernameParameter("username").passwordParameter("password");
默认get形式的/login来到登录页, post形式的/login用来表单登录.
当自定义了登录页面/userlogin后,那么get形式的/userlogin来到登录页, post形式的/userlogin用来表单登录. 见源码注释说明如下:
最终项目结构
核心配置类MySecurityConfig.java内容
package com.example.security.config;import org.springframework.context.annotation.Configuration; 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.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;@EnableWebSecurity //该注解本身就包含@Configuration 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");//参见 HttpSecurity 类的 public FormLoginConfigurer<HttpSecurity> formLogin() 方法注解//开启自动配置的 /login 登录页面,如果不配置, 那么无授权时会报403 Access is denied错误,且页面也不知道跳哪,因为还没有开启自动配置的login登录页面//默认使用的是/login 和 /login?error, 现在改成/userlogin,会经过KungfuController的 @GetMapping("/userlogin")注解的方法http.formLogin().loginPage("/userlogin");//.usernameParameter("username").passwordParameter("password");//开启自动配置的注销功能,默认访问/logout表示注销,会清空session及cookie,注销成功后返回/login?logout页面http.logout().logoutSuccessUrl("/");//自定义注销成功后跳转到/页面//开启记住我功能登录成功,会从服务器返回添加名为remember-me的cookie指令, 以后访问页面都会带上该cookie, 只要服务器通过检查就可以免登录了,默认14天后失效http.rememberMe().rememberMeParameter("remember-me");}/*** 定义认证规则,管理帐号/密码** @param auth* @throws Exception*/@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {//auth.jdbcAuthentication(); //一般用jdbc//学习用内存认证 auth.inMemoryAuthentication().withUser("bobo").password("123456").roles("VIP1").and().withUser("sisi").password("123456").roles("VIP1", "VIP2").and().withUser("xixi").password("123456").roles("VIP1", "VIP2", "VIP3");}}
直接关闭所有Security的所有拦截功能
http.headers().frameOptions().disable().and().authorizeRequests().antMatchers("/**", "/login*").permitAll()// 所有用户都可以访问
springboot security版本差异
帐号密码配置差异(亲测)
在security 4版本中(springboot1.5中使用), application.yml中简易帐号密码配置如下,没有spring:
security:user:name: bobopassword: 123456
而在security 5版本中(springboot2开始使用), application.yml中帐号密码配置如下, 多了一层spring:
spring:security:user:name: bobopassword: 123456
我的项目git地址
https://gitee.com/KingBoBo/springboot-05-security
相关系列 文章
SpringBoot 整合 oauth2(三)实现 token 认证==>https://www.jianshu.com/p/19059060036b
SpringBoot 整合 Security(一)实现用户认证并判断返回json还是view==>https://www.jianshu.com/p/18875c2995f1
SpringBoot 整合 Security(二)实现验证码登录==>https://www.jianshu.com/p/9d08c767b33e
转载于:https://www.cnblogs.com/whatlonelytear/p/10894040.html
springboot security 安全相关推荐
- springboot+security整合(1)
说明 springboot 版本 2.0.3 源码地址:点击跳转 系列 springboot+security 整合(1) springboot+security 整合(2) springboot+s ...
- springboot+security框架整合
springboot+security框架整合 springboot项目搭建大家可以去请教度娘,有很多文章,这里主要讲解springboot和security安全框架的集成,因为springmvc跟s ...
- springboot+security实例
springboot+security pom文件 <?xml version="1.0" encoding="UTF-8"?> <proje ...
- 视频教程-SpringBoot+Security+Vue前后端分离开发权限管理系统-Java
SpringBoot+Security+Vue前后端分离开发权限管理系统 10多年互联网一线实战经验,现就职于大型知名互联网企业,架构师, 有丰富实战经验和企业面试经验:曾就职于某上市培训机构数年,独 ...
- SpringBoot Security 自定义登录验证逻辑+密码加盐
密码加盐思路 JAVA 加盐加密方法_Teln_小凯的博客-CSDN博客 盐加密方法 @ApiOperation(value = "002-加密")@PreAuthorize(&q ...
- springboot +security +mybatis+thymeleaf 实现简单的用户 角色 权限(资源) 管理
1.用户 角色 资源的关系 2.实现思路 3.参考资料 Spring Boot Security +Redis 实现简单权限控制 将返回结果变成json 响应改客户端 在第六项 4.实现代码 ...
- springboot security 权限校验_springboot借助aop和注解实现权限校验
我们用springboot做后台开发,难免会用到权限校验,比如查看当前用户是否合法,是否是管理员.而spring的面向切面的特效可以帮助我们很好的实现动态的权限校验.这里我们就用到的spring的ao ...
- springboot security 权限不足_SpringBoot 整合 SpringSecurity 之起源篇(零)
本篇为SpringSecurity的第一篇,主要来介绍下什么是SpringSecurity,以及在springboot中如何使用它 I. 基本知识点 官方文档: https://docs.spring ...
- springboot security 权限校验_十二、SpringBoot 优雅的集成Spring Security
前言 至于什么是Spring security ,主要两个作用,用户认证和授权.即我们常说的,用户只有登录了才能进行其他操作,没有登录的话就重定向到登录界面.有的用户有权限执行某一操作,而有的用户不能 ...
- springBoot+security+mybatis 实现用户权限的数据库动态管理
[b][size=large]一.Spring Security 应用的概述[/size][/b] [size=medium] 鉴于目前微服务的兴起,Spring周边方案的普及,以及 Spring S ...
最新文章
- python环境下,执行系统命令方法
- 测试功能范围_IT8511+电子负载 OCP?测试功能
- linux i2c子系统看不懂啊,Linux 下的I2C子系统
- flex vue 垂直居中居上_移动开发-flex 布局
- boost::dynamic_property_map相关的测试程序
- 2018 KDD CUP支付宝安全团队Deep X斩获两项大奖
- ThinkPHP3.2.3从php5升级到php7艰辛之路
- 使用matlab生成数独(无回溯法)
- Unity开发教程 打造战棋手游《天地劫》
- 教你分割视频,用多个视频随机合并,添加音频
- html div旋转45度,CSS动画 - 在每次悬停时进一步旋转45度
- 1.7亿条数据,比胡同和撸串更真实的北京
- C# XNA 简单的 迷宫游戏
- 压力测试软件 loadr,初学abench压力测试 - 玄大冰 - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...
- Internet时间自动同步后,计算机系统时间比北京时间不能同步一致
- 大数据平台架构的层次划分
- RFID射频技术基本原理与射频技术中的基本单位
- 安卓通讯之《蓝牙单片机通讯助手》②扫描设备、连接设备和双向通讯。
- 我的素描基础练习第一周~欢迎大家多提意见哈
- Eclipse改字体大小
热门文章
- 小心投机分子绿坝软件的苦肉计
- springcloud与jdk版本问题
- python数据导出excel_python实现数据导出到excel的示例
- go 中 = 与:= 区别
- Centos中yum方式安装java
- k8s学习笔记-环境搭建篇
- 一个html可以有几个h1,一个页面可以有多个H1标签吗?
- python语言程序设计实践教程陈东_《Python程序设计》(陈春晖)【摘要 书评 试读】- 京东图书...
- io 流 txt本地生成
- 【渝粤教育】国家开放大学2019年春季 1250文论专题 参考试题