Spring Security 介绍
Spring Security介绍
- 开源
- 提供企业级的安全认证和授权
Spring安全拦截器
认证管理器
认证模式
Basic
HTTP 1.0中使用的认证方法,使用用户名和密码Base64编码的方式
浏览器弹出对话框,用户输入用户名和密码,加入头部
无状态
安全性不足
Digest
解决安全性问题
浏览器对用户名和密码请求方法,URL等进行MD5运算,发送到服务器
服务器获取到用户名密码,请求方法,URL等MD5运算,查看是否相等
安全性问题依然存在
X.509
访问决策管理器
运行身份管理器
常用权限拦截器
Spring-Security拦截器链流程图

介绍几个常用的Filter
SecurityContextPersistenceFilter
基于ThreadLocal
LogoutFilter
发送注销请求时,清空用户的session,cookie,重定向
AbstractAuthenticationProcessingFilter
用户登录的请求
DefaultLoginPageGeneratingFilter
生成登录页面
BasicAuthencationFilter
SecurityContextHolderAwareRequestFilter
包装,提供额外的数据
RememberMeAuthemticationFilter
提供RememberMe功能,当cookie中存在rememberme时,登录成功后生成唯一cookie
ExceptionTranlationFilter
请求到对应的页面,响应异常
ExceptionTranslationFilter异常处理过滤器,该过滤器用来处理在系统认证授权过程中抛出的异常(也就是下一个过滤器
FilterSecurityInterceptor),主要是 处理AuthenticationException和AccessDeniedExceptionSessionManagerFilter
FilterSecurityInterceptor(授权)
用户没有登录,抛出未登录异常
用户登录,但是没有权限访问该资源,抛出拒绝访问的异常
用户登录,有权限,则放行
此过滤器为认证授权过滤器链中最后一个过滤器,该过滤器之后就是请求真正的 服务
Filter如何执行
FilterChainProxy会按照顺序调用一组Filter,完成授权验证
请求首先经过Servlet Filter链,这里面包含6个Filter

可以看到如下的一个Filter
ApplicationFilterConfig[name=springSecurityFilterChain, filterClass=org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean$1]
这个就是Spring Security Filter的入口,它的类型是DelegationFilterProxy
那么,这个DelegationFilterProxy到底执行了什么样的操作呢?可以简单看一下源码
重点看一下部分的源码:
public class DelegatingFilterProxy extends GenericFilterBean {
// 类中的属性
@Nullable
private String contextAttribute;
@Nullable
private WebApplicationContext webApplicationContext;
@Nullable
private String targetBeanName;
private boolean targetFilterLifecycle = false;
/**
* 重点在于这个属性
*/
@Nullable
private volatile Filter delegate;
private final Object delegateMonitor = new Object();
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
// Lazily initialize the delegate if necessary.
Filter delegateToUse = this.delegate;
if (delegateToUse == null) {
synchronized (this.delegateMonitor) {
delegateToUse = this.delegate;
if (delegateToUse == null) {
WebApplicationContext wac = findWebApplicationContext();
if (wac == null) {
throw new IllegalStateException("No WebApplicationContext found: " +
"no ContextLoaderListener or DispatcherServlet registered?");
}
delegateToUse = initDelegate(wac);
}
this.delegate = delegateToUse;
}
}
// Let the delegate perform the actual doFilter operation.
invokeDelegate(delegateToUse, request, response, filterChain);
}
// .......
/**
* Initialize the Filter delegate, defined as bean the given Spring
* application context.
* <p>The default implementation fetches the bean from the application context
* and calls the standard {@code Filter.init} method on it, passing
* in the FilterConfig of this Filter proxy.
* @param wac the root application context
* @return the initialized delegate Filter
* @throws ServletException if thrown by the Filter
*/
protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
String targetBeanName = getTargetBeanName();
Assert.state(targetBeanName != null, "No target bean name set");
Filter delegate = wac.getBean(targetBeanName, Filter.class);
if (isTargetFilterLifecycle()) {
delegate.init(getFilterConfig());
}
return delegate;
}
// 调用delegate的doFilter方法
protected void invokeDelegate(
Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
delegate.doFilter(request, response, filterChain);
}
}
根据以上代码的注释我们可以看出大概的执行流程
那类中的Filter delegate是什么呢?断点调式运行可以找到以下东西

这是Spring Security 提供的一个FilterChainProxy,关注其中的关键源码
public class FilterChainProxy extends GenericFilterBean {
// 包含一组SecurityFilterChain
private List<SecurityFilterChain> filterChains;
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
boolean clearContext = request.getAttribute(FILTER_APPLIED) == null;
if (clearContext) {
try {
request.setAttribute(FILTER_APPLIED, Boolean.TRUE);
doFilterInternal(request, response, chain);
}
finally {
SecurityContextHolder.clearContext();
request.removeAttribute(FILTER_APPLIED);
}
}
else {
doFilterInternal(request, response, chain);
}
}
private void doFilterInternal(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
FirewalledRequest fwRequest = firewall
.getFirewalledRequest((HttpServletRequest) request);
HttpServletResponse fwResponse = firewall
.getFirewalledResponse((HttpServletResponse) response);
// 根据请求获取匹配的一组Filter
// 这里返回的Filter就是上述的那些AuthenticationFilter,比如UsernamePasswordAuthenticationFilter
List<Filter> filters = getFilters(fwRequest);
if (filters == null || filters.size() == 0) {
if (logger.isDebugEnabled()) {
logger.debug(UrlUtils.buildRequestUrl(fwRequest)
+ (filters == null ? " has no matching filters"
: " has an empty filter list"));
}
fwRequest.reset();
chain.doFilter(fwRequest, fwResponse);
return;
}
VirtualFilterChain vfc = new VirtualFilterChain(fwRequest, chain, filters);
vfc.doFilter(fwRequest, fwResponse);
}
/**
* Returns the first filter chain matching the supplied URL.
*
* @param request the request to match
* @return an ordered array of Filters defining the filter chain
*/
private List<Filter> getFilters(HttpServletRequest request) {
for (SecurityFilterChain chain : filterChains) {
if (chain.matches(request)) {
return chain.getFilters();
}
}
return null;
}
}
到此,我们可以总结出如下的流程图:

