# spring-oauth2-server **Repository Path**: yanglong.com/spring-oauth2-server ## Basic Information - **Project Name**: spring-oauth2-server - **Description**: 基于Spring Security oAuth2实现的认证中心 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-08-03 - **Last Updated**: 2022-05-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Oauth2授权服务器 使用Spring原生JWT功能,提供认证服务。 ## 扩展思路 ### 4种认证方式之外的扩展 扩展认证类型,即新的TokenGranter和AuthenticationProvider,加入到认证链中,扩展认证方式, ### 不扩展4种认证方式,但是扩展短信验证码,openId等登录方式 只实现AuthenticationProvider,此provider要支持UsernamePasswordAuthenticationToken的认证,前端在请求头或参数中加入特殊值,后端在此AuthenticationProvider中实现特殊值对应的认证逻辑。 核心原理是在AuthenticationProvider中获取到ServletRequest,然后获取到特殊值: > HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest() ## 关于防护 防刷等设置,可以使用filter,在请求刚进入时即拦截请求,进行防刷验证,验证通过后,再进行认证流程。 ### 授权码模式 浏览器访问: > http://localhost:8080/oauth/authorize?client_id=sample&response_type=code 会跳转到百度首页,获取地址栏的code。 然后访问: > curl --location --request POST 'http://localhost:8080/oauth/token?grant_type=authorization_code&username=zhangsan&password=123456&code=3Uhz4l' --header 'Authorization: Basic c2FtcGxlOnNhbXBsZQ==' 获取token。 ## password模式 直接访问: > curl --location --request POST 'http://localhost:8080/oauth/token?grant_type=password&username=zhangsan&password=123456' --header 'Authorization: Basic c2FtcGxlOnNhbXBsZQ==' 获取token。 ## 扩展的sms模式获取token > curl --location --request POST 'http://localhost:8080/oauth/token?grant_type=sms&mobile=13626878988&validate=8888' --header 'Authorization: Basic c2FtcGxlOnNhbXBsZQ==' ## 支持使用用户名密码登录认证获取token和refresh_token ## 网页token过期 网页token过期处理,如果token失效期和需求失效期不一致,可以为token设置一个唯一值key,然后以H5:LOGIN:_ 作为key存入redis,使用redis来控制,在redis失效期内访问,刷新1/2或1倍失效时间。如果已失效,返回登录。