资源服务器就是所谓的一些应用接口所在的服务器。比如用户信息CRUD接口,商品CRUD接口。想要访问这些接口时需要携带认证服务器颁发的token进行访问。下面我将搭建一个认证服务器和资源服务,进行简单的测试
1.认证服务器搭建
1.依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-oauth2</artifactId></dependency>
2.配置
server:port: 8080spring:application:name: authsecurity:oauth2:client:client-id: client1client-secret: 123456authorization:#必须配置否则 资源服务器无法验证check-token-access: isAuthenticated()
2.资源服务器搭建
1.依赖
同上
2.配置
server:port: 8081spring:application:name: resourcesecurity:oauth2:resource:id: client1token-info-uri: http://127.0.0.1:8080/oauth/check_token#必须配置client,因为验证token需要clientid和secretclient:client-id: client1client-secret: 123456
3.代码
@RestControllerpublic class TestController {@GetMapping("test")public String test(){return "Success";}}
3.流程演示
1.访问localhost:8081/test,无法成功访问
2.请求认证服务器颁发token(密码模式)

3.再次访问(成功)
