spring auth 核心流程详解
核心流程图Token 颁发阶段 常用扩展点说明按流程顺序序号扩展点配置方式典型用途代码示例位置1ClientDetailsServiceclients.withClientDetails(customService)从数据库、Redis 等动态加载客户端信息支持客户端动态注册AuthorizationServerConfig.configure(ClientDetailsServiceConfigurer)2TokenGranterendpoints.tokenGranter(customGranter)自定义 grant_type如短信验证码、手机号一键登录AuthorizationServerEndpointsConfigurer.tokenGranter()3UserDetailsServiceendpoints.userDetailsService(customService)自定义用户数据源非默认的loadUserByUsername在AuthorizationServerEndpointsConfigurer中注入你的代码中注释了该行4AuthorizationCodeServicesendpoints.authorizationCodeServices(customService)自定义授权码的存储方式默认是内存可改为 Redis、数据库AuthorizationServerEndpointsConfigurer.authorizationCodeServices()5TokenStoreendpoints.tokenStore(customStore)自定义 Token 存储策略Redis、JWT、JDBCAuthorizationServerEndpointsConfigurer.tokenStore()6TokenEnhancerendpoints.tokenEnhancer(customEnhancer)向 AccessToken 添加额外信息如用户 ID、租户 ID或生成 OIDC 的id_token你代码中Bean TokenEnhancer7WebResponseExceptionTranslatorendpoints.exceptionTranslator(customTranslator)统一异常响应的格式例如统一返回 JSON 格式的错误信息AuthorizationServerEndpointsConfigurer.exceptionTranslator() 总结核心流程从客户端请求/oauth/token开始 → 客户端认证 → 用户认证若需要→ 生成原始 Token → Token 增强 → 存储 → 返回。最常用的扩展点TokenStore切换 JWT / Redis 存储。TokenEnhancer自定义 Token 内容。ClientDetailsService让客户端配置可动态管理。WebResponseExceptionTranslator美化异常响应。