Spring Security 介绍的更多相关文章
- Spring Security 介绍与Demo
一.Spring Security 介绍 Spring Security 是针对Spring项目的安全框架,也是Spring Boot底层安全模块的默认技术选型.我们仅需引入spring-boot-s ...
- Spring Security安全框架入门篇
一.Spring Security相关概念 1.1..Spring Security介绍: Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安 ...
- Spring Security +Oauth2 +Spring boot 动态定义权限
Oauth2介绍:Oauth2是为用户资源的授权定义了一个安全.开放及简单的标准,第三方无需知道用户的账号及密码,就可获取到用户的授权信息,并且这是安全的. 简单的来说,当用户登陆网站的时候,需要账号 ...
- 「快学springboot」集成Spring Security实现鉴权功能
Spring Security介绍 Spring Security是Spring全家桶中的处理身份和权限问题的一员.Spring Security可以根据使用者的需要定制相关的角色身份和身份所具有的权 ...
- SpringBoot集成Spring Security
1.Spring Security介绍 Spring security,是一个强大的和高度可定制的身份验证和访问控制框架.它是确保基于Spring的应用程序的标准 --来自官方参考手册 Spring ...
- Java安全框架(一)Spring Security
Java安全框架(一)Spring Security 文章主要分三部分 1.Spring Security的架构及核心组件:(1)认证:(2)权限拦截:(3)数据库管理:(4)权限缓存:(5)自定 ...
- 权限管理3-整合Spring Security
一.Spring Security介绍 1.框架介绍 Spring 是一个非常流行和成功的 Java 应用开发框架.Spring Security 基于 Spring 框架,提供了一套 Web 应用安 ...
- Spring Security探究之路之开始
前言 在Spring Security介绍中,我们分析到了根据请求获取匹配的SecurityFilterChain,这个类中包含了一组Filter 接下来我们从这些Filter开始探究之旅 Sprin ...
- Spring Security 与 OAuth2 介绍
个人 OAuth2 全部文章 Spring Security 与 OAuth2(介绍):https://www.jianshu.com/p/68f22f9a00ee Spring Security 与 ...
随机推荐
- JMeter_用户自定义变量
在实际测试过程中,我们经常会碰到脚本开发时与测试执行时的服务地址不一样的情况,为了方便,我们会把访问地址参数化,当访问地址变化了,我们只需要把参数对应的值改动一下就可以了. 一.添加用户自定义变量元件 ...
- 你不得不了解的Python3.x新特性
从 3.0 到 3.8,Python 3 已经更新了一波又一波,但似乎我们用起来和 2.7 没有太大区别?以前该怎么写 2.7 的代码现在就怎么写,只不过少数表达方式变了而已.在这篇文章中,作者介绍了 ...
- 修正了Model1模式,进入如今盛行的的Model2模式,也就是MVC模式
注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6513668601843548675/ 1.<JSP页面实际上就是Servlet> 2.<JSP页 ...
- 使用.NET 6开发TodoList应用(23)——实现请求限流
系列导航及源代码 使用.NET 6开发TodoList应用文章索引 需求 Rate Limiting允许保护我们的API服务免受过多请求的连接导致的性能下降,如果请求次数超过了限制,API服务端将会拒 ...
- LINUX学习-Mysql集群-一主多从
新建一台服务器 192.168.88.40 yum -y install mysql mysql-server 编辑etc下的配置文件 vim /etc/my.cnf 输入 bin-log=mysql ...
- LINUX学习-Nginx+JDK+Tomcat+Mysql搭建JAVA WEB
一.安装JDK环境 1.先到 https://www.oracle.com/java/technologies/javase-jdk8-downloads.html 下载linux jdk-8u2 ...
- 学习javaScript必知必会(4)~事件、事件绑定、取消事件冒泡、事件对象
1.常用的事件: ① onload:页面加载 ② onblur: 失去焦点 onfocus: 获取焦点 ③ onclick:点击 ④ onmouseover:鼠标经过 onmouseout:鼠标离开 ...
- WebGPU相关的资源学习和社区
我在网上搜寻了很多关于WebGPU相关的资料: #我觉得首先在B站上看到的徐博士免费教程非常好,讲解详细,并且评论回复比较快,都会有回应,徐博士B站网址:https://space.bilibili. ...
- Tomcat下载安装以及配置方法
Tomcat环境变量配置方法 注意一定要在java环境配置成功之后再来配置tomcat.我这里仅展现在Windows系统下载的安装方法 Tomcat下载地址如下: https://tomcat.apa ...
- threejs - src - WebGLProgram是如何组建Shader的?
threejs - src - WebGLProgram是如何组建Shader的? WebGLProgram的构建 WebGLProgram构建的时候需要的参数如下: // \param render ...