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,如下形式

-- ::52.852  INFO  --- [           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 { @Override
protected 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 { @Override
protected 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
*/
@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");
} }

直接关闭所有Security的所有拦截功能

http.headers().frameOptions().disable().and().authorizeRequests().antMatchers("/**", "/login*").permitAll(); // 所有用户都可以访问
http.csrf().disable().authorizeRequests().anyRequest().permitAll().and().logout().permitAll(); //禁用security的 csrf功能

SpringSecurity默认是禁止接收POST请求的,而GET是默认可以的,官网给出两个解决方案:1是发送请求时带上CSRF的token,2是不推荐的做法(把SpringSecurity的CSRF功能关掉),不然很有可能是get请求正常, 但post请求报403 forbidden

springboot security版本差异

帐号密码配置差异(亲测)

在security 4版本中(springboot1.5中使用), application.yml中简易帐号密码配置如下,没有spring:

security:
user:
name: bobo
password:

而在security 5版本中(springboot2开始使用), application.yml中帐号密码配置如下, 多了一层spring:

spring:
security:
user:
name: bobo
password:

禁用security

pom.xml中如果存在以下依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

启动时控制台会随机生成登录密码, 打印如下:

Using generated security password: -34f3-444c--7243038d0af9

请求接口时会跳到登录页 Please sign in

禁用spring security两种方法, 配置类或启动类上加上如下(二选一)

@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class}) 或者   @SpringBootApplication(exclude = {SecurityAutoConfiguration.class })

登录问题

Spring Security在登录验证中增加额外数据(如验证码)==>https://www.cnblogs.com/phoenix-smile/p/5666686.html

spring security前台登录如何传递多个参数要后台验证==>https://bbs.csdn.net/topics/390493958    (在username里用_分割啊哈哈哈)

$$$$$$$$$$spring security controller层实现登陆==>https://blog.csdn.net/qq_34675369/article/details/91499798

我的项目git地址

https://gitee.com/KingBoBo/springboot-05-security

最好的教程

在SpringBoot 中使用Security安全框架==>https://blog.csdn.net/qwe86314/article/details/89509765

相关系列 文章

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

springboot security 安全的更多相关文章

  1. Springboot security cas整合方案-实践篇

    承接前文Springboot security cas整合方案-原理篇,请在理解原理的情况下再查看实践篇 maven环境 <dependency> <groupId>org.s ...

  2. Springboot security cas源码陶冶-ExceptionTranslationFilter

    拦截关键的两个异常,对异常进行处理.主要应用异常则跳转至cas服务端登录页面 ExceptionTranslationFilter#doFilter-逻辑入口 具体操作逻辑如下 public void ...

  3. Springboot security cas源码陶冶-CasAuthenticationFilter

    Springboot security cas整合方案中不可或缺的校验Filter类或者称为认证Filter类,其内部包含校验器.权限获取等,特开辟新地啃啃 继承结构 - AbstractAuthen ...

  4. Springboot security cas整合方案-原理篇

    前言:网络中关于Spring security整合cas的方案有很多例,对于Springboot security整合cas方案则比较少,且有些仿制下来运行也有些错误,所以博主在此篇详细的分析cas原 ...

  5. SpringBoot security关闭验证

    SpringBoot security关闭验证 springboot2.x security关闭验证https://www.cnblogs.com/guanxiaohe/p/11738057.html ...

  6. springboot+security整合(3)自定义鉴权

    说明 springboot 版本 2.0.3源码地址:点击跳转 系列 springboot+security 整合(1) springboot+security 整合(2) springboot+se ...

  7. springboot+security整合(2)自定义校验

    说明 springboot 版本 2.0.3源码地址:点击跳转 系列 springboot+security 整合(1) springboot+security 整合(2) springboot+se ...

  8. springboot+security整合(1)

    说明 springboot 版本 2.0.3源码地址:点击跳转 系列 springboot+security 整合(1) springboot+security 整合(2) springboot+se ...

  9. SpringBoot + Security实现权限控制

    网上找了好几个,因为各种原因不太行,下面这个亲测可行 参考:https://blog.csdn.net/u012702547/article/details/54319508 基于SpringBoot ...

随机推荐

  1. ROS多线程编程

    int main(int argc, char **argv) { ros::init(argc, argv, "multi_sub"); multiThreadListener ...

  2. Mybatis框架+原理

    https://www.cnblogs.com/luoxn28/p/6417892.html(转载,蛮详细的)

  3. 【HTML5】如何处理HTML5新标签的浏览器兼容版问题

    HTML5规范毕竟是刚刚才定义完成的规范,还有一些浏览器并不能支持其中的新标签和新属性,尤其是IE8及以下版本浏览器.以下介绍一些在页面中使用HTML5新标签的实践方法,目的是让HTML5中的新标签在 ...

  4. android服务的bindService/startService

    1,高版本android已经不允许只通过action来bindService/startService,可以通过: intent.setPackage("XXXX"); 来指定服务 ...

  5. 关于Cesium 官方教程

    最近一直在准备第一次QQ群的Cesium基础培训公开课,虽说使用Cesium也有段日子了,但是要说对Cesium了解有多深,还真不一定.原因是一直以来我都是用哪里学哪里.基于多年开发三维数字地球的底层 ...

  6. openCV 矩阵(图像)操作函数

    有很多函数有mask,代表掩码,如果某位mask是0,那么对应的src的那一位就不计算,mask要和矩阵/ROI/的大小相等.大多数函数支持ROI,如果图像ROI被设置,那么只处理ROI部分 少部分函 ...

  7. Django项目: 项目环境搭建 ---- 一、创建django项目

    项目环境搭建 一.创建django项目 1.创建python虚拟环境 在虚拟机上创建python虚拟环境,因为实际项目部署,实在linux mkvirtualenv -p /usr/bin/pytho ...

  8. Hadoop IO 特性详解(2)【文件校验】

    (本文引用了microheart,ggjucheng的一些资料,在此感谢.charles觉得知识无价,开源共享无价) 这一次我们接着分析文件IO校验的相关代码,看看最底层是如何实现这种大数据集的文件校 ...

  9. TZOJ 5986 玄武密码(AC自动机)

    描述 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中.老人们说,这是玄武神灵将天书藏匿在此. 很多年后,人们终于在 ...

  10. Leetcode476.Number Complement数字的补数

    给定一个正整数,输出它的补数.补数是对该数的二进制表示取反. 注意: 给定的整数保证在32位带符号整数的范围内. 你可以假定二进制数不包含前导零位. 示例 1: 输入: 5 输出: 2 解释: 5的二 ...