Spring Security 文档
Spring Security OAuth 已经不推荐使用了,最新的OAuth 2.0 的支持由Spring Security提供,官方给出了迁移指南 http://OAuth 2.0 Migration Guide。文档还是摆在一起的, Spring Security 大类里面也有 Spring Security OAuth 的文档。
Spring Security是一套可定制的授权和访问控制框架,容易扩展适应需要。
架构
应用安全问题大体可归为两个,认证(Authentication)和授权(authorization )
认证(Authentication)
AuthenticationManager 是认证的主要策略接口,只有一个方法,authenticate()方法可能返回值有三种:
1.输入有效,验证通过,返回Authentication
对象 2.输入无效,验证不通过,抛出异常AuthenticationException
,此异常不需要捕获处理 3.不能确定,返回null
public interface AuthenticationManager {Authentication authenticate(Authentication authentication)throws AuthenticationException;
}
ProviderManager是最常用的AuthenticationManager的实现,其内部委托AuthenticationProvider
链进行处理。
受保护的资源通常逻辑上可以分组(路径匹配同一个模式,如 /api/** 的资源),每组可以拥有自己专门的AuthenticationManager,通常是ProviderManager,这些ProviderManager共享同一个parent,parent是一种全局的资源,作为所有provider的后备。
定制认证管理
Spring Security提供了一些帮助工具用于构建AuthenticationManager, AuthenticationManagerBuilder是最常用的帮助工具
配置全局AuthenticationManager的方式
@Configuration
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {... // web stuff here@Autowiredpublic void initialize(AuthenticationManagerBuilder builder, DataSource dataSource) {builder.jdbcAuthentication().dataSource(dataSource).withUser("dave").password("secret").roles("USER");}}
相比之下,使用@Override的将构建一个本地的AuthenticationManager
@Configuration
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {@AutowiredDataSource dataSource;... // web stuff here@Overridepublic void configure(AuthenticationManagerBuilder builder) {builder.jdbcAuthentication().dataSource(dataSource).withUser("dave").password("secret").roles("USER");}}
Spring Boot提供了默认的全局AuthenticationManager
,默认的也是足够安全的,除非你需要自己定制AuthenticationManager
。
访问控制
一旦认证成功,便可授权访问了,访问控制的核心策略接口AccessDecisionManager,它有三个实现,最后都是委托给AccessDecisionVoter
实例,有点像ProviderManager
委托给AuthenticationProviders。
public interface AccessDecisionVoter<S> {int ACCESS_GRANTED = 1;int ACCESS_ABSTAIN = 0;int ACCESS_DENIED = -1;boolean supports(ConfigAttribute attribute);boolean supports(Class<?> clazz);int vote(Authentication authentication, S object,Collection<ConfigAttribute> attributes);
}
object代表用户想访问的资源,attributes是对object的修饰。大部分情况使用默认的AccessDecisionManager,即AffirmativeBased(任意一个vote返回肯定,便可访问资源)。
Web Security
在web中Spring Security是基于Servlet Filters。客户端发送请求,容器根据uri决定应用哪个Filter和Servlet。最多只有一个Servlet处理请求,但Filter是一个有序链条,可以终止向下,可以修改请求和响应,所以Filter顺序至关重要。
v3.1开始FilterChainProxy配置SecurityFilterChain链条,用来匹配请求。
自定义过滤器链
@Configuration
@Order(SecurityProperties.BASIC_AUTH_ORDER - 10)
public class ApplicationConfigurerAdapter extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.antMatcher("/match1/**")...;}
}
请求匹配和授权
@Configuration
@Order(SecurityProperties.BASIC_AUTH_ORDER - 10)
public class ApplicationConfigurerAdapter extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.antMatcher("/match1/**").authorizeRequests().antMatchers("/match1/user").hasRole("USER").antMatchers("/match1/spam").hasRole("SPAM").anyRequest().isAuthenticated();}
}
参考:
Securing a Web Application
Spring Security Architecture
OAuth 2.0 Resource Server
Spring Security 文档相关推荐
- Spring Security文档
1.项目 https://spring.io/projects/spring-security 2.文档 https://docs.spring.io/spring-security/site/doc ...
- Spring Boot(2)---Spring Boot文档及其导航说明
Spring Boot文档及其导航说明 TagsSpring Boot, Spring Boot中文官方文档 本节简要介绍Spring Boot参考文档.它作为文档其余部分的导航. 1.关于文档 Sp ...
- Spring帮助文档、API查看
Spring帮助文档.API查看 这里的 spring指的是springFramework 一.打开网站首页 二.进入到springFramework介绍页面 三.点击LEARN查看文档内容
- Spring学习文档
文章目录 Spring 创建maven项目,在项目中产生的pom.xml文件中,导入spring的坐标 Spring MVC Spring 创建maven项目,在项目中产生的pom.xml文件中,导入 ...
- spring框架文档学习(包会)
文章目录 spring简介 IOC控制反转 ioc概念 ioc使用 (1)导入依赖 (2)编写实体类 (3)编写配置文件 (3)创建容器从容器中获取对象并测试 ioc三种创建对象方式 (1)下标赋值 ...
- boot spring test 文档_Spring、Spring Boot 和 TestNG 测试指南 ( 3 )
原标题:Spring.Spring Boot 和 TestNG 测试指南 ( 3 ) 来源:chanjarster, github.com/chanjarster/spring-test-exampl ...
- Spring框架文档(二 )
@[TOC](文章目录) 原文英文链接: https://docs.spring.io/spring/docs/5.2.3.BUILD-SNAPSHOT/spring-framework-refere ...
- Spring架构文档:程序员(工程师)的圣经
圣经 作为一名工程师,尤其是服务端工程师,在Spring架构大行其道的时候,只有熟悉Spring的架构,才能有未来啊! 一.Spring的架构文档 Spring的架构文档 这个有空的时候,多读读!
- GitHub标星75k,阿里15W字的Spring高级文档(全彩版),真的太香了
随着 Spring 使用越来越广泛,Spring 已经成为 Java 程序员面试的必问知识点,很多同学对于Spring理解不是那么的深刻,经常就会被几个连环追问给干趴了! 今天小编整理了一下一线架构师 ...
最新文章
- CV之FD:基于dlib、cv2库利用warpPerspective函数和_68_face_landmarks文件实现AI换脸渐变融合视频效果
- no nlsxbe in java.library.path
- @Scheduled
- [ZJOI2007]矩阵游戏
- Error running tomcat8 Address localhost:1099 is already in use 错误解决
- linux中内存挂载到目录下
- 使用浏览器地址栏调用CXF Webservice的写法
- android 退出应用没有走ondestory方法,[Android基础论]为何Activity退出之后,系统没有调用onDestroy方法?...
- 机器学习大作业_机器学习编程作业6-支持向量机(Python版)
- 通信尾纤常用尾纤简介
- linux 配置 MP3 RMVB 解码器
- 副业真没想你的这么容易做!
- 致我们终将远离的子女
- 随身WIFI安装Debian流程记录
- ABAP 客户主数据批量导入
- 传祺gac6480_传祺gs82020款,传祺GAC6480J2F5
- 西门子SMART 200 modbus rtu通讯宇电温控器例程 运行/停止的控制及指示;温度设定,上下限报警设定
- Android4.4之后休眠状态下Alarm不准时的问题
- 欧拉角与旋转矩阵的转换关系
- hdu4745区间dp处理环形
热门文章
- 5G QoS和DNN以及网络切片技术
- Android水印字体,android实现文字水印效果 支持多行水印
- 阿里云轻量应用服务器最新价格表(2023更新)
- jsp mysql简单仓库信息管理系统_基于jsp+mysql的JSP简单学生信息管理系统
- 更新公告丨AToken2.6版本更新 轻松管理你的地址簿
- 实现GithubMarkdown目录/页内跳转
- 基于java的聊天室系统设计与实现(项目报告+开题报告+答辩PPT+源代码+部署视频)
- SOLIDWORKS钣金功能-整体折弯后边线法兰选择不了边线
- sysinfo结构体
- 获取linux详细信息,通过编程获取Linux文件系统使用的详细信息