认证源码分析与自定义后端认证逻辑

简介: 本文分析Spring Security认证流程,核心为`UsernamePasswordAuthenticationFilter`过滤器,负责提取用户名密码并封装成`UsernamePasswordAuthenticationToken`,交由`AuthenticationManager`处理。后者通过`ProviderManager`遍历`AuthenticationProvider`链完成认证,支持多种认证方式扩展,是安全框架的核心机制。

1.认证流程分析

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;
        }
    }
}
相关文章
|
5月前
|
XML Java 数据格式
Spring @Configuration 注解详解:用 Java 代码替代 XML 配置
`@Configuration` 是 Spring 实现 Java 配置的核心注解,替代传统 XML,通过 `@Bean` 注册 Bean,结合 `@Import`、`@ComponentScan` 等实现类型安全、可维护的配置方式,推动 Spring 应用现代化。
|
5月前
|
SQL Java 数据库连接
MyBatis 分页机制详解:从 RowBounds 到物理分页实践
MyBatis分页策略解析:逻辑分页(RowBounds)将全量数据加载至内存,仅适用于小数据量;物理分页通过SQL层面限制返回数据,性能更优。推荐使用PageHelper插件,自动适配数据库方言,一行代码实现高效分页,避免OOM风险,提升系统稳定性。
|
5月前
|
NoSQL 前端开发 BI
MongoDB 分页、排序与统计查询技巧
MongoDB 提供 `count()`、`sort()`、`skip()`、`limit()` 等链式方法,高效实现数据统计、分页与排序。支持多字段排序、条件分页,执行顺序固定为:先排序 → 再跳过 → 后限制数量,适用于各类业务场景的数据查询处理。(239字符)
|
5月前
|
JSON 前端开发 安全
用自定义注解 + 拦截器实现登录鉴权
通过自定义注解 `@Login` 结合 Spring 拦截器,实现声明式登录校验。无需重复编码,自动拦截未登录请求,提升代码可维护性与安全性,适用于前后端分离架构的权限控制实践。
|
5月前
|
缓存 前端开发 JavaScript
Spring Boot 中使用 Thymeleaf:从页面渲染到数据绑定
Thymeleaf是Spring Boot推荐的模板引擎,HTML即原型又可动态渲染,提升前后端协作效率。支持页面跳转、数据绑定、列表遍历、条件显示等常用功能,开发时关闭缓存可实时预览。静态页可直接浏览,后端注入数据无缝衔接,助力高效开发。
|
5月前
|
XML Java 数据格式
Spring Boot 是什么?—— 简化 Spring 开发的革命性工具
自2002年Spring框架兴起,企业级Java开发迎来变革。但繁重的XML配置、复杂集成与外部容器依赖等问题凸显。2013年,Spring Boot应运而生,以自动配置、起步依赖、内嵌服务器等特性,实现“约定优于配置”,极大提升开发效率。它非替代Spring,而是其现代化加速器,让开发者专注业务,成为微服务与云原生时代的首选基石。
|
5月前
|
Java Spring
Spring Boot 中的 @RequestParam:获取查询参数与表单数据
`@RequestParam` 用于提取 HTTP 请求中的查询参数或表单数据,支持设置参数名、是否必填及默认值。适用于 `?key=value` 形式或 POST 表单,字段多时推荐封装为实体类自动绑定,与 `@PathVariable` 按路径取值不同,用途明确,是处理 Web 请求的常用方式。
|
5月前
|
机器学习/深度学习 Java 数据库连接
MyBatis 关联映射详解:1:1、1:N、N:1 与 N:N 实现方式
本文详解 MyBatis 中 resultMap 的四种关联映射:一对一、一对多、多对一和多对多,结合代码示例讲解实现方式与最佳实践,助你掌握复杂对象关系的数据映射技巧。
|
5月前
|
Java API Spring
Spring Boot 中的 @PathVariable:获取 RESTful 路径参数
在Spring Boot中,`@PathVariable`用于提取RESTful API URL中的动态参数。支持单个或多个路径变量,参数名一致时可省略value属性,不一致时需显式绑定,是构建清晰、语义化接口的关键注解之一。
|
5月前
|
存储 NoSQL 定位技术
MongoDB 索引知识详解:提升查询性能的核心利器
MongoDB索引是提升查询性能的核心机制,通过B树结构实现快速定位数据,避免全集合扫描。支持单字段、复合、地理空间、文本及哈希索引,合理使用可将查询效率从秒级降至毫秒级,但需避免过度索引影响写入性能。

热门文章

最新文章