【Spring Security】基本功能介绍
文章目录
- 1、spring security 简介
- spring security 基本原理
- 2 入门项目
- 2.1 web工程配置
- 2.1 加入Spring Security
- 3. 参数详解
- 3.1、注解 @EnableWebSecurity
- 3.2、抽象类 WebSecurityConfigurerAdapter
- 3.2.1 、configure(AuthenticationManagerBuilder auth)方法
- 3.2.2 configure(HttpSecurity http)方法
- 4. 原理讲解
- 4.1、校验流程图
- 4.2 源码
- 参考
1、spring security 简介
spring security 的核心功能主要包括:
认证 (你是谁) 通过注解
@EnableWebSecurity
开启简单来说,就是需要登录,你需要输入用户名和密码,才能访问某个url。
授权 (你能干什么) 不需要通过指定的开关开启,而是通过配置来增加授权规则来生效,不增加授权规则就不生效
授权的目的是可以把资源进行划分,例如公司有不同的资料,有普通级别和机密级别,只有公司高层才能看到机密级别的子类,而普通级别的资料大家都可以看到! 那么授权就是允许你查看某个资源,当然,如果你没有权限,就拒绝你查看!
认证可以单独使用,即不划分资源的级别,所有人只要登录都可以查看
spring security 基本原理
spring security的核心就是一组过滤器链,项目启动后将会自动配置。最核心的就是 Basic Authentication Filter 用来认证用户的身份,一个在spring security中一种过滤器处理一种认证方式。
如下图所示,这是一组链式处理器类,请求从做往右依次经过多个过滤器类处理:
比如,对于username password认证过滤器来说:
会检查是否是一个登录请求;是否包含username 和 password (也就是该过滤器需要的一些认证信息) ;如果不满足则放行给下一个。
下一个按照自身职责判定是否是自身需要的信息,basic的特征就是在请求头中有 Authorization:Basic eHh4Onh4
的信息。中间可能还有更多的认证过滤器。最后一环是 FilterSecurityInterceptor,这里会判定该请求是否能进行访问rest服务,判断的依据是 BrowserSecurityConfig中的配置,如果被拒绝了就会抛出不同的异常(根据具体的原因)。Exception Translation Filter 会捕获抛出的错误,然后根据不同的认证方式进行信息的返回提示。
注意:绿色的过滤器可以配置是否生效,其他的都不能控制。
2 入门项目
2.1 web工程配置
我们先创建一个普通的webmvc工程,然后创建一些url资源:
这些资源后续会配置为访问受限,需要输入用户名和密码才能访问,其中比较特殊的是/login,当开启认证开关时,这个url资源会有默认实现,即有一个controller对应/login请求,并在没有登录时,重定向至登陆页,并提供一个默认的登陆页。
首先创建spring boot项目HelloSecurity,其pom主要依赖如下:
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-test</artifactId><scope>test</scope></dependency>
</dependencies>
然后在src/main/resources/templates/目录下创建页面:
home.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"><head><title>Spring Security Example</title></head><body><h1>Welcome!</h1>
<p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p></body>
</html>
我们可以看到, 在这个简单的视图中包含了一个链接: “/hello”. 链接到了如下的页面,Thymeleaf模板如下:
hello.html:
目的是覆盖默认的登陆页
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"><head><title>Hello World!</title></head><body><h1>Hello world!</h1></body>
</html>
Web应用程序基于Spring MVC。 因此,你需要配置Spring MVC并设置视图控制器来暴露这些模板。 如下是一个典型的Spring MVC配置类。在src/main/java/hello目录下(所以java都在这里):
注意:自定义了/login的声明,就会覆盖默认的(假设开启认证开关的话,否则默认的就不存在)。
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/home").setViewName("home");registry.addViewController("/").setViewName("home");registry.addViewController("/hello").setViewName("hello");registry.addViewController("/login").setViewName("login");}
}
addViewControllers()方法(覆盖WebMvcConfigurerAdapter中同名的方法)添加了四个视图控制器。 两个视图控制器引用名称为“home”的视图(在home.html中定义),另一个引用名为“hello”的视图(在hello.html中定义)。 第四个视图控制器引用另一个名为“login”的视图。 将在下一部分中创建该视图。
此时,可以跳过来使应用程序可执行并运行应用程序,而无需登录任何内容。
然后启动程序如下:
@SpringBootApplication
public class Application {public static void main(String[] args) throws Throwable {SpringApplication.run(Application.class, args);}
}
2.1 加入Spring Security
假设你希望防止未经授权的用户访问“/ hello”。 此时,如果用户点击主页上的链接,他们会看到问候语,请求被没有被拦截。 你需要添加一个障碍,使得用户在看到该页面之前登录。您可以通过在应用程序中配置Spring Security来实现。 如果Spring Security在类路径上,则Spring Boot会使用“Basic认证”来自动保护所有HTTP端点。 同时,你可以进一步自定义安全设置。首先在pom文件中引入:
<dependencies>...<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>...
</dependencies>
如下是安全配置,使得只有认证过的用户才可以访问到问候页面:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/", "/home").permitAll().anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll().and().logout().permitAll();}
@Autowiredpublic void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");}
}
通过 @EnableWebSecurity声明开启认证!
WebSecurityConfig类使用了@EnableWebSecurity注解 ,以启用Spring Security的Web安全支持,并提供Spring MVC集成。它还扩展了WebSecurityConfigurerAdapter
,并覆盖了一些方法来设置Web安全配置的一些细节。
configure(HttpSecurity)方法定义了哪些URL路径应该被保护,哪些不应该。具体来说,“/”和“/ home”路径被配置为不需要任何身份验证。所有其他路径必须经过身份验证。
当用户成功登录时,它们将被重定向到先前请求的需要身份认证的页面。有一个由 loginPage()指定的自定义“/登录”页面,每个人都可以查看它。
对于configureGlobal(AuthenticationManagerBuilder) 方法,它将单个用户设置在内存中。该用户的用户名为“user”,密码为“password”,角色为“USER”。
现在我们需要创建登录页面。前面我们已经配置了“login”的视图控制器,因此现在只需要创建登录页面即可:
login.html是非必须的,后台会自动生成一个登陆页
login.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"><head><title>Spring Security Example </title></head><body><div th:if="${param.error}">Invalid username and password.</div><div th:if="${param.logout}">You have been logged out.</div><form th:action="@{/login}" method="post"><div><label> User Name : <input type="text" name="username"/> </label></div><div><label> Password: <input type="password" name="password"/> </label></div><div><input type="submit" value="Sign In"/></div></form></body>
</html>
你可以看到,这个Thymeleaf模板只是提供一个表单来获取用户名和密码,并将它们提交到“/ login”。 根据配置,Spring Security提供了一个拦截该请求并验证用户的过滤器。 如果用户未通过认证,该页面将重定向到“/ login?error”,并在页面显示相应的错误消息。 注销成功后,我们的应用程序将发送到“/ login?logout”,我们的页面显示相应的登出成功消息。最后,我们需要向用户提供一个显示当前用户名和登出的方法。 更新hello.html 向当前用户打印一句hello,并包含一个“注销”表单
,如下所示:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"><head><title>Hello World!</title></head><body><h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1><form th:action="@{/logout}" method="post"><input type="submit" value="Sign Out"/></form></body>
</html>
3. 参数详解
3.1、注解 @EnableWebSecurity
在 Spring boot 应用中使用 Spring Security,用到了 @EnableWebSecurity注解,官方说明为,该注解和 @Configuration 注解一起使用, 注解 WebSecurityConfigurer 类型的类,或者利用@EnableWebSecurity 注解继承 WebSecurityConfigurerAdapter的类,这样就构成了 Spring Security 的配置。
3.2、抽象类 WebSecurityConfigurerAdapter
一般情况,会选择继承 WebSecurityConfigurerAdapter 类,其官方说明为:WebSecurityConfigurerAdapter 提供了一种便利的方式去创建 WebSecurityConfigurer的实例,只需要重写 WebSecurityConfigurerAdapter 的方法,即可配置拦截什么URL、设置什么权限等安全控制。
Demo 中重写了 WebSecurityConfigurerAdapter 的两个方法:
WebSecurityConfigurerAdapter.java,方法注释是原文翻译过来的:
/*** 通过 {@link #authenticationManager()} 方法的默认实现尝试获取一个 {@link AuthenticationManager}.* 如果被复写, 应该使用{@link AuthenticationManagerBuilder} 来指定 {@link AuthenticationManager}.** 例如, 可以使用以下配置在内存中进行注册公开内存的身份验证{@link UserDetailsService}:** // 在内存中添加 user 和 admin 用户* @Override* protected void configure(AuthenticationManagerBuilder auth) {* auth* .inMemoryAuthentication().withUser("user").password("password").roles("USER").and()* .withUser("admin").password("password").roles("USER", "ADMIN");*/protected void configure(AuthenticationManagerBuilder auth) throws Exception {this.disableLocalConfigureAuthenticationBldr = true;}/*** 复写这个方法来配置 {@link HttpSecurity}. * 通常,子类不能通过调用 super 来调用此方法,因为它可能会覆盖其配置。 默认配置为:* * http.authorizeRequests().anyRequest().authenticated().and().formLogin().and().httpBasic();**/protected void configure(HttpSecurity http) throws Exception {logger.debug("Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity).");
http.authorizeRequests().anyRequest().authenticated().and().formLogin().and().httpBasic();}
3.2.1 、configure(AuthenticationManagerBuilder auth)方法
入参 AuthenticationManagerBuilder 用于创建一个 AuthenticationManager,让我能够轻松的实现内存验证、LADP验证、基于JDBC的验证、添加UserDetailsService、添加AuthenticationProvider。
示例用法:
protected void configure(AuthenticationManagerBuilder auth) {auth .inMemoryAuthentication().withUser("user").password("password").roles("USER").and().withUser("admin").password("password").roles("USER", "ADMIN");
3.2.2 configure(HttpSecurity http)方法
入参是 final 类 HttpSecurity,可以定义哪些资源是受限的,并且具体受哪些角色之类的限制
HttpSecurity 常用方法及说明:
方法 | 说明 |
---|---|
openidLogin() | 用于基于 OpenId 的验证 |
headers() | 将安全标头添加到响应 |
cors() | 配置跨域资源共享( CORS ) |
sessionManagement() | 允许配置会话管理 |
portMapper() | 允许配置一个PortMapper(HttpSecurity#(getSharedObject(class))),其他提供SecurityConfigurer的对象使用 PortMapper 从 HTTP 重定向到 HTTPS 或者从 HTTPS 重定向到 HTTP。默认情况下,Spring Security使用一个PortMapperImpl映射 HTTP 端口8080到 HTTPS 端口8443,HTTP 端口80到 HTTPS 端口443 |
jee() | 配置基于容器的预认证。 在这种情况下,认证由Servlet容器管理 |
x509() | 配置基于x509的认证 |
rememberMe | 允许配置“记住我”的验证 |
authorizeRequests() | 允许基于使用HttpServletRequest限制访问 |
requestCache() | 允许配置请求缓存 |
exceptionHandling() | 允许配置错误处理 |
securityContext() | 在HttpServletRequests之间的SecurityContextHolder上设置SecurityContext的管理。 当使用WebSecurityConfigurerAdapter时,这将自动应用 |
servletApi() | 将HttpServletRequest方法与在其上找到的值集成到SecurityContext中。 当使用WebSecurityConfigurerAdapter时,这将自动应用 |
csrf() | 添加 CSRF 支持,使用WebSecurityConfigurerAdapter时,默认启用 |
logout() | 添加退出登录支持。当使用WebSecurityConfigurerAdapter时,这将自动应用。默认情况是,访问URL”/ logout”,使HTTP Session无效来清除用户,清除已配置的任何#rememberMe()身份验证,清除SecurityContextHolder,然后重定向到”/login?success” |
anonymous() | 允许配置匿名用户的表示方法。 当与WebSecurityConfigurerAdapter结合使用时,这将自动应用。 默认情况下,匿名用户将使用org.springframework.security.authentication.AnonymousAuthenticationToken表示,并包含角色 “ROLE_ANONYMOUS” |
formLogin() | 指定支持基于表单的身份验证。如果未指定FormLoginConfigurer#loginPage(String),则将生成默认登录页面 |
oauth2Login() | 根据外部OAuth 2.0或OpenID Connect 1.0提供程序配置身份验证 |
requiresChannel() | 配置通道安全。为了使该配置有用,必须提供至少一个到所需信道的映射 |
httpBasic() | 配置 Http Basic 验证 |
addFilterAt() | 在指定的Filter类的位置添加过滤器 |
4. 原理讲解
4.1、校验流程图
4.2 源码
参考
spring security——基本介绍(一) 本篇参考主体
手把手带你入门 Spring Security!
Spring Security解析五:WebSecurityConfigurerAdapter
Spring Security 中文参考手册
【Spring Security】基本功能介绍相关推荐
- Spring Security 与 OAuth2 介绍
个人 OAuth2 全部文章 Spring Security 与 OAuth2(介绍):https://www.jianshu.com/p/68f22f9a00ee Spring Security 与 ...
- Spring Security Oauth2的介绍与应用详解
简介 Spring Security 是一个强大的和高度可定制的身 份验证和访问控制框架,Spring security Oauth2协议, oAuth参考 授权码认证 步骤: 客户端请求第三方授权, ...
- Java认证授权框架Spring Security介绍
Spring Security 是一个非常强大的身份验证和授权控制框架.为了满足企业项目的不同需求,它提供了很多定制化开发的解决方案,通过简单的调整配置,就能为我们的应用提供一套可靠的安全保障.本节课 ...
- Spring Security 基本介绍,初窥路径
Spring Security 基本介绍和环境搭建 实验介绍 Spring Security 是一个非常强大的身份验证和授权控制框架.为了满足企业项目的不同需求,它提供了很多定制化开发的解决方案,通过 ...
- 【Spring】Spring Security介绍及其入门案例
文章目录 前言 1. SpringSecurity 框架简介 1.1 概要 1.2 历史 1.3 同款产品对比 1.3.1 Spring Security 1.3.2 Shiro 1.4 模块划分 2 ...
- springboot2 war页面放在那_Spring Boot2 系列教程(三十三)整合 Spring Security
Spring Security 是 Spring 家族中的一个安全管理框架,实际上,在 Spring Boot 出现之前,Spring Security 就已经发展了多年了,但是使用的并不多,安全管理 ...
- 手把手带你入门 Spring Security!
Spring Security 是 Spring 家族中的一个安全管理框架,实际上,在 Spring Boot 出现之前,Spring Security 就已经发展了多年了,但是使用的并不多,安全管理 ...
- springboot 访问html_Spring Boot中使用Spring Security进行安全控制
我们在编写Web应用时,经常需要对页面做一些安全控制,比如:对于没有访问权限的用户需要转到登录表单页面.要实现访问控制的方法多种多样,可以通过Aop.拦截器实现,也可以通过框架实现(如:Apache ...
- Spring Boot中使用Spring Security进行安全控制
我们在编写Web应用时,经常需要对页面做一些安全控制,比如:对于没有访问权限的用户需要转到登录表单页面.要实现访问控制的方法多种多样,可以通过Aop.拦截器实现,也可以通过框架实现(如:Apache ...
- Spring Security:基于内存的认证信息
本文来说下Spring Boot+Spring Security:基于内存的认证信息 文章目录 概述 需求缘起 编码思路 基于内存的认证信息 创建一个配置类 密码加密 本文小结 概述 需求缘起 上面我 ...
最新文章
- [Ubuntu]更改所有子文件和子目录所有者权限
- Paper Review: Bayesian Shrinkage towards Sharp Minimaxity
- Eclipse run configrations 配置
- 信息学奥赛一本通(1227:Ride to Office)
- Android 系统(137)---android打包解包boot.img,system.img
- Memecached stats
- Redis 最大连接数查询与设置、释放超时链接
- centos7安装git踩坑记
- hrrn算法java_常用的调度算法
- 关于线程协同的火车票售卖程序
- 基于Jeecg的权限获取
- 计算机一级考试B十类理论题,10计算机一级统考理论题.doc
- python画海贼王_用python自动爬取海贼王漫画推送kindle
- sin n次方 x 的降幂公式
- 数据结构 PTA 绪论
- 以下是三套目前Web 2.0网站流行试用的颜色元素
- IntelliJ IDEA 之 Spring Boot (五) Freemarker 模板引擎渲染 Web 试图
- 软件工程导论项目之小学生四则运算出题软件
- 软件测试-如何体现自己的价值?
- 【英语定语从句理解】
热门文章
- 【vue axios 跨域】cookie、origin等一步步递进的跨域踩坑 已解决
- linux 内核学习12-符号共享
- 安装Saas芸众商城系统后提示:{“result“:0,“msg“:““,“data“:{“status“:-4}}报错的原因
- BroadcastReceiver多次unregisterReceiver 导致 Receiver not registered问题
- php session 编译,编译安装PHP使用session_start()时报错解决办法
- 刺激战场电脑版android,逍遥安卓模拟器像端游操控玩《绝地求生刺激战场》电脑版...
- 管理中一些好用可口的胡萝卜
- 论文阅读之Improved Word Representation Learning with Sememes(2017)
- Http Trunk
- react或vue启动命令那些事