UsernamePasswordAuthenticationFi

2017-05-11  本文已影响0人  凌云v

UsernamePasswordAuthenticationFilterAbstractAuthenticationProcessingFilter 的子类,主要作用是对用户身份信息的验证。
  关于 AbstractAuthenticationProcessingFilter 的分析见此: AbstractAuthenticationProcessingFilter 源码分析

继承关系

UsernamePasswordAuthenticationFilter 继承自AbstractAuthenticationProcessingFilter

public class UsernamePasswordAuthenticationFilter extends
        AbstractAuthenticationProcessingFilter {

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

流程分析

关于 UsernamePasswordAuthenticationFilter 处理请求的大体流程和其父类一致,见此: AbstractAuthenticationProcessingFilter 源码分析。该处主要分析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);
    }
protected AuthenticationManager getAuthenticationManager() {
        return authenticationManager;
    }

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

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

attemptAuthentication 验证函数流程图.png

参考

上一篇 下一篇

猜你喜欢

热点阅读