认证流程分析

简介: `UsernamePasswordAuthenticationFilter` 是 Spring Security 处理表单登录的核心过滤器,拦截 `/login` 的 POST 请求,提取用户名密码并封装为 `UsernamePasswordAuthenticationToken`,交由 `AuthenticationManager` 认证。认证成功后将结果存入 `SecurityContext`,支持“记住我”及后续处理,失败则清空上下文并调用失败处理器。

UsernamePasswordAuthenticationFilter
先看主要负责认证的过滤器UsernamePasswordAuthenticationFilter,有删减,注意注释。
public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter
{
public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "username";
public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "password";
private String usernameParameter = "username";
private String passwordParameter = "password";
private boolean postOnly = true;

public UsernamePasswordAuthenticationFilter() {
    super(new AntPathRequestMatcher("/login", "POST"));
}

public Authentication attemptAuthentication(HttpServletRequest request, 
                                            HttpServletResponse response) throws AuthenticationException {
    //必须为POST请求
    if (this.postOnly && !request.getMethod().equals("POST")) {
        throw new AuthenticationServiceException("Authentication method not supported: " +
                                                 request.getMethod());
    } else {

        String username = this.obtainUsername(request);
        String password = this.obtainPassword(request);

        if (username == null) {
            username = "";
        }

        if (password == null) {
            password = "";
        }

        username = username.trim();

        //将填写的用户名和密码封装到了UsernamePasswordAuthenticationToken中
        UsernamePasswordAuthenticationToken authRequest = new
        UsernamePasswordAuthenticationToken(username, password);

        this.setDetails(request, authRequest);
        //调用AuthenticationManager对象实现认证
        return this.getAuthenticationManager().authenticate(authRequest);
    }
}

}
AuthenticationManager
由上面源码得知,真正认证操作在AuthenticationManager里面!
public class ProviderManager implements AuthenticationManager, MessageSourceAware,
InitializingBean {

private static final Log logger = LogFactory.getLog(ProviderManager.class);
private AuthenticationEventPublisher eventPublisher;
private List<AuthenticationProvider> providers;
protected MessageSourceAccessor messages;
private AuthenticationManager parent;
private boolean eraseCredentialsAfterAuthentication;

//注意AuthenticationProvider这个对象,SpringSecurity针对每一种认证,什么qq登录啊,
//用户名密码登陆啊,微信登录啊都封装了一个AuthenticationProvider对象。
public ProviderManager(List<AuthenticationProvider> providers) {
    this(providers, (AuthenticationManager)null);
}

public Authentication authenticate(Authentication authentication) throws
AuthenticationException {

    Class<? extends Authentication> toTest = authentication.getClass();
    AuthenticationException lastException = null;
    AuthenticationException parentException = null;
    Authentication result = null;
    Authentication parentResult = null;
    boolean debug = logger.isDebugEnabled();
    Iterator var8 = this.getProviders().iterator();

    //循环所有AuthenticationProvider,匹配当前认证类型。
    while(var8.hasNext()) {
        AuthenticationProvider provider = (AuthenticationProvider)var8.next();
        if (provider.supports(toTest)) {
            if (debug) {
                logger.debug("Authentication attempt using " +
                             provider.getClass().getName());
            }
            try {
                //找到了对应认证类型就继续调用AuthenticationProvider对象完成认证业务。
                result = provider.authenticate(authentication);
                if (result != null) {
                    this.copyDetails(authentication, result);
                    break;
                }
            } catch (AccountStatusException var13) {
                this.prepareException(var13, authentication);
                throw var13;
            } catch (InternalAuthenticationServiceException var14) {
                this.prepareException(var14, authentication);
                throw var14;
            } catch (AuthenticationException var15) {
                lastException = var15;
            }
        }
    }

    if (result == null && this.parent != null) {
        try {
            result = parentResult = this.parent.authenticate(authentication);
        } catch (ProviderNotFoundException var11) {
        } catch (AuthenticationException var12) {
            parentException = var12;
            lastException = var12;
        }
    }

    if (result != null) {
        if (this.eraseCredentialsAfterAuthentication && result instanceof
            CredentialsContainer) {
            ((CredentialsContainer)result).eraseCredentials();
        }
        if (parentResult == null) {
            this.eventPublisher.publishAuthenticationSuccess(result);
        }
        return result;
    } else {
        if (lastException == null) {
            lastException = new
            ProviderNotFoundException(this.messages.getMessage("ProviderManager.providerNotFound", new
                                                               Object[]{toTest.getName()}, "No AuthenticationProvider found for {0}"));
        }
        if (parentException == null) {
            this.prepareException((AuthenticationException)lastException, authentication);
        }
        throw lastException;
    }
}

}
AbstractUserDetailsAuthenticationProvider
咱们继续再找到AuthenticationProvider的实现类AbstractUserDetailsAuthenticationProvider:
public class DaoAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
private static final String USER_NOT_FOUND_PASSWORD = "userNotFoundPassword";
private PasswordEncoder passwordEncoder;
private volatile String userNotFoundEncodedPassword;
private UserDetailsService userDetailsService;
private UserDetailsPasswordService userDetailsPasswordService;
protected final UserDetails retrieveUser(String username,
UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
this.prepareTimingAttackProtection();
try {
//重点来了!主要就在这里了!
//可别忘了,咱们为什么要翻源码,是想用自己数据库中的数据实现认证操作啊!
//UserDetails就是SpringSecurity自己的用户对象。
//this.getUserDetailsService()其实就是得到UserDetailsService的一个实现类
//loadUserByUsername里面就是真正的认证逻辑
//也就是说我们可以直接编写一个UserDetailsService的实现类,告诉SpringSecurity就可以了!
//loadUserByUsername方法中只需要返回一个UserDetails对象即可
UserDetails loadedUser = this.getUserDetailsService().loadUserByUsername(username);
//若返回null,就抛出异常,认证失败。
if (loadedUser == null) {
throw new InternalAuthenticationServiceException("UserDetailsService returned
null, which is an interface contract violation");
} else {
//若有得到了UserDetails对象,返回即可。
return loadedUser;
}
} catch (UsernameNotFoundException var4) {
this.mitigateAgainstTimingAttack(authentication);
throw var4;
} catch (InternalAuthenticationServiceException var5) {
throw var5;
} catch (Exception var6) {
throw new InternalAuthenticationServiceException(var6.getMessage(), var6);
}
}
}
AbstractUserDetailsAuthenticationProvider
按理说到此已经知道自定义认证方法的怎么写了,但咱们把返回的流程也大概走一遍,上面不是说到返回了一个 UserDetails对象对象吗?
跟着它就又回到AbstractUserDetailsAuthenticationProvider对象中authenticate方法最后一行了。
public abstract class AbstractUserDetailsAuthenticationProvider implements
AuthenticationProvider, InitializingBean, MessageSourceAware {

public Authentication authenticate(Authentication authentication) throws
AuthenticationException {
    //最后一行返回值,调用了createSuccessAuthentication方法,此方法就在下面!
    return this.createSuccessAuthentication(principalToReturn, authentication, user);
}

//咿!?怎么又封装了一次UsernamePasswordAuthenticationToken,开局不是已经封装过了吗?
protected Authentication createSuccessAuthentication(Object principal, 
                                                     Authentication authentication, 
                                                     UserDetails user) {
    //那就从构造方法点进去看看,这才干啥了。
    UsernamePasswordAuthenticationToken result = new
    UsernamePasswordAuthenticationToken(principal, authentication.getCredentials(),
                                        this.authoritiesMapper.mapAuthorities(user.getAuthorities()));
    result.setDetails(authentication.getDetails());
    return result;
}

}
UsernamePasswordAuthenticationToken
来到UsernamePasswordAuthenticationToken对象发现里面有两个构造方法
public class UsernamePasswordAuthenticationToken extends AbstractAuthenticationToken {

private static final long serialVersionUID = 510L;
private final Object principal;
private Object credentials;

//认证成功前,调用的是这个带有两个参数的。
public UsernamePasswordAuthenticationToken(Object principal, 
                                           Object credentials) {
    super((Collection)null);
    this.principal = principal;
    this.credentials = credentials;
    this.setAuthenticated(false);
}

//认证成功后,调用的是这个带有三个参数的。
public UsernamePasswordAuthenticationToken(Object principal, 
                                           Object credentials,
                                           Collection<? extends GrantedAuthority> authorities) {
    //看看父类干了什么!
    super(authorities);
    this.principal = principal;
    this.credentials = credentials;
    super.setAuthenticated(true);
}

}
AbstractAuthenticationToken
再点进去super(authorities)看看:
public abstract class AbstractAuthenticationToken implements Authentication,
CredentialsContainer {

private final Collection<GrantedAuthority> authorities;
private Object details;
private boolean authenticated = false;

public AbstractAuthenticationToken(Collection<? extends GrantedAuthority> authorities) {

    //这时两个参数那个分支!
    if (authorities == null) {
        this.authorities = AuthorityUtils.NO_AUTHORITIES;
    } else {
        //三个参数的,看这里!
        Iterator var2 = authorities.iterator();
        //原来是多个了添加权限信息的步骤
        GrantedAuthority a;
        do {
            if (!var2.hasNext()) {
                ArrayList<GrantedAuthority> temp = new ArrayList(authorities.size());
                temp.addAll(authorities);
                this.authorities = Collections.unmodifiableList(temp);
                return;
            }
            a = (GrantedAuthority)var2.next();
        } while(a != null);
        //若没有权限信息,是会抛出异常的!
        throw new IllegalArgumentException("Authorities collection cannot contain any null
        elements");
    }
}

}
由此,咱们需要牢记自定义认证业务逻辑返回的UserDetails对象中一定要放置权限信息! 现在可以结束源码分析了?先不要着急! 咱们回到最初的地方UsernamePasswordAuthenticationFilter,你看好看了,这可是个过滤器,咱们分析这么 久,都没提到doFilter方法,你不觉得心里不踏实?可是这里面也没有doFilter呀?那就从父类找!
AbstractAuthenticationProcessingFilter
点开AbstractAuthenticationProcessingFilter,删掉不必要的代码!
public abstract class AbstractAuthenticationProcessingFilter
extends GenericFilterBean
implements ApplicationEventPublisherAware, MessageSourceAware {

//doFilter再次!
public void doFilter(ServletRequest req, 
                     ServletResponse res, 
                     FilterChain chain) throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest)req;
    HttpServletResponse response = (HttpServletResponse)res;

    if (!this.requiresAuthentication(request, response)) {
        chain.doFilter(request, response);
    } else {

        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Request is to process authentication");
        }

        Authentication authResult;
        try {
            authResult = this.attemptAuthentication(request, response);
            if (authResult == null) {
                return;
            }
            this.sessionStrategy.onAuthentication(authResult, request, response);
        } catch (InternalAuthenticationServiceException var8) {
            this.logger.error("An internal error occurred while trying to authenticate the
            user.", var8);
            this.unsuccessfulAuthentication(request, response, var8);
            return;
        } catch (AuthenticationException var9) {
            this.unsuccessfulAuthentication(request, response, var9);
            return;
        }
        if (this.continueChainBeforeSuccessfulAuthentication) {
            chain.doFilter(request, response);
        }
        this.successfulAuthentication(request, response, chain, authResult);
    }
}

