使用了Spring Cloud OAuth2、Spring Cloud Security、Eureka、Zuul。实现了统一授权认证。使用JPA自动创建数据表免去导入麻烦。

Spring Cloud 基于网关的统一授权认证
本项目基于汪云飞记录本Github地址(最后有连接)由于不好部署需要导入数据库等原因本人稍微做了一些改进,但总体上还是相似的,只是更容易跑起来,省去了导入数据库等麻烦的操作。 如果对Spring Boot开发感兴趣可以看看JavaEE开发的颠覆者: Spring Boot实战作者也是汪云飞.

使用OAuth2实现多个微服务的统一认证授权,通过向OAUTH服务发送某个类型的grant type进行集中认证和授权获得access token,这个access token是受其他微服务信任的。后续访问中可以通过这个access token来进行。

  • account: 用户微服务
  • xfauth: OAUTH2认证授权中心
  • gateway: 边界网关
  • eureka: 服务注册和发现

基础环境

  • 开启MySql 修改xfauth配置文件bootstrap.yml中的datasource配置mysql用户名、密码、数据库名。
  • 开启Redis 修改xfauth配置文件bootstrap.yml中的redis如果默认端口号是6379 host为 localhost则不用修改。
  • 项目中使用了lombok如果你的IDE是Eclipse需要安装相应的插件,如果是IDEA2017版本的不用安装插件已经支持。引入lombok的方式请自行百度。

运行

  • 运行eureka 端口号8888
  • 运行gateway 端口号8088
  • 运行xfauth(因为使用了JPA会自动创建数据表不用导入数据库,只需要开启mysql) 端口号5000
    账户1: username:fpf password:fpf
    账户2: username:wl password:wl
    相关的设置可以在xfauth项目中的Init类中看到
  • 运行account 端口号8083

测试
一、密码模式

  • 通关zuul网关访问认证服务获取 access token 8088是网关端口

http://localhost:8088/uaa/oauth/token?grant_type=password&username=fpf&password=fpf

springcloud权限管理可以不使用框架吗 springcloud权限认证_zuul

  • 通过access token访问xfauth中的/userAPI获取用户信息 或者在header中添加Authorization

http://localhost:5000/user?access_token=2c274c59-6f5e-4129-88e9-e762d2241921

springcloud权限管理可以不使用框架吗 springcloud权限认证_zuul_02

  • 使用相同的access token访问account中的/currentAPI获取用户信息 可以看到都是相同的用户信息

http://localhost:8083/current?access_token=2c274c59-6f5e-4129-88e9-e762d2241921

springcloud权限管理可以不使用框架吗 springcloud权限认证_security_03

  • 使用access token访问account中带权限的/queryAPI
    http://localhost:8083/query?access_token=2c274c59-6f5e-4129-88e9-e762d2241921
  • springcloud权限管理可以不使用框架吗 springcloud权限认证_eureka_04


  • 使用wl用户重新获取access token访问account中带权限的/queryAPI

http://localhost:8088/uaa/oauth/token?grant_type=password&username=wl&password=wl

springcloud权限管理可以不使用框架吗 springcloud权限认证_security_05

  • 通过wl用户的access token访问xfauth中的/userAPI获取用户信息
    或者在header中添加Authorization

http://localhost:5000/user?access_token=e174aba9-f110-447c-b070-b7415ee249da

springcloud权限管理可以不使用框架吗 springcloud权限认证_zuul_06

  • 使用wl用户的access token访问account中的/currentAPI获取用户信息 可以看到都是相同的用户信息

http://localhost:8083/current?access_token=e174aba9-f110-447c-b070-b7415ee249da

springcloud权限管理可以不使用框架吗 springcloud权限认证_zuul_07

  • 使用wl用户的access token访问account中带权限的/queryAPI

http://localhost:8083/query?access_token=e174aba9-f110-447c-b070-b7415ee249da

springcloud权限管理可以不使用框架吗 springcloud权限认证_oauth2_08

二、授权码模式

  • 请求

http://localhost:8088/uaa/oauth/authorize?response_type=code&client_id=webapp&redirect_uri=http://baidu.com&state=123 输入密码:fpf fpf

  • 回调:

https://www.baidu.com/?code=1JsK76&state=123

  • 变更code,获取token

http://localhost:8088/uaa/oauth/token?client_id=webapp&grant_type=authorization_code&code=1JsK76&client_secret=webapp&redirect_uri=http://baidu.com

{
  "access_token": "02469e49-627b-4a13-926b-ec642d283537",
  "token_type": "bearer",
  "refresh_token": "60358383-e7a1-4d88-8df1-9eab72592637",
  "expires_in": 43199,
  "scope": "webapp"
}

springcloud权限管理可以不使用框架吗 springcloud权限认证_zuul_09

  • 通过access token访问xfauth中的/userAPI获取用户信息 或者在header中添加Authorization

http://localhost:5000/user?access_token=02469e49-627b-4a13-926b-ec642d283537

springcloud权限管理可以不使用框架吗 springcloud权限认证_oauth2_10

  • 使用相同的access token访问account中的/currentAPI获取用户信息 可以看到都是相同的用户信息

http://localhost:8083/current?access_token=02469e49-627b-4a13-926b-ec642d283537

springcloud权限管理可以不使用框架吗 springcloud权限认证_security_11

  • 使用access token访问account中带权限的/queryAPI

http://localhost:8083/query?access_token=02469e49-627b-4a13-926b-ec642d283537

springcloud权限管理可以不使用框架吗 springcloud权限认证_zuul_12

  • 请求

http://localhost:8088/uaa/oauth/authorize?response_type=code&client_id=webapp&redirect_uri=http://baidu.com&state=123 输入密码:wl wl

  • 回调:

https://www.baidu.com/?code=IsT97v&state=123

springcloud权限管理可以不使用框架吗 springcloud权限认证_eureka_13

  • 变更code,获取token

http://localhost:8088/uaa/oauth/token?client_id=webapp&grant_type=authorization_code&code=IsT97v&client_secret=webapp&redirect_uri=http://baidu.com

{
  "access_token": "b0a81a7c-8aa7-4a4f-8337-c6f34bd47847",
  "token_type": "bearer",
  "refresh_token": "a2079994-210f-4339-bec9-89c75cb891a7",
  "expires_in": 43199,
  "scope": "webapp"
}

springcloud权限管理可以不使用框架吗 springcloud权限认证_security_14

  • 通过access token访问xfauth中的/userAPI获取用户信息 或者在header中添加Authorization

http://localhost:5000/user?access_token=b0a81a7c-8aa7-4a4f-8337-c6f34bd47847

springcloud权限管理可以不使用框架吗 springcloud权限认证_zuul_15

  • 使用相同的access token访问account中的/currentAPI获取用户信息 可以看到都是相同的用户信息

http://localhost:8083/current?access_token=b0a81a7c-8aa7-4a4f-8337-c6f34bd47847

springcloud权限管理可以不使用框架吗 springcloud权限认证_zuul_16

  • 使用access token访问account中带权限的/queryAPI

http://localhost:8083/query?access_token=b0a81a7c-8aa7-4a4f-8337-c6f34bd47847

springcloud权限管理可以不使用框架吗 springcloud权限认证_eureka_17