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理解不是那么的深刻,经常就会被几个连环追问给干趴了! 今天小编整理了一下一线架构师 ...
最新文章
- Unit01: AJAX 、 AJAX的使用
- PHP高效获取远程图片尺寸和大小(转)
- java httppost 400_java – HTTP状态400 – 必需字符串参数’walletName’不存在
- Web前端是什么?大牛推荐的高效学习路线,减少2倍时间
- libQt5Core.so: undefined reference to `dlclose@GLIBC_2.4'
- [转]华人总结“十个”出了国才能知道秘密!
- Code[VS]1159 最大全0子矩阵
- 百度文库的内容怎么复制
- 柏云服务器点歌系统IP,阿蛮歌霸网络版点歌系统安装说明.docx
- 软件工程导论复习之需求分析
- Windows中ActiveX控件注册的方法
- esp8266教程:网络基础知识
- vbs教程《变量使用》
- Java的内心世界和外部世界协调的统一
- 融入动画技术的交互应用——解压小游戏“蒲公英与星”
- Ubuntu18.04 sqlite3安装及其应用
- 手机lightroom预设位置
- 多CPU ,多核和进程以及多线程之间的关系
- 弘辽科技:淘宝销量数据从哪查?销量怎么提升?
- MySQL日期格式化函数DATE_FORMAT(‘2020-09-14 14:08:16‘,‘%y%m%d %H%I%S‘)