protected boolean requiresAuthentication(HttpServletRequest request, 
                                         HttpServletResponse response) {
    return this.requiresAuthenticationRequestMatcher.matches(request);
}

//成功走successfulAuthentication
protected void successfulAuthentication(HttpServletRequest request, 
                                        HttpServletResponse response, 
                                        FilterChain chain, 
                                        Authentication authResult) throws IOException, ServletException {

    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Authentication success. Updating SecurityContextHolder to
                          contain: " + authResult);
    }

    //认证成功,将认证信息存储到SecurityContext中!
    SecurityContextHolder.getContext().setAuthentication(authResult);
    //登录成功调用rememberMeServices
    this.rememberMeServices.loginSuccess(request, response, authResult);
    if (this.eventPublisher != null) {
        this.eventPublisher.publishEvent(new
                                         InteractiveAuthenticationSuccessEvent(authResult, this.getClass()));
    }
    this.successHandler.onAuthenticationSuccess(request, response, authResult);
}

//失败走unsuccessfulAuthentication
protected void unsuccessfulAuthentication(HttpServletRequest request, 
                                          HttpServletResponse response, 
                                          AuthenticationException failed) throws IOException, ServletException {
    SecurityContextHolder.clearContext();
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Authentication request failed: " + failed.toString(), failed);
        this.logger.debug("Updated SecurityContextHolder to contain null Authentication");
        this.logger.debug("Delegating to authentication failure handler " +
                          this.failureHandler);
    }
    this.rememberMeServices.loginFail(request, response);
    this.failureHandler.onAuthenticationFailure(request, response, failed);
}

}
可见AbstractAuthenticationProcessingFilter这个过滤器对于认证成功与否,做了两个分支,成功执行 successfulAuthentication,失败执行unsuccessfulAuthentication。
在successfulAuthentication内部,将认证信息存储到了SecurityContext中。并调用了loginSuccess方法,这就是常见的“记住我”功能!此功能具体应用,咱们后续再研究!

