# pap-oauth **Repository Path**: alexgaoyh/pap-oauth ## Basic Information - **Project Name**: pap-oauth - **Description**: pap oauth 操作。 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-09-18 - **Last Updated**: 2022-01-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #20190918 1、添加 pom 依赖: spring-cloud-starter-oauth2这个依赖应该已经包含了spring security相关的jar,但是spring-cloud-dependencies版本为Greenwich.RELEASE时, spring-security-oauth2-autoconfigure子module中引入的版本一直是2.1.0.M4,不管是重新import还是删除本地maven repository都不管用, 在官方的issue中也有人遇到的相同的问题, 所以以上依赖暂时使用单独引入spring-security-oauth2-autoconfigure,version还必须在子module中指定. org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-security org.springframework.security.oauth.boot spring-security-oauth2-autoconfigure 2.1.8.RELEASE 2、使用默认配置 1、 添加注解:你需要为我们启动类添加一个启动的注解 @EnableAuthorizationServer 通过一个注解就已经完成一个安全的授权的创建,运行查看输出日志。 查看日志,生成默认的用户密码、客户端ID与秘钥 2、携带生成的 client id 进行访问,如果没有添加项目上下文,直接访问 /oauth/authorize http://localhost:9100/oauth/authorize?response_type=code&client_id=&redirect_uri=http://www.pap.com&scope=all 访问有可能会抛出异常,缺少 配置 spring security, 则可以进行配置,可以使用默认配置: @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { } 再次运行,即可跳转到登录页面, 输入用户名密码,即可进行跳转,注意需要提前注册回调地址: security.oauth2.client.registered-redirect-uri=http://www.pap.com 启动日志中含有请求链接过程中需要的参数,替换 client_id, 用户名默认为 user, 输入日志中的 Using generated security password 用户密码, 访问链接后即可获取对应的 授权码 code , 即可通过 授权码 获取 token。 3、解析 token: check_token 需要添加 配置以支持 token 解析: security.oauth2.authorization.check-token-access=isAuthenticated() 4、访问资源(API): 携带token 进行 接口API 访问的过程中,需要开启 资源服务器, 即 增加 注解 @EnableResourceServer . 注意请求过程中,增加 Header : Authorization: bearer * 5、刷新token 要使用refresh_token的话,需要额外配置userDetailsService @Configuration public class OAuth2Config extends AuthorizationServerConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.userDetailsService(userDetailsService); } } TODO: 将配置文件中的 spring.security.user.name spring.security.user.password 两个部分,移动到 SecurityConfig 文件中,同时解决加密方式的问题。 #20190919 1、将 spring.security.user.name spring.security.user.password 部分的配置,移动至代码内部 1.1、注意 SecurityConfig 和 OauthConfig 中,都需要对 security 的用户进行配置维护; 1.2、增加 PapUserDetailsService 类文件,针对 OauthConfig 进行用户属性配置; 2、测试 oauth2 的四种 授权模式:授权码模式,客户端模式,简化模式,密码模式; 并测试通过; #20190920 1、Security 中的用户信息进行维护处理,不放置到内存中,而是通过 (PapUserDetailsService)UserDetailsManager.loadUserByUsername() 方法进行查询。 将 Security 和 Oauth 中的用户进行统一管理。 2、增加 自定义扩展的 SmsCode* 手机号验证码登录器。详见 authentication.mobile 包定义。 https://github.com/longfeizheng/logback 3、增加 AuthTokenAspect 切面编程,将 /oauth/token 方法的返回值进行额外处理,增加按照约定的返回值格式。 处理 oauth2 返回值 格式与约定不同的情况。 #20190921 1、增加统一的异常处理: 1.1、 AuthTokenAspect , 增加 切面编程,在返回 access_token refresh_token 时候,进行返回值格式的封装; 1.2、PapSecurityOauth2WebResponseExceptionTranslator 增加 security oauth2 的自定义异常处理类,针对不同的异常进行封装处理。 #20190923 1、移除 com.pap.oauth.authentication.mobile 针对 手机号验证码的支持: 原因: 1、希望将所有的用户信息查询存放在一个地方(自定义的认证方法中); 2、后续会有大量的多种登录方式的认证,希望入口保持一致 /oauth/token ; 1.1、使用 SecurityConfig.AuthenticationManagerBuilder.authenticationProvider(papUserDetailsAuthenticationProvider()) 方法,扩展用户认证过程中需要的额外参数,形如多种认证方式,可以将其改为 通过额外参数的传入(domain/authenticationType), 进行区分控制; 1.2、增加针对 com.pap.oauth.config.security.jwt 的支持,将 access_token 等信息改为 JWT 信息进行维护处理。 同时注意增加了用户属性,详见 包路径下 关于 userId 部分的控制; 1.3、同时移除 userDetailsServier 的支持,将所有的用户信息的查询,移动至 自定义的用户认证中: 1.4、增加自定义异常转换器 PapSecurityOauth2WebResponseExceptionTranslator,将异常统一处理;