# springcloud-oauth
**Repository Path**: getlore/springcloud-oauth
## Basic Information
- **Project Name**: springcloud-oauth
- **Description**: 授权认证服务器 (登陆)
- **Primary Language**: Unknown
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 1
- **Created**: 2022-04-09
- **Last Updated**: 2022-04-09
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# 认证和授权
## 地址
- [github](https://github.com/westwong/springcloud-oauth)
## 简介
- 该组件采用Oauth 2.0,如果你不太了解什么是oauth2,还是推荐 阮一峰《[理解OAuth 2.0](http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html)》。
- 该组件涉及到的技术栈有: [Spring Security OAuth](https://spring.io/projects/spring-security-oauth) 、JPA 、Mysql、Redis 以及微服务的基本组件。
```xml
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-oauth2
org.springframework.boot
spring-boot-starter-data-redis
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
com.aliyun
aliyun-java-sdk-core
3.7.0
com.aliyun
aliyun-java-sdk-ecs
4.11.0
com.alibaba
fastjson
1.2.25
org.apache.commons
commons-lang3
3.3.2
compile
```
- 该组件支持 用户名密码模式、手机验证码登录,openId 登录 基本上能满足一般项目的需求。
- 手机验证码登录,短信服务用的是[阿里云短信服务](https://help.aliyun.com/document_detail/101414.html)。
- 微信openId 登录目前只是针对小程序。
相关yml配置如下:
```properties
ali:
message:
access-key: 123
access-secret: 123
wechat:
mini-app:
app-id: 123
app-key: 123
```
- 常规配置就不细说了。对于第一次用相关组件的同学,还是建议您首先看看文档。
关于客户端Client的配置:
(我只粘贴了部分代码)
```java
package com.k2future.oauth2server.config.security;
/**
*
* @author West
* @date create in 2019/05/07
*
**/
@Configuration
@EnableAuthorizationServer
public class Oauth2Config extends AuthorizationServerConfigurerAdapter {
/**
* 配置客户端信息
* @param clients 客户端
* @throws Exception
*/
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
// 将客户端的信息存储在内存中
clients.inMemory()
.withClient("web-client")
.secret(passwordEncoder.encode("password"))
.authorizedGrantTypes("client_credentials", "refresh_token", "password", "mobile")
.accessTokenValiditySeconds(DEFAULT_VALIDITY_S)
.scopes("web")
.and()
.withClient("app-client")
.secret(passwordEncoder.encode("password"))
.authorizedGrantTypes("client_credentials", "refresh_token", "password", "mobile","sign")
.accessTokenValiditySeconds(DEFAULT_VALIDITY_S)
.scopes("app");
}
}
```
关于url权限过滤的的配置:
(主要针对以前没有用过security的同学,其实和shiro差不多)
(我只粘贴了部分代码)
```java
package com.k2future.oauth2server.config.security;
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS).permitAll()
.antMatchers("/users/register",
"/mobile/**",
"/sign/**").permitAll()
.anyRequest().authenticated() // 所有请求都需要安全验证
.and()
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
```
- 最后还需要说明一下除了指定上面指定不需要权限的api,其他都接口都是需要权限的。token分为一下两种类型:
- 用于获取token的 Authorization: 为basic类型,他的算法是 "clientId:clientSecret"的 base64 。如:Basic d2ViLWNsaWVudDpZdW5RdWU=Y2xpbmljLWNsaWVudDpZdW5RdWU=
- 第二种是Oauth2的授权模式:
```json
{
"access_token": "ce04de44-1da0-4ca0-8310-c26e3f6a4843",
"token_type": "bearer",
"refresh_token": "87520888-bb1a-437e-91b8-2e44f61af949",
"expires_in": 606161,
"scope": "web"
}
```
他的 Authorization:为beare, 用法是 (token_type access_token),如:bearer ce04de44-1da0-4ca0-8310-c26e3f6a4843
因为方便,我把access_token的过期时间设置为15天。如果你觉得不方便可以修改过期时间,并在应用中使用刷新获取 refresh_token 获取 access_token 的方式。参考下面的api: **获取token**。
# API(仅供参考)
## 手机短信验证码获取token
### 基本信息
**Path:** /mobile/token
**Method:** POST
**接口描述:**
token 刷新 参考 接口 获取token(/oauth/token)
### 请求参数
**Headers**
| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
| ------------- | --------------------------------- | -------- | ---------------------------------------------------------- | ------------------------------- |
| Content-Type | application/x-www-form-urlencoded | 是 | | |
| Authorization | Basic | 是 | Basic d2ViLWNsaWVudDpZdW5RdWU=Y2xpbmljLWNsaWVudDpZdW5RdWU= | "clientId:clientSecret"的base64 |
**Body**
| 参数名称 | 参数类型 | 是否必须 | 示例 | 备注 |
| -------- | -------- | -------- | ----------- | ------------ |
| mobile | text | 是 | 13312341234 | 必须是手机号 |
| code | text | 是 | 123456 | 六位数字 |
### 返回数据
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
access_token | string | 必须 | | | |
token_type | string | 必须 | | | |
refresh_token | string | 必须 | | | |
expires_in | number | 必须 | | | |
scope | string | 必须 | | | |
## 签名获取token
### 基本信息
**Path:** /sign/token
**Method:** POST
**接口描述:**
token 刷新 参考 接口 获取token(/oauth/token)
### 请求参数
**Headers**
| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
| ------------- | --------------------------------- | -------- | ---------------------------------- | ------------------------------- |
| Content-Type | application/x-www-form-urlencoded | 是 | | |
| Authorization | Basic | 是 | Basic Y2xpbmljLWNsaWVudDpZdW5RdWU= | "clientId:clientSecret"的base64 |
**Body**
| 参数名称 | 参数类型 | 是否必须 | 示例 | 备注 |
| -------- | -------- | -------- | ---- | ---------------------- |
| sign | text | 是 | | openId等第三方唯一标识 |
### 返回数据
## 获取token
### 基本信息
**Path:** /oauth/token
**Method:** POST
**接口描述:**
### 请求参数
**Headers**
| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
| ------------- | --------------------------------- | -------- | ------------------------------ | ------------------------------- |
| Content-Type | application/x-www-form-urlencoded | 是 | | |
| Authorization | Basic | 是 | Basic d2ViLWNsaWVudDpZdW5RdWU= | "clientId:clientSecret"的base64 |
**Body**
| 参数名称 | 参数类型 | 是否必须 | 示例 | 备注 |
| ------------- | -------- | -------- | -------- | ------------------------------------- |
| username | text | 是 | qwe | 账号 刷新时候不需要 |
| password | text | 是 | 123456 | 密码 刷新时候不需要 |
| grant_type | text | 是 | password | 授权方式 "refresh_token", "password" |
| refresh_token | text | 否 | | 刷新时需要 |
### 返回数据
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
access_token | string | 必须 | | token | |
token_type | string | 必须 | | 类型 | |
refresh_token | string | 必须 | | 用于刷新token的token | |
expires_in | number | 必须 | | 过期时间 | |
scope | string | 必须 | | 作用范围 | |
## 获取短信验证码
### 基本信息
**Path:** /mobile/code/get
**Method:** POST
**接口描述:**
获取6位短信验证码,有效时间10分钟
### 请求参数
**Headers**
| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
| ------------- | --------------------------------- | -------- | ------------------------------ | ------------------------------- |
| Content-Type | application/x-www-form-urlencoded | 是 | | |
| Authorization | Basic | 是 | Basic d2ViLWNsaWVudDpZdW5RdWU= | "clientId:clientSecret"的base64 |
**Body**
| 参数名称 | 参数类型 | 是否必须 | 示例 | 备注 |
| -------- | -------- | -------- | ----------- | ------------ |
| mobile | text | 是 | 13312341234 | 必须是手机号 |
### 返回数据
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
access_token | string | 必须 | | | |
token_type | string | 必须 | | | |
refresh_token | string | 必须 | | | |
expires_in | number | 必须 | | | |
scope | string | 必须 | | | |
## 验证短信验证码
### 基本信息
**Path:** /mobile/code/verify
**Method:** POST
**接口描述:**
### 请求参数
**Headers**
| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
| ------------ | --------------------------------- | -------- | ---- | ---- |
| Content-Type | application/x-www-form-urlencoded | 是 | | |
**Body**
| 参数名称 | 参数类型 | 是否必须 | 示例 | 备注 |
| -------- | -------- | -------- | ----------- | ------------ |
| mobile | text | 是 | 13312341234 | 必须是手机号 |
| code | text | 是 | 123456 | 六位数字 |
### 返回数据
## 修改用户
### 基本信息
**Path:** /users/update
**Method:** POST
**接口描述:**
### 请求参数
**Headers**
| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
| ------------- | ---------------- | -------- | ------------------------------------------- | ---- |
| Content-Type | application/json | 是 | | |
| Authorization | bearer | 是 | bearer ce04de44-1da0-4ca0-8310-c26e3f6a4843 | |
**Body**
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
id | number | 必须 | | 用户id | |
phone | string | 非必须 | | 电话 | |
openId | string | 非必须 | | openId | |
### 返回数据
## 修改用户密码
### 基本信息
**Path:** /users/pwd/update
**Method:** POST
**接口描述:**
### 请求参数
**Headers**
| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
| ------------- | ---------------- | -------- | ---- | ------------ |
| Content-Type | application/json | 是 | | |
| Authorization | | 是 | | access_token |
**Body**
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
userId | number | 必须 | | 用户id | |
oldPassword | string | 必须 | | | |
newPassword | string | 必须 | | | |
### 返回数据
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
error | string | 非必须 | | | |
status | number | 非必须 | | | |
## 修改用户角色
### 基本信息
**Path:** /users/role/update
**Method:** POST
**接口描述:**
### 请求参数
**Headers**
| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
| ------------ | ---------------- | -------- | ---- | ---- |
| Content-Type | application/json | 是 | | |
**Body**
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
id | number | 必须 | | userId | |
roleList | object [] | 必须 | | 删除传 :[] (注意:不是null) | item 类型: object |
├─ id | number | 必须 | | roleId | |
### 返回数据
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
status | number | 非必须 | | | |
## 修改用户详情
### 基本信息
**Path:** /userDetail/update
**Method:** POST
**接口描述:**
### 请求参数
**Headers**
| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
| ------------- | ---------------- | -------- | ---- | ---- |
| Content-Type | application/json | 是 | | |
| Authorization | | 是 | | |
**Body**
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
id | number | 必须 | | | |
name | string | 非必须 | | | |
age | string | 非必须 | | | |
phone | string | 非必须 | | | |
gender | string | 非必须 | | | |
addr | string | 非必须 | | | |
createTime | string | 非必须 | | yyyy-MM-dd HH:mm:ss | |
nation | string | 非必须 | | | |
instruction | string | 非必须 | | | |
images | string | 非必须 | | | |
birthday | string | 非必须 | | yyyy-MM-dd | |
### 返回数据
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
status | number | 非必须 | | | |
error | string | 非必须 | | | |
## 查询用户
### 基本信息
**Path:** /users/find
**Method:** POST
**接口描述:**
### 请求参数
**Headers**
| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
| ------------- | ---------------- | -------- | ---- | ---- |
| Content-Type | application/json | 是 | | |
| Authorization | | 是 | | |
**Body**
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
id | number | 非必须 | | 当传了id时 以id为主 | |
username | string | 非必须 | | | |
### 返回数据
## 注册用户
### 基本信息
**Path:** /users/register
**Method:** POST
**接口描述:**
### 请求参数
**Headers**
| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
| ------------ | ---------------- | -------- | ---- | ---- |
| Content-Type | application/json | 是 | | |
**Body**
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
username | string | 非必须 | | | |
password | string | 非必须 | | | |
### 返回数据
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
error | string | 非必须 | | | |
status | number | 非必须 | | | |
message | number | 必须 | | 用户id | |
## 添加用户
### 基本信息
**Path:** /users/save
**Method:** POST
**接口描述:**
### 请求参数
**Headers**
| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
| ------------- | ---------------- | -------- | ---- | ------------ |
| Content-Type | application/json | 是 | | |
| Authorization | | 是 | | access_token |
**Body**
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
username | string | 非必须 | | | |
password | string | 非必须 | | | |
id | number | 非必须 | | | |
roleList | object [] | 非必须 | | | item 类型: object |
├─ roleName | string | 必须 | | | |
### 返回数据
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
error | string | 非必须 | | | |
status | number | 非必须 | | | |
message | number | 必须 | | 用户id | |
## 获取当前用户详情
### 基本信息
**Path:** /userDetail/current
**Method:** POST
**接口描述:**
### 请求参数
**Headers**
| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
| ------------- | ---------------- | -------- | ---- | ---- |
| Content-Type | application/json | 是 | | |
| Authorization | | 是 | | |
**Body**
### 返回数据
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
message | object | 非必须 | | | |
├─ id | number | 非必须 | | | |
├─ name | string | 非必须 | | | |
├─ age | null | 非必须 | | | |
├─ phone | null | 非必须 | | | |
├─ gender | null | 非必须 | | | |
├─ addr | null | 非必须 | | | |
├─ createTime | string | 非必须 | | | |
├─ nation | null | 非必须 | | | |
├─ instruction | null | 非必须 | | | |
├─ images | null | 非必须 | | | |
├─ birthday | null | 非必须 | | | |
status | number | 非必须 | | | |
error | string | 非必须 | | | |
## 获取用户详情
### 基本信息
**Path:** /userDetail/get
**Method:** POST
**接口描述:**
### 请求参数
**Headers**
| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
| ------------- | ---------------- | -------- | ---- | ---- |
| Content-Type | application/json | 是 | | |
| Authorization | | 是 | | |
**Body**
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
id | number | 必须 | | 当传了id时 以id为主 | |
### 返回数据
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
message | object | 非必须 | | | |
├─ id | number | 非必须 | | | |
├─ name | string | 非必须 | | | |
├─ age | null | 非必须 | | | |
├─ phone | null | 非必须 | | | |
├─ gender | null | 非必须 | | | |
├─ addr | null | 非必须 | | | |
├─ createTime | string | 非必须 | | | |
├─ nation | null | 非必须 | | | |
├─ instruction | null | 非必须 | | | |
├─ images | null | 非必须 | | | |
├─ birthday | null | 非必须 | | | |
status | number | 非必须 | | | |
error | string | 非必须 | | | |
## 获取角色
### 基本信息
**Path:** /roles/list
**Method:** POST
**接口描述:**
### 请求参数
**Headers**
| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
| ------------- | ---------------- | -------- | ---- | ---- |
| Content-Type | application/json | 是 | | |
| Authorization | | 是 | | |
**Body**
### 返回数据
名称 | 类型 | 是否必须 | 默认值 | 备注 | 其他信息 |
message | object [] | 非必须 | | | item 类型: object |
├─ id | number | 必须 | | | |
├─ roleName | string | 必须 | | | |
status | number | 非必须 | | | |