SpringSecurity

安全简介

1、在 Web 开发中,安全一直是非常重要的一个方面。安全虽然属于应用的非功能性需求,但是应该在应用开发的初期就考虑进来。如果在应用开发的后期才考虑安全的问题,就可能陷入一个两难的境地:一方面,应用存在严重的安全漏洞,无法满足用户的要求,并可能造成用户的隐私数据被攻击者窃取;另一方面,应用的基本架构已经确定,要修复安全漏洞,可能需要对系统的架构做出比较重大的调整,因而需要更多的开发时间,影响应用的发布进程。因此,从应用开发的第一天就应该把安全相关的因素考虑进来,并在整个应用的开发过程中。

2、市面上存在比较有名的:Shiro,Spring Security !

3、这里需要阐述一下的是,每一个框架的出现都是为了解决某一问题而产生了,那么Spring Security框架的出现是为了解决什么问题呢?

4、首先我们看下它的官网介绍:Spring Security官网地址

Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications.

Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements

5、Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架。它实际上是保护基于spring的应用程序的标准。

6、Spring Security是一个框架,侧重于为Java应用程序提供身份验证和授权。与所有Spring项目一样,Spring安全性的真正强大之处在于它可以轻松地扩展以满足定制需求

7、从官网的介绍中可以知道这是一个权限框架。想我们之前做项目是没有使用框架是怎么控制权限的?对于权限 一般会细分为功能权限,访问权限,和菜单权限。代码会写的非常的繁琐,冗余。

8、怎么解决之前写权限代码繁琐,冗余的问题,一些主流框架就应运而生而Spring Scecurity就是其中的一种。

9、Spring 是一个非常流行和成功的 Java 应用开发框架。Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案。一般来说,Web 应用的安全性包括用户认证(Authentication)和用户授权(Authorization)两个部分。

  • 用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统。用户认证一般要求用户提供用户名和密码。系统通过校验用户名和密码来完成认证过程。
  • 用户授权指的是验证某个用户是否有权限执行某个操作。在一个系统中,不同用户所具有的权限是不同的。比如对一个文件来说,有的用户只能进行读取,而有的用户可以进行修改。一般来说,系统会为不同的用户分配不同的角色,而每个角色则对应一系列的权限。

10、对于上面提到的两种应用情景,Spring Security 框架都有很好的支持。

  • 在用户认证方面,Spring Security 框架支持主流的认证方式,包括 HTTP 基本认证、HTTP 表单验证、HTTP 摘要认证、OpenID 和 LDAP 等。
  • 在用户授权方面,Spring Security 提供了基于角色的访问控制和访问控制列表(Access Control List,ACL),可以对应用中的领域对象进行细粒度的控制。

实战测试

实验环境搭建

  1. 新建一个初始的springboot项目web模块,thymeleaf模块

  2. 导入静态资源

  3. controller跳转!

    package nuc.ss.controller;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"})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("/level3/{id}")public String level3(@PathVariable("id") int id) {return "views/level3/" + id;}
    }
    
  4. 测试实验环境是否OK!

    首页

    登录

    详情

认识SpringSecurity

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

记住几个类:

  • WebSecurityConfigurerAdapter:自定义Security策略
  • AuthenticationManagerBuilder:自定义认证策略
  • @EnableWebSecurity:开启WebSecurity模式

Spring Security的两个主要目标是 “认证” 和 “授权”(访问控制)。

“认证”(Authentication)

身份验证是关于验证您的凭据,如用户名/用户ID和密码,以验证您的身份。

身份验证通常通过用户名和密码完成,有时与身份验证因素结合使用。

“授权” (Authorization)

授权发生在系统成功验证您的身份后,最终会授予您访问资源(如信息,文件,数据库,资金,位置,几乎任何内容)的完全权限。

这个概念是通用的,而不是只在Spring Security 中存在。

认证和授权

