springcloud-oauth2
springcloud-oauth2 copied to clipboard
基于Spring Cloud Oauth2 JWT搭建微服务的安全认证中心
使用OAuth2实现多个微服务的统一认证授权,通过向OAUTH服务发送某个类型的grant type进行集中认证和授权获得access token,这个access token是受其他微服务信任的。后续访问中可以通过这个access token来进行
系统结构
eureka-server: 服务注册和发现
oauth-server: 安全认证包括资源服务器和认证服务器
技术栈
- Spring Cloud
- Mybatis-plus
- Oauth2
运行
运行eureka-server 端口号8761 运行oauth-server 端口号8080
测试
启动springboot应用之后,使用http工具通过接口 /registry 进行注册
注册成功后 使用账号密码去登录
password模式:
http://localhost:8080/oauth/token? username=user&password=111111& grant_type=password&scope=select& client_id=client_2&client_secret=123456
看到成功返回token
在配置中,我们已经配置了对order资源的保护,如果直接访问: http://localhost:8080/order/1 ,会得到这样的响应
显示没有权限去访问
而对于未受保护的product资源 得到返回的结果
携带accessToken参数访问受保护的资源, 使用password模式获得的token
得到了之前匿名访问无法获取的资源
client模式
client模式:
http://localhost:8080/oauth/token?grant_type=client_credentials&scope=select&client_id=client_1&client_secret=123456
响应如下:
使用client模式获得的token:
去访问 http://localhost:8080/order/1?access_token=xxxx