SpringSecurity源码解析-UsernamePassw

2018-12-04  本文已影响0人  圆滚滚_8e70

package org.springframework.security.web.authentication

类图-总体结构

UsernamePasswordAutherticationFilter类图

结构说明

从类图上看,UsernamePasswordAuthenticationFilter这个类继承了AbstractAuthenticationProcessingFilter

关于AbstractAuthenticationProcessingFilter类的详解参考文章 源码解析-AbstractAuthenticationProcessingFilter

说明

看类结构UsernamePasswordAuthenticationFilter应该是对类AbstractAuthenticationProcessingFilter的补充和实现。

构造方法说明

1.public UsernamePasswordAuthenticationFilter()
构造方法,具体实现如下:

public UsernamePasswordAuthenticationFilter() {
       //调用了父类的构造函数,默认采用/login,且使用POST方法;
        super(new AntPathRequestMatcher("/login", "POST"));
    }

实例方法说明

1.public Authentication attemptAuthentication(HttpServletRequest,HttpServletResponse)

public Authentication attemptAuthentication(HttpServletRequest request,
            HttpServletResponse response) throws AuthenticationException {
        //如果postOnly为true,并且request的请求方式不是"POST",那么抛出认证异常.
        if (postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException(
                    "Authentication method not supported: " + request.getMethod());
        }
        //获取用户名和密码
        String username = obtainUsername(request);
        String password = obtainPassword(request);
        //如果用户名为空,则设置默认值
        if (username == null) {
            username = "";
        }
        //如果密码为空,则设置默认值
        if (password == null) {
            password = "";
        }
        //对用户名进行去空格处理
        username = username.trim();
        //根据用户名和密码创建用户名和密码认证token
        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
                username, password);
        //设置认证详细信息
        // Allow subclasses to set the "details" property
        setDetails(request, authRequest);
        //获取认证管理处理类,并进行认证,返回认证结果
        return this.getAuthenticationManager().authenticate(authRequest);
    }

整体流程图


尝试认证流程图
  1. protected String obtainPassword(HttpServletRequest)
protected String obtainPassword(HttpServletRequest request) {
        //从Http的request中获取key为password的字段,获取用户输入的密码
        return request.getParameter(passwordParameter);
    }

3.···protected String obtainUsername(HttpServletRequest)```

protected String obtainUsername(HttpServletRequest request) {
        //从request中获取用户名,获取用户输入的用户名
        return request.getParameter(usernameParameter);
    }

4.setDetails(HttpServletRequest,UsernamePasswordAuthenicationToken)

/**
     * Provided so that subclasses may configure what is put into the authentication
     * request's details property.
     *
     * @param request that an authentication request is being created for
     * @param authRequest the authentication request object that should have its details
     * set
     */
    protected void setDetails(HttpServletRequest request,
            UsernamePasswordAuthenticationToken authRequest) {
        //补充authRequest-认证请求,默认为WebAuthenticationDetails(内带ip和sessionId)
        authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
    }

5.public void setUsernameParameter(String)

/**
     * Sets the parameter name which will be used to obtain the username from the login
     * request.
     *
     * @param usernameParameter the parameter name. Defaults to "username".
     */
    public void setUsernameParameter(String usernameParameter) {
        Assert.hasText(usernameParameter, "Username parameter must not be empty or null");
        this.usernameParameter = usernameParameter;
    }

6.public void setPasswordParameter(String)

/**
     * Sets the parameter name which will be used to obtain the password from the login
     * request..
     *
     * @param passwordParameter the parameter name. Defaults to "password".
     */
    public void setPasswordParameter(String passwordParameter) {
        Assert.hasText(passwordParameter, "Password parameter must not be empty or null");
        this.passwordParameter = passwordParameter;
    }

7.public void setPostOnly(boolean)

/**
     * Defines whether only HTTP POST requests will be allowed by this filter. If set to
     * true, and an authentication request is received which is not a POST request, an
     * exception will be raised immediately and authentication will not be attempted. The
     * <tt>unsuccessfulAuthentication()</tt> method will be called as if handling a failed
     * authentication.
     * <p>
     * Defaults to <tt>true</tt> but may be overridden by subclasses.
     */
    public void setPostOnly(boolean postOnly) {
        this.postOnly = postOnly;
    }

8.public String getUsernameParameter()

public final String getUsernameParameter() {
        return usernameParameter;
    }

9.public String getPasswordParameter()

public final String getPasswordParameter() {
        return passwordParameter;
    }
上一篇 下一篇

猜你喜欢

热点阅读