目前,我们的测试环境,是谁都可以访问的,我们使用 Spring Security 增加上认证和授权的功能

  1. 引入 Spring Security 模块

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

  2. 编写 Spring Security 配置类

    • 参考官网:https://spring.io/projects/spring-security

    • 查看我们自己项目中的版本,找到对应的帮助文档:https://docs.spring.io/spring-security/site/docs/5.3.0.RELEASE/reference/html5

    • servlet-applications 8.16.4

      @EnableWebSecurity
      public class Config extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.apply(customDsl()).flag(true).and()...;}
      }
      

  3. 编写基础配置类

    package nuc.ss.config;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// 开启WebSecurity模式
    public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {super.configure(http);}
    }
  4. 定制请求的授权规则

    看源码

    仿写

    //链式编程
    @Override
    protected void configure(HttpSecurity http) throws Exception {// 首页所有人都可以访问,功能也只有对应有权限的人才能访问到// 请求授权的规则http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/level1/**").hasRole("vip1").antMatchers("/level2/**").hasRole("vip2").antMatchers("/level3/**").hasRole("vip3");}
    
  5. 测试一下:发现除了首页都进不去了!因为我们目前没有登录的角色,因为请求需要登录的角色拥有对应的权限才可以!

  6. 在configure()方法中加入以下配置,开启自动配置的登录功能!

    // 开启自动配置的登录功能
    // /login 请求来到登录页
    // /login?error 重定向到这里表示登录失败
    http.formLogin();
    
  7. 测试一下:发现,没有权限的时候,会跳转到登录的页面!

  8. 查看刚才登录页的注释信息;

    我们可以定义认证规则,重写configure的另一个方法

    源码:

    仿写

    // 认证,springboot 2.1.x 可以直接使用
    // 密码编码: PasswordEncoder
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {//这些数据正常应该中数据库中读auth.inMemoryAuthentication().withUser("kuangshen").password("123456").roles("vip2","vip3").and().withUser("root").password("123456").roles("vip1","vip2","vip3").and().withUser("guest").password("123456").roles("vip1");
    }
    
  9. 测试,我们可以使用这些账号登录进行测试!发现会报错!

    There is no PasswordEncoder mapped for the id “null”

  10. 原因,我们要将前端传过来的密码进行某种方式加密,否则就无法登录,修改代码

    // 认证,springboot 2.1.x 可以直接使用
    // 密码编码: PasswordEncoder
    // 在spring Secutiry 5.0+ 新增了很多加密方法
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {//这些数据正常应该中数据库中读auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("kuangshen").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");
    }
    
  11. 测试,发现,登录成功,并且每个角色只能访问自己认证下的规则!搞定

权限控制和注销

  1. 开启自动配置的注销的功能

    //定制请求的授权规则
    @Override
    protected void configure(HttpSecurity http) throws Exception {//....//开启自动配置的注销的功能// /logout 注销请求http.logout();
    }
    
  2. 我们在前端,增加一个注销的按钮,index.html导航栏中

    <!--注销-->
    <a class="item" th:href="@{/logout}"><i class="sign-out icon"></i> 注销
    </a>
    
  3. 我们可以去测试一下,登录成功后点击注销,发现注销完毕会跳转到登录页面!

  4. 但是,我们想让他注销成功后,依旧可以跳转到首页,该怎么处理呢?

    源码:

    // .logoutSuccessUrl("/"); 注销成功来到首页
    http.logout().logoutSuccessUrl("/");
    
  5. 测试,注销完毕后,发现跳转到首页OK

  6. 我们现在又来一个需求:用户没有登录的时候,导航栏上只显示登录按钮,用户登录之后,导航栏可以显示登录的用户信息及注销按钮!还有就是,比如kuangshen这个用户,它只有 vip2,vip3功能,那么登录则只显示这两个功能,而vip1的功能菜单不显示!这个就是真实的网站情况了!该如何做呢?

    我们需要结合thymeleaf中的一些功能

    sec:authorize="isAuthenticated()":是否认证登录!来显示不同的页面

    Maven依赖:

    <!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
    <dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-springsecurity5</artifactId><version>3.0.4.RELEASE</version>
    </dependency>
    
    • 整合包4(springsecurity4)——springboot版本2.0.9
    • 整合包5(springsecurity5)——springboot版本之后
  7. 修改我们的前端页面

    导入命名空间

    <html lang="en" xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
    

    修改导航栏,增加认证判断

    <!--登录注销-->
    <div class="right menu"><!--如果未登录--><div sec:authorize="!isAuthenticated()"><a class="item" th:href="@{/login}"><i class="address card icon"></i> 登录</a></div><!--如果已登录--><div sec:authorize="isAuthenticated()"><a class="item"><i class="address card icon"></i>用户名:<span sec:authentication="principal.username"></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>
    
  8. 重启测试,我们可以登录试试看,登录成功后确实,显示了我们想要的页面;

    • 未登录

    • 登录

  9. 点击注销产生的问题

    • 整合包4(springsecurity4)

    • 整合包5(springsecurity5)(不算问题,需要点击确定,才能回到首页)

    解决问题:

    • 它默认防止csrf跨站请求伪造,因为会产生安全问题
    • 将请求改为post表单提交
    • 在spring security中关闭csrf功能http.csrf().disable();

    再次点击注销按钮之后(直接退出到首页)

  10. 我们继续将下面的角色功能块认证完成!

    <!--菜单根据用户的角色动态的实现-->
    <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>
    
  11. 测试一下!

  • 用户首页未登录

  • 某个用户登录

  • 权限控制和注销搞定!

记住我

现在的情况,我们只要登录之后,关闭浏览器,再登录,就会让我们重新登录,但是很多网站的情况,就是有一个记住密码的功能,这个该如何实现呢?很简单

  1. 开启记住我功能

    //定制请求的授权规则
    @Override
    protected void configure(HttpSecurity http) throws Exception {//。。。。。。。。。。。//开启记住我功能: cookie,默认保存两周http.rememberMe();
    }
    
  2. 我们再次启动项目测试一下

    • 发现登录页多了一个记住我功能

    • 我们登录之后关闭 浏览器,然后重新打开浏览器访问,发现用户依旧存在!

      思考:如何实现的呢?其实非常简单

      我们可以查看浏览器的cookie

  3. 我们点击注销的时候,可以发现,spring security 帮我们自动删除了这个 cookie

  4. cookie发送给浏览器保存,以后登录带上这个cookie,只要通过检查就可以免登录了。如果点击注销,则会删除这个cookie,具体的原理我们在JavaWeb阶段都讲过了,这里就不在多说了!

定制登录页

现在这个登录页面都是spring security 默认的,怎么样可以使用我们自己写的Login界面呢?

  1. 在刚才的登录页配置后面指定 loginpage

    protected void configure(HttpSecurity http) throws Exception {//......// 没有权限默认会到登录页面,需要开启登录的页面// /login页面http.formLogin().loginPage("/toLogin");//......
    }
    
  2. 然后前端也需要指向我们自己定义的 login请求

    <div sec:authorize="!isAuthenticated()"><a class="item" th:href="@{/toLogin}"><i class="address card icon"></i> 登录</a>
    </div>
    
  3. 我们登录,需要将这些信息发送到哪里,我们也需要配置,login.html 配置提交请求及方式,方式必须为post:

    在 loginPage()源码中的注释上有写明:

  4. 这个请求提交上来,我们还需要验证处理,怎么做呢?我们可以查看formLogin()方法的源码!我们配置接收登录的用户名和密码的参数!

    protected void configure(HttpSecurity http) throws Exception {//......// 没有权限默认会到登录页面,需要开启登录的页面// /login页面http.formLogin().usernameParameter("username").passwordParameter("password").loginPage("/toLogin").loginProcessingUrl("/login"); // 登陆表单提交请求//......
    }
    
  5. 在登录页增加记住我的多选框

    <input type="checkbox" name="remember"> 记住我
    
  6. 后端验证处理!

    protected void configure(HttpSecurity http) throws Exception {//......//开启记住我功能: cookie,默认保存两周,自定义接收前端的参数http.rememberMe().rememberMeParameter("remember");
    }
    
  7. 测试,OK

完整配置代码

package nuc.ss.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;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;// AOP:拦截器
@EnableWebSecurity  // 开启WebSecurity模式
public class SecurityConfig extends WebSecurityConfigurerAdapter {//链式编程//授权@Overrideprotected void configure(HttpSecurity http) throws Exception {// 首页所有人都可以访问,功能也只有对应有权限的人才能访问到// 请求授权的规则http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/level1/**").hasRole("vip1").antMatchers("/level2/**").hasRole("vip2").antMatchers("/level3/**").hasRole("vip3");// 没有权限默认会到登录页面,需要开启登录的页面// /login页面http.formLogin().usernameParameter("username").passwordParameter("password").loginPage("/toLogin").loginProcessingUrl("/login");//注销,开启了注销功能,跳到首页http.logout().logoutSuccessUrl("/");// 防止网站工具:get,posthttp.csrf().disable();//关闭csrf功能,登录失败肯定存在的原因//开启记住我功能: cookie,默认保存两周,自定义接收前端的参数http.rememberMe().rememberMeParameter("remember");}// 认证,springboot 2.1.x 可以直接使用// 密码编码: PasswordEncoder// 在spring Secutiry 5.0+ 新增了很多加密方法@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {//这些数据正常应该中数据库中读auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("kuangshen").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");}}

// 在spring Secutiry 5.0+ 新增了很多加密方法
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {

    //这些数据正常应该中数据库中读auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("kuangshen").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");
}

}


SpringBoot18:集成SpringSecurity相关推荐

  1. springboot md5加密_SpringSecurity入门-SpringBoot集成SpringSecurity

    前言 至今Java能够如此的火爆Spring做出了很大的贡献,它的出现让Java程序的编写更为简单灵活,而Spring如今也形成了自己的生态圈,今天咱们探讨的是Spring旗下的一个款认证工具:Spr ...

  2. java.securti_springboot集成springsecurity 使用OAUTH2做权限管理的教程

    Spring Security OAuth2 主要配置,注意application.yml最后的配置resource filter顺序配置,不然会能获取token但是访问一直 没有权限 WebSecu ...

  3. springboot17 集成SpringSecurity

    17.集成SpringSecurity 17.1.安全简介 在 Web 开发中,安全一直是非常重要的一个方面.安全虽然属于应用的非功能性需求,但是应该在应用开发的初期就考虑进来.如果在应用开发的后期才 ...

  4. SpringBoot集成SpringSecurity(二) 个性化登录配置(remember-me mongodb)

    前言 本文件所记录的是使用SpringSecurity实现remember me功能,有兴趣的朋友可以继续阅读,有何不足之处还请各位指出(本文未对用户 -  角色 - 权限三者的关系进行详细介绍详情见 ...

  5. springboot集成springSecurity,jwt实现前后端分离

    ** ## springboot集成springSecurity,jwt实现授权,查看权限,获取用户信息:] 简单的springsecurity授权登录校验我就暂时不写了,博客太多了: 第一步:还是导 ...

  6. 手把手带你在集成SpringSecurity的SpringBoot应用中添加短信验证码登录认证功能

    本文目录 前言 1 自定义AuthenticationToken类 2 自定义AuthenticationProvider类 3 自定义MobilePhoneAuthenticationFilter ...

  7. 狂神java什么来头_狂神说SpringBoot18:集成SpringSecurity

    狂神说SpringBoot系列连载课程,通俗易懂,基于SpringBoot2.2.5版本,欢迎各位狂粉转发关注学习.未经作者授权,禁止转载 SpringSecurity 安全简介 在 Web 开发中, ...

  8. springboot整个cas_SpringBoot集成SpringSecurity+CAS

    1 简介 本文主要讲述如何通过SpringSecurity+CAS在springboot项目中实现单点登录和单点注销的功能. 2 项目依赖 主要依赖如下 org.springframework.boo ...

  9. SpringBoot 实现登录验证码(附集成SpringSecurity)

    SpringBoot 实现登录验证码 1. 生成验证码的工具类 2. 验证码测试接口 3. 验证码过滤器 4. Spring Security配置类引入验证码过滤器 5. 效果图 1. 生成验证码的工 ...

  10. Springboot集成SpringSecurity(获取当前登录人)

    简言 Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架.它是用于保护基于Spring的应用程序的实际标准. Spring Security是一个框架,致力于为Java应 ...

最新文章

  1. Eclipse无法编译,提示错误“找不到或者无法加载主类”解决方法
  2. 如何告别那些没卵用的线上告警!
  3. 网站关键词排名骤降的原因及解决办法
  4. 水利水电工程管理与实务电子版习题_水利水电工程管理与实务复习题集
  5. Android socket 学习记录 之 执行new socket(ip, port)程序崩溃
  6. 使用 Newtonsoft.Json; 序列化正常,反序列化异常的一点思考
  7. [Android] The connection to adb is down, and a severe error has occured
  8. mobi格式电子书_这几种电子书格式的关系与区别,资深Kindler有必要了解了解 !...
  9. java 凸包算法_JAVA凸包算法
  10. 千套多行业多样式单页多页中英文简历模板(自荐信,简历封面,求职),共630M
  11. CDA LEVEL I分数占比解读,看懂这些,考试更容易拿分
  12. JAVASE复习计划
  13. 迭代收缩阈值算法matlab,一种基于自适应快速迭代收缩阈值算法的图像处理方法与流程...
  14. Linux文字游戏制作软件,小精灵美化app
  15. OpenWrt network 启动过程
  16. 什么叫二值化以及二值化的作…
  17. Original error: Error executing adbExec
  18. C#中的lock(锁)应用例子
  19. 《延禧攻略》知识点整理,没看剧的看思维导图就够啦
  20. 设计模式 : 访问者模式

热门文章

  1. 怎么把dicom数据转成nifty数据
  2. Google Test - Google Testing and Mocking Framework
  3. 代码实现堆溢出、栈溢出、永久代溢出、直接内存溢出
  4. STM32H7的终极调试组件Event Recorder
  5. Remix 完全本地化部署
  6. 前端学习日记day4——图文混排
  7. (c++ 遗传算法解决TSP问题)不是吧,这就是遗传算法吗?爱了爱了
  8. echarts关系图(force图)
  9. 做谷粒商城电脑配置不够怎么办?
  10. oracle中todate函数实例,pl/sql to_date 函数使用实例讲解