一、安全

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

认证

身份验证是关于验证您的凭据,如用户名/用户ID和密码,以验证您的身份。系统确定您是否就是您所说的使用凭据。在公共和专用网络中,系统通过登录密码验证用户身份。身份验证通常通过用户名和密码完成,

授权

另一方面,授权发生在系统成功验证您的身份后,最终会授予您访问资源(如信息,文件,数据库,资金,位置,几乎任何内容)的完全权限。简单来说,授权决定了您访问系统的能力以及达到的程度。验证成功后,系统验证您的身份后,即可授权您访问系统资源。

二、Spring Security

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

WebSecurityConfigurerAdapter:自定义Security策略

通过在配置类中继承该类重写configure(HttpSecurity http)方法来实现自定义策略

@EnableWebSecurity:开启WebSecurity模式

在配置类上标注@EnableWebSecurity开启WebSecurity模式

三、Springboot整合Spring Security

(1)加入依赖
        <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>

导入spring security的包之后,默认情况所有应用访问认证授权,默认用户名user,密码为随机生成的uuid,启动时打印在控制台

(2)登录 / 认证 / 授权

自定义安全配置

@EnableWebSecurity
public class MySecurityConfig 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来到登陆页//   重定向到/login?error表示登陆失败http.formLogin();}//定义认证规则@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser("zhang").password("123").roles("VIP1", "VIP2").and().withUser("aaa").password("1234").roles("VIP3");}
}

自定义为明文的加密方式的类

public class CustomPasswordEncoder implements PasswordEncoder {@Overridepublic String encode(CharSequence charSequence) {return charSequence.toString();}@Overridepublic boolean matches(CharSequence charSequence, String s) {return s.equals(charSequence.toString());}
}

如果不定义此类,会报 There is no PasswordEncoder mapped for the id “null”
参见:
https://blog.csdn.net/Hello_World_QWP/article/details/81811462

(3)注销 / 权限认证

注销

@Overrideprotected void configure(HttpSecurity http) throws Exception {//根目录允许所有人访问,其他目录都需要对应角色http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/level1/**").hasRole("VIP1").antMatchers("/level2/**").hasRole("VIP2").antMatchers("/level3/**").hasRole("VIP3");//开启自动配置的登陆功能,效果,如果没有登陆,没有权限就会来到登陆页面// /login来到登陆页//   重定向到/login?error表示登陆失败http.formLogin();//开启自动配置的注销功能//向/logout发送post请求表示注销//.logoutSuccessUrl() 可以自定义注销跳转到什么页面http.logout().logoutSuccessUrl("/");}

权限认证

作用

  1. 登录后只有注销按钮
  2. 根据是否登录显示游客或用户信息
  3. 根据角色类型显示信息

步骤

  1. 加入thymeleaf和spring security整合依赖
        <dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-springsecurity5</artifactId></dependency>
  1. welcome.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<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="@{/login}">请登录</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>
(4)记住我 / 定制登录页

记住我

    protected void configure(HttpSecurity http) throws Exception {//根目录允许所有人访问,其他目录都需要对应角色http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/level1/**").hasRole("VIP1").antMatchers("/level2/**").hasRole("VIP2").antMatchers("/level3/**").hasRole("VIP3");//开启自动配置的登陆功能,效果,如果没有登陆,没有权限就会来到登陆页面//   /login来到登陆页//   重定向到/login?error表示登陆失败http.formLogin();//开启自动配置的注销功能//向/logout发送post请求表示注销//.logoutSuccessUrl() 可以自定义注销跳转到什么页面http.logout().logoutSuccessUrl("/");//开始记住我功能//登录成功后,将cookie发给浏览器保存,以后访问页面带上这个cookie,只要通过检查就可以免登陆// 点击注销,会删除cookieshttp.rememberMe();}

定制登录页

protected void configure(HttpSecurity http) throws Exception {//根目录允许所有人访问,其他目录都需要对应角色http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/level1/**").hasRole("VIP1").antMatchers("/level2/**").hasRole("VIP2").antMatchers("/level3/**").hasRole("VIP3");//开启自动配置的登陆功能,效果,如果没有登陆,没有权限就会来到登陆页面//   /login来到登陆页//   重定向到/login?error表示登陆失败http.formLogin().usernameParameter("user")  //表单用户名name.passwordParameter("pwd")   //表单密码name.loginPage("/userlogin");   //定制登陆页路径;//开启自动配置的注销功能//向/logout发送post请求表示注销//.logoutSuccessUrl() 可以自定义注销跳转到什么页面http.logout().logoutSuccessUrl("/");//开始记住我功能//登录成功后,将cookie发给浏览器保存,以后访问页面带上这个cookie,只要通过检查就可以免登陆// 点击注销,会删除cookieshttp.rememberMe().rememberMeParameter("rem");}

通过loginPage(url)设置登录页路径后,在定制的登录页发送post url即为登录请求,并设置表单的name属性都为对应值;
通过勾选记住我,session退出后依然能通过cookie保存用户信息,下次免登陆

<!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="rem">记住我<br><input type="submit" value="登陆"></form></div>
</body>
</html>

31 | SpringBoot安全之整合Spring Security相关推荐

  1. springboot安全之整合spring security

    1-新建一个项目,先不引入security,引入, 2-resources->templates->welcome.html <!DOCTYPE html> <html ...

  2. 八、springboot整合Spring Security

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

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

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

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

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

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

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

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

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

  7. spring boot整合spring security笔记

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

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

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

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

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

最新文章

  1. [Buzz.Today]2011.06.26
  2. js 方法传递对象参数
  3. 服务器kvm切换器维修,KVM切换器常见问题
  4. 试列出种计算机组生产率的公式,农业机械化生产学思考题
  5. 几个交换问题的咨询?
  6. DVR服务器如何装系统,车载监控dvr如何配置服务器
  7. 主从复制之操作实践(二)
  8. sa linux,sa | 搜索结果 | Linux运维部落
  9. Java重命名文件的方法_java重命名文件(附道客巴巴文档下载方法)
  10. ES Transport Client学习
  11. 自定义微信小程序TabBar
  12. win7 win 2008 R2多系统multi OS
  13. ycharm无法识别导入本地py文件
  14. 2019年又一位华为工程师倒下了
  15. MySQL 5.6 免安装版(绿色版or解压版)修改编码
  16. This service allows sftp connections only
  17. python课程改进建议_关于Python课程的思考和意见
  18. python arduino 微信_MicroPython动手做(27)——物联网之微信小程序
  19. vue 组件开发 ---- rui-vue-poster 海报制作
  20. 【正则表达式】去除首尾空格

热门文章

  1. 小傻蛋的妹妹跟随小甲鱼学习Python的第十节010
  2. MinkowskiEngine安装问题
  3. Android ShapeableImageView使用详解,告别shape、三方库
  4. 安装ffmpeg win10教程
  5. 与小米一起“星辰大海,云端漫步”
  6. for(int a:b)在C++中的作用
  7. 手机流量偷跑调查:使用习惯不当或软件出问题
  8. Telelogic Tau 第二代
  9. 移动端和pc端微信加入群聊
  10. 软件开发过程与项目管理(1.项目管理概述)