OAuth2是一个关于授权的开放标准,核心思路是通过各类认证手段(具体什么手段OAuth2不关心)认证用户身份,并颁发token(令牌),使得第三方应用可以使用该token(令牌)在限定时间、限定范围访问指定资源。
OAuth2中使用token验证用户登录合法性,但token最大的问题是不携带用户信息,资源服务器无法在本地进行验证,每次对于资源的访问,资源服务器都需要向认证服务器发起请求,一是验证token的有效性,二是获取token对应的用户信息。如果有大量的此类请求,无疑处理效率是很低,且认证服务器会变成一个中心节点,这在分布式架构下很影响性能。如果认证服务器颁发的是jwt格式的token,那么资源服务器就可以直接自己验证token的有效性并绑定用户,这无疑大大提升了处理效率且减少了单点隐患。
SpringCloud认证授权解决思路:认证服务负责认证,网关负责校验认证和鉴权,其他API服务负责处理自己的业务逻辑。安全相关的逻辑只存在于认证服务和网关服务中,其他服务只是单纯地提供服务而没有任何安全相关逻辑。
微服务鉴权功能划分:
gitegg-oauth:Oauth2用户认证和单点登录
gitegg-gateway:请求转发和统一鉴权
gitegg-system: 读取系统配置的RBAC权限配置并存放到缓存
一、鉴权配置1、GitEgg-Platform工程下新建gitegg-platform-oauth2工程,用于统一管理OAuth2版本,及统一配置
<!--?xml version="1.0" encoding="UTF-8"?--> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 "> <parent> <artifactid>GitEgg-Platform</artifactid> <groupid>com.gitegg.platform</groupid> <version>1.0-SNAPSHOT</version> </parent> <modelversion>4.0.0</modelversion> <artifactid>gitegg-platform-oauth2</artifactid> <name>${project.artifactId}</name> <packaging>jar</packaging> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-configuration-processor</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-oauth2</artifactid> </dependency> <dependency> <groupid>org.springframework.security</groupid> <artifactid>spring-security-oauth2-jose</artifactid> </dependency> <dependency> <groupid>org.springframework.security</groupid> <artifactid>spring-security-oauth2-resource-server</artifactid> </dependency> <dependency> <groupid>com.gitegg.platform</groupid> <artifactid>gitegg-platform-swagger</artifactid> <optional>true</optional> </dependency> </dependencies> </project>2、在gitegg-oauth工程中引入需要的库
<!--?xml version="1.0" encoding="UTF-8"?--> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 "> <parent> <artifactid>GitEgg-Cloud</artifactid> <groupid>com.gitegg.cloud</groupid> <version>1.0-SNAPSHOT</version> </parent> <modelversion>4.0.0</modelversion> <artifactid>gitegg-oauth</artifactid> <name>${project.artifactId}</name> <packaging>jar</packaging> <dependencies> <!-- gitegg-platform-boot --> <dependency> <groupid>com.gitegg.platform</groupid> <artifactid>gitegg-platform-boot</artifactid> <version>${gitegg.project.version}</version> </dependency> <!-- gitegg-platform-cloud --> <dependency> <groupid>com.gitegg.platform</groupid> <artifactid>gitegg-platform-cloud</artifactid> <version>${gitegg.project.version}</version> </dependency> <!-- gitegg-platform-oauth2 --> <dependency> <groupid>com.gitegg.platform</groupid> <artifactid>gitegg-platform-oauth2</artifactid> <version>${gitegg.project.version}</version> </dependency> <!-- gitegg数据库驱动及连接池 --> <dependency> <groupid>com.gitegg.platform</groupid> <artifactid>gitegg-platform-db</artifactid> </dependency> <!-- gitegg mybatis-plus --> <dependency> <groupid>com.gitegg.platform</groupid> <artifactid>gitegg-platform-mybatis</artifactid> </dependency> <!-- 验证码 --> <dependency> <groupid>com.gitegg.platform</groupid> <artifactid>gitegg-platform-captcha</artifactid> </dependency> <!-- gitegg-service-system 的fegin公共调用方法 --> <dependency> <groupid>com.gitegg.cloud</groupid> <artifactid>gitegg-service-system-api</artifactid> <version>${gitegg.project.version}</version> </dependency> <dependency> <groupid>org.apache.tomcat.embed</groupid> <artifactid>tomcat-embed-core</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </dependency> </dependencies> </project>