1.下载资源
演示代码:
OAuth2-example: 演示OAuth2的认证流程https://gitee.com/lisenaq/oauth2-example
克隆下载到本地:
导入项目:
client 客户
authorization-server 认证服务
resource-owner 资源所有者
resource-server 资源服务器
流程:
客户向资源所有者申请授权码
资源所有者下发授权码
客户拿到授权码后向认证服务器申请令牌
认证服务器接到申请下方令牌
客户获取令牌后向资源服务器请求数据
2.必须配置的资源
<dependencies>
<dependency>
<groupId>org.apache.oltu.oauth2</groupId>
<artifactId>org.apache.oltu.oauth2.client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
application.yml
spring:
application:
name: OAuth2-client
server:
port: 8080
认证服务器:
pom.xml
<dependencies><dependency><groupId> org.apache.oltu.oauth2 </groupId><artifactId> org.apache.oltu.oauth2.authzserver </artifactId></dependency><dependency><groupId> org.apache.oltu.oauth2 </groupId><artifactId> org.apache.oltu.oauth2.resourceserver </artifactId></dependency><dependency><groupId> org.springframework.boot </groupId><artifactId> spring-boot-starter-web </artifactId></dependency><dependency><groupId> org.projectlombok </groupId><artifactId> lombok </artifactId><optional> true </optional></dependency><dependency><groupId> org.springframework.boot </groupId><artifactId> spring-boot-starter-test </artifactId><scope> test </scope></dependency></dependencies>
application.yml
spring:application:name: OAuth2-authorization-serverserver:port: 8081
资源拥有者
<dependencies><dependency><groupId> org.apache.oltu.oauth2 </groupId><artifactId> org.apache.oltu.oauth2.authzserver </artifactId></dependency><dependency><groupId> org.apache.oltu.oauth2 </groupId><artifactId> org.apache.oltu.oauth2.resourceserver </artifactId></dependency><dependency><groupId> org.springframework.boot </groupId><artifactId> spring-boot-starter-web </artifactId></dependency><dependency><groupId> org.projectlombok </groupId><artifactId> lombok </artifactId><optional> true </optional></dependency><dependency><groupId> org.springframework.boot </groupId><artifactId> spring-boot-starter-test </artifactId><scope> test </scope></dependency></dependencies>
application.yml
spring :application :name : OAuth2-resource-ownerserver :port : 8082
资源服务器 :
pom.xml
<dependencies><dependency><groupId> org.apache.oltu.oauth2 </groupId><artifactId> org.apache.oltu.oauth2.authzserver </artifactId></dependency><dependency><groupId> org.apache.oltu.oauth2 </groupId><artifactId> org.apache.oltu.oauth2.resourceserver </artifactId></dependency><dependency><groupId> org.springframework.boot </groupId><artifactId> spring-boot-starter-web </artifactId></dependency><dependency><groupId> org.projectlombok </groupId><artifactId> lombok </artifactId><optional> true </optional></dependency><dependency><groupId> org.springframework.boot </groupId><artifactId> spring-boot-starter-test </artifactId><scope> test </scope></dependency></dependencies>
application.yml
spring :application :name : oauth2-resource-serverserver :port : 8083