相关文章
|
NoSQL Redis
若依管理系统去掉Redis相关配置
若依管理系统去掉Redis相关配置
|
25天前
|
存储 人工智能 运维
阿里云OSS对象存储服务平台新版功能介绍:对象+向量+表格三桶合一,AI原生多模态存储新底座
阿里云对象存储OSS作为全球领先的海量、安全、低成本、高持久的云存储服务,新版在**多模态存储架构、AI原生能力、数据管理、安全合规、自动化运维、生态集成**六大方向实现全面升级,构建起“对象桶+向量桶+表格桶”三位一体的完整产品家族,成为覆盖非结构化数据、向量数据与结构化数据的AI原生统一存储底座。新版依托全球2800+边缘节点、99.9999%数据可靠性、EB级弹性扩展能力,为图片、视频、文档、日志、AI数据集、结构化数据等全类型数据提供一站式存储、管理、分析与计算服务,彻底解决传统存储“容量有限、类型单一、管理复杂、成本高昂、AI能力缺失”等痛点,全面满足企业数字化转型、AI应用落地、数
209 4
|
3月前
|
消息中间件 安全 Java
阿里云短信服务从入门到精通:完整对接使用指南
本文提供了一份完整的阿里云短信服务对接使用指南。首先需完成账号注册与企业实名认证,开通短信服务并购买资源包或确保账户余额充足。接着需依次申请短信资质、签名和模板,三者均审核通过后方可发送短信。发送方式支持控制台群发助手与API/SDK程序化调用,推荐使用RAM子账号AccessKey保障安全。文章详细介绍了Java和Python两种语言的SDK集成示例,涵盖SendSms接口调用、参数配置与异常处理。同时讲解了安全与限流配置、回执消息接收、常见错误码排查以及成本优化策略,帮助开发者快速、安全、低成本地完成短信服务对接。
|
网络协议 Java 中间件
2024年最新阿里Java高级岗200+面试题,掌握80%进阿里没问题
更新的阿里集团Java岗JD标准,信息来源于阿里集团的招聘专场,包括天猫、蚂蚁金服、中间件团队的高级Java、技术专家岗位。文末随附BATJTMD等一线互联网企业的Java高级研发岗位的面试题目及答案。
|
Java 应用服务中间件 Maven
Maven - 两种Tomcat插件的配置
Maven - 两种Tomcat插件的配置
1428 0
|
Java API Spring
Spring Boot 2.x基础教程:Swagger接口分类与各元素排序问题详解
特别开一篇详细说说Swagger中文档内容如何来组织以及其中各个元素如何控制前后顺序的具体配置方法。
3822 0
Spring Boot 2.x基础教程:Swagger接口分类与各元素排序问题详解
|
数据库
产品需求文档(PRD)的写作方法之笔记一
1、写前准备(思维导图): http://www.woshipm.com/?p=80070 1.在写之前,请先很区分清楚什么是MRD文档(市场需求文档),BRD文档(商业需求文档),什么是PRD文档(产品需求文档) 可查阅知乎https://www.zhihu.com/question/19655491 2.规划产品的思维导图(信息结构图)   在写作这份文档前,我们需要先做一些准备,把BRD、MRD的相关需求消化并融合规划出产品的结构图。
2664 0
|
人工智能 IDE 定位技术
通义灵码 AI IDE 正式上线,智能体自动写代码,首创自动记忆,工程感知全面升级
阿里云发布的通义灵码AI IDE深度适配千问3大模型,集成智能编码助手功能,支持编程智能体、工具调用、工程感知等能力。其核心亮点包括:支持最强开源模型千问3,全面集成通义灵码插件能力,自带编程智能体模式,支持长期记忆与行间建议预测(NES)。通义灵码已覆盖主流IDE,助力开发者实现高效智能编程,插件下载量超1500万,生成代码超30亿行,成为国内最受欢迎的辅助编程工具。立即体验更智能的开发流程!
3548 1
|
Java 数据处理
|
JSON Java Nacos
需要在你的项目中引入Nacos的依赖
需要在你的项目中引入Nacos的依赖
1068 2

热门文章

最新文章