博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ranger通过web界面登录用户验证类UsernamePasswordAuthenticationFilter
阅读量:4045 次
发布时间:2019-05-24

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

UsernamePasswordAuthenticationFilterAbstractAuthenticationProcessingFilter 的子类,主要作用是对用户身份信息的验证。

  关于 AbstractAuthenticationProcessingFilter 的分析见此: 。

继承关系

UsernamePasswordAuthenticationFilter 继承自AbstractAuthenticationProcessingFilter

public class UsernamePasswordAuthenticationFilter extends        AbstractAuthenticationProcessingFilter {

AbstractAuthenticationProcessingFilter 是处理 form 登陆的过滤器,与 form 登陆有关的所有操作都是在该类中及其子类中进行的。

流程分析

关于 UsernamePasswordAuthenticationFilter 处理请求的大体流程和其父类一致,见此: 。该处主要分析UsernamePasswordAuthenticationFilter 实现的父类方法 attemptAuthentication() 中的用户身份验证逻辑。

  attemptAuthentication() 方法代码如下所示:

public Authentication attemptAuthentication(HttpServletRequest request,            HttpServletResponse response) throws AuthenticationException {        if (postOnly && !request.getMethod().equals("POST")) {            throw new AuthenticationServiceException(                    "Authentication method not supported: " + request.getMethod());        }-->1    String username = obtainUsername(request);-->2    String password = obtainPassword(request);        if (username == null) {            username = "";        }        if (password == null) {            password = "";        }        username = username.trim();-->3    UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(                username, password);        // Allow subclasses to set the "details" property        setDetails(request, authRequest);-->4    return this.getAuthenticationManager().authenticate(authRequest);    }
  • -->1-->2 处的代码从 request 中提取用户名和密码。
  • -->3 处代码为构建类UsernamePasswordAuthenticationToken 成员变量,UsernamePasswordAuthenticationToken 是一个用户信息的载体类,用来存储及传递用户名和用户密码。
  • -->4 处代码调用getAuthenticationManager()方法获取AuthenticationManager实例,getAuthenticationManager()方法定义如下:
protected AuthenticationManager getAuthenticationManager() {        return authenticationManager;    }

然后调用获取到的AuthenticationManager实例的authenticate()方法对封装在authRequest [UsernamePasswordAuthenticationToken]中的用户名及密码进行验证。

注意: AuthenticationManager这里可以理解为 spring security 配置文件中 <authentication-manager/> 的实现类。

attemptAuthentication 验证函数流程图.png

转载地址:http://nhwci.baihongyu.com/

你可能感兴趣的文章
mongodb mongoexprt 导出数据 json csv格式
查看>>
MySQL MERGE存储引擎 简
查看>>
数据库分片(Sharding)与分区(Partition)的区别
查看>>
node.js递归打印文件目录、文件名
查看>>
本地与远程linux上传下载
查看>>
NodeJS的代码调试和性能调优
查看>>
浅谈V8引擎中的垃圾回收机制
查看>>
引擎V8及优化技术
查看>>
Node.js domain异常捕获的一些实践
查看>>
mac下修改环境变量
查看>>
JVM性能调优
查看>>
JVM调优总结
查看>>
Redis与Memcached的区别
查看>>
WebSocket(1)-- WebSocket API简介
查看>>
WebSocket(2)--为什么引入WebSocket协议
查看>>
WebSocket(3)-- WebSocket协议简介
查看>>
WebSocket(4)-- WebSocket与TCP、Http的关系
查看>>
TCP/IP, WebSocket 和 MQTT
查看>>
CentOS、Ubuntu、Debian三个linux比较异同
查看>>
javascript闭包和闭包的几种写法及用途
查看>>