目录
创建项目结构
父工程
客户
认证服务器
资源拥有者
资源服务器
创建项目结构
演示代码下载:
https://gitee.com/lisenaq/oauth2-example.git
演示客户发请求:
http://localhost:8080/client/getCode
父工程
父工程有:子模块需要重新导入该标签的作用是为了锁定整个微服务项目的版本
父工程中没有:子模块不需要导入对应的依赖
客户
创建
springboot
项目,加入配置必要的依赖包
<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-server
server:
port: 8081
资源拥有者
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-owner
server:
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-server
server:
port: 8083
1.资源所有者
资源所有者接到客户请求,需要返回授权码
2.客户
编写一个controller,向资源所有者请求发送请求来获取授权码
3.客户
资源所有者生成授权码后,需要回调客户的一个接口,将授权码传回, 客户得到授权码后,需要向认证
服务器发出请求,申请令牌
4.认证服务器
认证服务器接到客户请求,生成令牌,并返回令牌数据
5. 客户
客户获取了令牌,并使用令牌向资源服务器请求数据
6. 资源服务器
资源服务器接到请求,返回数据
关于代码调试,下载导入的项目中已经打好了Debugger断点,自己去调就好了。