UsernamePasswordAuthenticationFilter 登录认证
来源:互联网 发布:斗鱼直播平台源码 编辑:程序博客网 时间:2024/05/05 19:48
UsernamePasswordAuthenticationFilter本身不是过滤器,而是继承了AbstractAuthenticationProcessingFilter才拥有过滤器的性能。
认证过程如下
一、先判断请求(请求必须是post请求)地址是否为/j_spring_security_check,如果不是,则放行,进入下一个过滤器,是则进行校验。
二、验证用户密码信息并返回Authentication类,在验证过程中如果失败则捕获异常进行处理(执行unsuccessfulAuthentication方法调转到配置中的错误链接),如果验证成功,则将调用SessionAuthenticationStrategy中的方法onAuthentication()判断用户能否重复登陆,是否二次登陆——根据你的配置文件决定,如果用户登陆满足条件则再执行successfulAuthentication(配置中的验证成功链接),如果失败则还是执行unsuccessfulAuthentication方法
详细说明:
1、用户密码认证过程
AbstractAuthenticationProcessingFilter调用UsernamePasswordAuthenticationFilter中的attemptAuthentication方法,将表单请求的信息(用户、密码等信息)赋值给UsernamePasswordAuthenticationToken(authRequest),然后调用AbstractAuthenticationProcessingFilter中的AuthenticationManager类中的(默认是ProviderManager类)方法——getAuthenticationManager().authenticate(authRequest)对用户密码的正确性进行验证,认证失败就抛出异常,成功就返回Authentication对象。
AuthenticationManager说明:顾名思义就是认证管理器,用于管理使用什么AuthenticationProvider验证用户的合法性,它拥有2个重要的参数类:
List<AuthenticationProvider> providers;AuthenticationManager parent
其中providers是AuthenticationProvider(接口,认证实现者)的集合
AuthenticationProvider的作用:可以处理一个特定的Authentication,实现身份验证。它有2个方法
Authentication authenticate(Authentication authentication) throws AuthenticationException;(根据authentication信息实现身份验证)
boolean supports(Class<?> authentication); 用于判断该AnonymousAuthenticationProvider能够验证某种类型的Authentication
说明:Authentication可以拥有多种类型,不同的类型使用不同的AuthenticationProvider
比如DaoAuthenticationProvider支持使用UsernamePasswordAuthenticationToken,AnonymousAuthenticationProvider支持AnonymousAuthenticationToken等
AuthenticationManager的方法authenticate执行逻辑如下(以DaoAuthenticationProvider为例):
(1)、判断是否有Authentication 对应的AuthenticationProvider,有就执行AuthenticationProvider的authenticate方法,没有就获取父类AuthenticationManager,查看父类中是否有Authentication 对应的AuthenticationProvider,如果也没有则抛出ProviderNotFoundException异常
(2)执行AuthenticationProvider的authenticate方法
1、根据输入名,查看缓存中是否已经有有用户实体对象,如果有,则对密码重新验证;如果没有,则进行用户的信息验证,及执行DaoAuthenticationProvider中的retrieveUser方法,获取一个UserDetails对象,如果UserDetails对象为null或者获取时出错就抛出异常UsernameNotFoundException或AuthenticationServiceException异常,然后一些属性验证之后,对用户密码进行验证。
2、给新获取的UserDetails对象放入缓存中
3、新建一个UsernamePasswordAuthenticationToken一个对象,将authenticated设为true(原来传入的UsernamePasswordAuthenticationToken对象authenticated为false)并返回
2、SessionAuthenticationStrategy.onAuthentication处理过程(主要处理一个用户是否可以同时多次登陆)
1、checkAuthenticationAllowed方法
根据maximumSessions和exceptionIfMaximumExceeded的设置判断用户是否多次登陆,是否超过maximumSessions同时登陆了,是否限制用户二次登陆(限制的话则第二次登陆的时候会抛出SessionAuthenticationException异常)还是第二次登陆使第一次登陆无效
2、复制一个新的session,拥有新的sessionID
3、更新SessionRegistry中的ConcurrentMap<Object,Set<String>> principals和Map<String, SessionInformation> sessionIds,这个是在第三个过滤器中ConcurrentSessionFilter需要使用的
- UsernamePasswordAuthenticationFilter 登录认证
- UsernamePasswordAuthenticationFilter
- UsernamePasswordAuthenticationFilter
- 用户登录:Spring Security3源码分析-UsernamePasswordAuthenticationFilter分析
- 登录认证
- sqlserver 登录认证
- 关于登录认证过程
- windows 登录认证函数
- 操作系统认证登录Oracle
- oracle登录认证知识
- C# LDAP认证登录
- 单点登录、统一认证
- Oracle 登录认证相关
- cookie自动登录认证
- Windows认证登录
- C# LDAP认证登录
- C# LDAP认证登录
- SSH登录认证详解
- RenderQueue 与 ZTest的区别
- 4.Median of Two Sorted Arrays
- PHP--面向对象
- NHibernate使用入门(四)
- 新人报到
- UsernamePasswordAuthenticationFilter 登录认证
- SublimeText3常用快捷操作
- 解决锁表问题
- 新人报到
- 大型网站架构之分布式消息队列
- 修改文件输出目录后,提示配置错误(VS2010)
- Oracle11g for linux
- 新人报到
- iOS--js和oc交互