博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Security验证流程剖析及自定义验证方法
阅读量:6936 次
发布时间:2019-06-27

本文共 3103 字,大约阅读时间需要 10 分钟。

Spring Security本质上是一连串的Filter, 然后又以一个独立的Filter的形式插入到Filter Chain里,其名为FilterChainProxy。 如图所示。

o_security-filters.png

实际上FilterChainProxy下面可以有多条Filter Chain,来针对不同的URL做验证,而Filter Chain中所拥有的Filter则会根据定义的服务自动增减。所以无需要显示再定义这些Filter,除非想要实现自己的逻辑。

o_security-filters-dispatch.png

关键类

Authentication

Authentication是一个接口,用来表示用户认证信息,在用户登录认证之前相关信息会封装为一个Authentication具体实现类的对象,在登录认证成功之后又会生成一个信息更全面,包含用户权限等信息的Authentication对象,然后把它保存在 SecurityContextHolder所持有的SecurityContext中,供后续的程序进行调用,如访问权限的鉴定等。

AuthenticationManager

用来做验证的最主要的接口为AuthenticationManager,这个接口只有一个方法:

public interface AuthenticationManager {  Authentication authenticate(Authentication authentication)    throws AuthenticationException;}

其中authenticate()方法运行后可能会有三种情况:

  1. 验证成功,返回一个带有用户信息的Authentication
  2. 验证失败,抛出一个AuthenticationException异常。
  3. 无法判断,返回null

ProviderManager

ProviderManager是上面的AuthenticationManager最常见的实现,它不自己处理验证,而是将验证委托给其所配置的AuthenticationProvider列表,然后会依次调用每一个 AuthenticationProvider进行认证,这个过程中只要有一个AuthenticationProvider验证成功,就不会再继续做更多验证,会直接以该认证结果作为ProviderManager的认证结果。

o_Structure.jpg

认证过程

  1. 用户使用用户名和密码进行登录。
  2. Spring Security将获取到的用户名和密码封装成一个Authentication接口的实现类,比如常用的UsernamePasswordAuthenticationToken
  3. 将上述产生的Authentication对象传递给AuthenticationManager的实现类ProviderManager进行认证。
  4. ProviderManager依次调用各个AuthenticationProvider进行认证,认证成功后返回一个封装了用户权限等信息的Authentication对象。
  5. AuthenticationManager返回的Authentication对象赋予给当前的SecurityContext

自定义验证

有了以上的知识储备后就可以来自定义验证方法了。通过上面可以看出,实际上真正来做验证操作的是一个个的AuthenticationProvider,所以如果要自定义验证方法,只需要实现一个自己的AuthenticationProvider然后再将其添加进ProviderManager里就行了。

自定义AuthenticationProvider

@Componentpublic class CustomAuthenticationProvider  implements AuthenticationProvider {     @Override    public Authentication authenticate(Authentication authentication)       throws AuthenticationException {          String name = authentication.getName();        String password = authentication.getCredentials().toString();                 if (shouldAuthenticateAgainstThirdPartySystem()) {              // use the credentials            // and authenticate against the third-party system            return new UsernamePasswordAuthenticationToken(              name, password, new ArrayList<>());        } else {            return null;        }    }     @Override    public boolean supports(Class
authentication) { return authentication.equals( UsernamePasswordAuthenticationToken.class); }}

其中的supports()方法接受一个authentication参数,用来判断传进来的authentication是不是该AuthenticationProvider能够处理的类型。

注册AuthenticationProvider

现在再将刚创建的AuthenticationProvider与ProviderManager里注册,所有操作就完成了。

@Configuration@EnableWebSecurity@ComponentScan("org.baeldung.security")public class SecurityConfig extends WebSecurityConfigurerAdapter {      @Autowired    private CustomAuthenticationProvider authProvider;     @Override    protected void configure(      AuthenticationManagerBuilder auth) throws Exception {          auth.authenticationProvider(authProvider);    }     @Override    protected void configure(HttpSecurity http) throws Exception {        http.authorizeRequests().anyRequest().authenticated()            .and()            .httpBasic();    }}

参考资料

转载于:https://www.cnblogs.com/xz816111/p/8528896.html

你可能感兴趣的文章
[shell] if else以及大于、小于、等于逻辑表达式 [转]
查看>>
关于MySQL的wait_timeout连接超时问题报错解决方案
查看>>
Windows Phone 8初学者开发—第19部分:设置RecordAudio.xaml页面
查看>>
实例化和具体化详解
查看>>
端口学习
查看>>
paip.突破 网站 手机 验证码 的 破解 总结
查看>>
设计模式笔记:策略模式(Strategy)
查看>>
zk 06之:ZooKeeper命令、命令行工具及简单操作
查看>>
嵌入式系统 Boot Loader
查看>>
Replication--将LSN转换成16进制
查看>>
关闭 ubuntu System program problem detected
查看>>
extjs编写自定义组件,第一个例子
查看>>
8天玩转并行开发——第六天 异步编程模型
查看>>
如何做到早起一小时?(转)
查看>>
[ACM_水题] ZOJ 3712 [Hard to Play 300 100 50 最大最小]
查看>>
OGG_GoldenGate数据表定义方式DEFGEN(案例)
查看>>
使用jQuery获取radio/checkbox组的值的代码收集
查看>>
计算机:2014考研大纲全面解析
查看>>
linux下系统启动时,几个配置文件 /etc/profile、~/.bash_profile 等几个文件的执行过程,先后顺序...
查看>>
[Android]对BaseAdapter中ViewHolder编写简化
查看>>