* 实现方式有5中, * - 1.JdbcTokenStore * - 2.RedisTokenStore * - 3.JwtTokenStore * - 4.InMemmoryTokenStore * - 5.JwkTokenStore *
* 3.WebResponseExceptionTranslator为自定义的验证错误异常返回类,其中responsedata为自定义的数据返回类 包含code,message,data三个属性 */ public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints .authenticationManager(authenticationManager) .userDetailsService(userDetailsService) .tokenStore(redisTokenStore()); endpoints.exceptionTranslator(webResponseExceptionTranslator); } ``` ### 4.资源服务器 编写`@Configuration @EnableResourceServer` 类继承`ResourceServerConfigurerAdapter` ~~~java @Configuration @EnableResourceServer @Order(3) public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { @Override public void configure(ResourceServerSecurityConfigurer resources) throws Exception { super.configure(resources); } @Override public void configure(HttpSecurity http) throws Exception { http .httpBasic().and() .csrf().disable() .authorizeRequests() .anyRequest().authenticated() .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler()); } // @Override // public void configure(HttpSecurity http) throws Exception { // http. // anonymous().disable() // .requestMatchers().antMatchers("/user*/**") // .and().authorizeRequests().anyRequest().authenticated() // .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler()); // } } ~~~ ## 三、授权码模式 ### 3.1 获取code #### 3.1.1 请求url http://localhost:8090/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code&scope={scope}&state={state} ~~~shell http://localhost:8090/oauth/authorize?client_id=client&redirect_uri=http://www.baidu.com&response_type=code&scope=app ~~~ #### 3.1.2 请求方式 - GET #### 3.1.3 请求参数 | 参数名 | 参数值 | 是否必须 | 类型 | 说明 | | :------------ | :----- | :------- | :----- | :----------------------------------------------------------- | | client_id | | 是 | string | 应用id | | redirect_uri | | 是 | string | 回跳地址(必需和应用配置里面的地址一致) | | response_type | code | 是 | string | 返回类型 | | scope | | 否 | string | 授权范围 | | state | | 否 | string | 表示客户端的当前状态,可以指定任意值,认证服务器会原封不动地返回这个值 | #### 3.1.4 返回示例 - **登录成功后跳转回调地址redirect_uri并带上code参数**  ### 3.2 通过code获取token #### 3.2.1 请求URL http://localhost:8090/oauth/token?code={code}&grant_type=authorization_code&redirect_uri={redirect_uri}&scope={scope} ~~~shell curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=authorization_code&code=Li4NZo&redirect_uri=http://www.baidu.com' "http://client:secret@localhost:8090/oauth/token" ~~~ #### 3.2.2 请求方式 - post #### 3.2.3 请求头 | 参数名 | 参数值 | 是否必须 | 类型 | 说明 | | :------------ | :------------------------------ | :------- | :----- | :----------------------------------------------------------- | | Authorization | Basic {clientId}:{clientSecret} | 是 | string | {clientId}:{clientSecret} 的值必需使用base64加密,clientId为应用id,clientSecret为应用密钥 |  #### 3.2.4 请求参数 | 参数名 | 参数值 | 是否必须 | 类型 | 说明 | | :----------- | :----------------- | :------- | :----- | :------------------------- | | grant_type | authorization_code | 是 | string | 授权类型 | | code | | 是 | string | 第一步获取的授权码 | | redirect_uri | | 是 | string | 回调地址,必需与第一步一致 | | scope | | 否 | string | 授权范围 | #### 3.2.5 返回示例 - **正确时返回:** ``` { "access_token": "20b106d3-fd9d-437c-87c3-300ffaf6e67e", "token_type": "bearer", "refresh_token": "c4b3d67b-4311-41aa-8253-832fe39224ea", "expires_in": 16534, "scope": "app" } ``` - **错误时返回:** ``` { "datas": null, "resp_code": 1, "resp_msg": "Invalid authorization code: asPRRW" } ``` ## 四、密码模式 POST请求,http只有一步调用,如下截图 认证服务器,授予第三方应用使用哪个用户权限。 资源服务器,进行资源的鉴权 ### 4.1 请求头 | 参数名 | 参数值 | 是否必须 | 类型 | 说明 | | :------------ | :------------------------------ | :------- | :----- | :----------------------------------------------------------- | | Authorization | Basic {clientId}:{clientSecret} | 是 | string | {clientId}:{clientSecret} 的值必需使用base64加密,clientId为应用id,clientSecret为应用密钥 |  ### 4.2 请求参数 | 参数名 | 参数值 | 是否必须 | 类型 | 说明 | | :--------- | :------- | :------- | :----- | :------- | | grant_type | password | 是 | string | 授权类型 | | username | | 是 | string | 用户名 | | password | | 是 | string | 密码 | 截图如下  ### 4.3 返回示例 - **正确时返回:** ``` { "access_token": "20b106d3-fd9d-437c-87c3-300ffaf6e67e", "token_type": "bearer", "refresh_token": "c4b3d67b-4311-41aa-8253-832fe39224ea", "expires_in": 16302, "scope": "app" } ``` ## 五、项目参考 https://github.com/yangxiufeng666/Micro-Service-Skeleton/tree/v2.0 http://www.ruoyi.vip/ https://gitee.com/y_project/RuoYi-Cloud