文章目录

  • 一。OAuth2.0
    • 1.介绍
    • 2.例子
    • 3.执行流程
  • 二.Spring Cloud Security OAuth2
    • 1.环境介绍
    • 2.认证流程
  • 三.整合分布式项目
    • 1.技术方案
    • 2.项目结构
  • 四。Spring Security基本数据库结构
    • t_user(用户表)
    • t_role(角色表)
    • t_permission(权限表)
    • t_user_role(用户角色表)
    • t_permission(角色权限表)
  • 五。注册中心搭建
  • 六。创建UAA授权服务工程
    • 1.环境搭建
    • 2.授权服务器配置
      • 2.1 EnableAuthorizationServer
      • 2.2 配置客户端详细信息
      • 2.3 管理令牌
      • 2.4 令牌访问端点配置
      • 2.5 令牌端点的安全约束
      • 2.6 web安全配置
    • 3.密码模式
    • 4.JWT令牌
      • 4.1 JWT介绍
      • 4.2 配置JWT令牌服务
      • 4.3 JWT令牌生成与校验
    • 5. 完善环境配置
  • 七。搭建网关工程
    • 1.环境配置
    • 2.token配置
    • 3.配置资源服务
    • 4.安全配置
    • 5. 转发明文token给微服务
  • 八。创建order资源服务工程
    • 1.环境配置
    • 2.token配置
    • 3.配置资源服务
    • 4.安全配置
    • 4.过滤器配置
    • 5.控制器示例代码
  • 九。集成测试
  • 十。扩展用户信息
    • 1.需求分析
    • 2.修改userDTO
    • 3.修改UserDetailService
    • 4.修改资源服务过虑器
    • 5.测试
  • 十一。项目地址

一。OAuth2.0

1.介绍

OAuth(开放授权)是一个开放标准,允许用户授权第三方应用访问他们存储在另外的服务提供者上的信息,而不 需要将用户名和密码提供给第三方应用或分享他们数据的所有内容。OAuth2.0是OAuth协议的延续版本,但不向 后兼容OAuth 1.0即完全废止了OAuth1.0。很多大公司如Google,Yahoo,Microsoft等都提供了OAUTH认证服 务,这些都足以说明OAUTH标准逐渐成为开放资源授权的标准。

Oauth协议目前发展到2.0版本,1.0版本过于复杂,2.0版本已得到广泛应用。

参考:https://baike.baidu.com/item/oAuth/7153134?fr=aladdin

Oauth协议:https://tools.ietf.org/html/rfc6749

2.例子

下边分析一个Oauth2认证的例子,通过例子去理解OAuth2.0协议的认证流程,本例子是黑马程序员网站使用微信 认证的过程,这个过程的简要描述如下:

用户借助微信认证登录黑马程序员网站,用户就不用单独在黑马程序员注册用户,怎么样算认证成功吗?黑马程序 员网站需要成功从微信获取用户的身份信息则认为用户认证成功,那如何从微信获取用户的身份信息?用户信息的 拥有者是用户本人,微信需要经过用户的同意方可为黑马程序员网站生成令牌,黑马程序员网站拿此令牌方可从微 信获取用户的信息。

1、客户端请求第三方授权

用户进入黑马程序的登录页面,点击微信的图标以微信账号登录系统,用户是自己在微信里信息的资源拥有者。

点击“微信”出现一个二维码,此时用户扫描二维码,开始给黑马程序员授权:

2、资源拥有者同意给客户端授权

资源拥有者扫描二维码表示资源拥有者同意给客户端授权,微信会对资源拥有者的身份进行验证, 验证通过后,微信会询问用户是否给授权黑马程序员访问自己的微信数据,用户点击“确认登录”表示同意授权,微信认证服务器会颁发一个授权码,并重定向到黑马程序员的网站。

3、客户端获取到授权码,请求认证服务器申请令牌

此过程用户看不到,客户端应用程序请求认证服务器,请求携带授权码。

4、认证服务器向客户端响应令牌

微信认证服务器验证了客户端请求的授权码,如果合法则给客户端颁发令牌,令牌是客户端访问资源的通行证。

此交互过程用户看不到,当客户端拿到令牌后,用户在黑马程序员看到已经登录成功。

5、客户端请求资源服务器的资源

客户端携带令牌访问资源服务器的资源。

黑马程序员网站携带令牌请求访问微信服务器获取用户的基本信息。

6、资源服务器返回受保护资源

资源服务器校验令牌的合法性,如果合法则向用户响应资源信息内容。

3.执行流程

以上认证授权详细的执行流程如下:

通过上边的例子我们大概了解了OAauth2.0的认证过程,下边我们看OAuth2.0认证流程:
引自OAauth2.0协议rfc6749 :https://tools.ietf.org/html/rfc6749

OAauth2.0包括以下角色:

1、客户端

本身不存储资源,需要通过资源拥有者的授权去请求资源服务器的资源,比如:Android客户端、Web客户端(浏览器端)、微信客户端等。

2、资源拥有者

通常为用户,也可以是应用程序,即该资源的拥有者。

3、授权服务器(也称认证服务器)

用于服务提供商对资源拥有的身份进行认证、对访问资源进行授权,认证成功后会给客户端发放令牌
(access_token),作为客户端访问资源服务器的凭据。本例为微信的认证服务器。

4、资源服务器

存储资源的服务器,本例子为微信存储的用户信息。
现在还有一个问题,服务提供商能允许随便一个客户端就接入到它的授权服务器吗?答案是否定的,服务提供商会给准入的接入方一个身份,用于接入时的凭据:

client_id:客户端标识
client_secret:客户端秘钥

因此,准确来说,授权服务器对两种OAuth2.0中的两个角色进行认证授权,分别是资源拥有者、客户端。

二.Spring Cloud Security OAuth2

1.环境介绍

Spring-Security-OAuth2是对OAuth2的一种实现,并且跟我们之前学习的Spring Security相辅相成,与Spring Cloud体系的集成也非常便利,接下来,我们需要对它进行学习,最终使用它来实现我们设计的分布式认证授权解决方案。

OAuth2.0的服务提供方涵盖两个服务,即授权服务 (Authorization Server,也叫认证服务) 和资源服务(Resource Server),使用 Spring Security OAuth2 的时候你可以选择把它们在同一个应用程序中实现,也可以选择建立使用同一个授权服务的多个资源服务。

授权服务 (Authorization Server),应包含对接入端以及登入用户的合法性进行验证并颁发token等功能,对令牌的请求端点由 Spring MVC 控制器进行实现,下面是配置一个认证服务必须要实现的endpoints:

AuthorizationEndpoint服务于认证请求。默认 URL:/oauth/authorize
TokenEndpoint服务于访问令牌的请求。默认 URL: /oauth/token

资源服务 (Resource Server),应包含对资源的保护功能,对非法请求进行拦截,对请求中token进行解析鉴权等,下面的过滤器用于实现 OAuth 2.0 资源服务:

OAuth2AuthenticationProcessingFilter用来对请求给出的身份令牌解析鉴权。

2.认证流程

认证流程如下:

  1. 客户端请求UAA授权服务进行认证。
  2. 认证通过后由UAA颁发令牌。
  3. 客户端携带令牌Token请求资源服务。
  4. 资源服务校验令牌的合法性,合法即返回资源信息。

三.整合分布式项目

1.技术方案

技术方案如下:

1、UAA认证服务负责认证授权。
2、所有请求经过 网关到达微服务
3、网关负责鉴权客户端以及请求转发
4、网关将token解析后传给微服务,微服务进行授权。

2.项目结构

项目名称:distributed-security

  • 注册中心:distributed-security-discovery 占用端口:53000
  • 网关模块:distributed-security-gateway 占用端口:53010
  • 认证模块:distributed-security-uaa 占用端口:53020
  • 订单模块:distributed-security-order 占用端口:53021

四。Spring Security基本数据库结构

由于本项目中的验证是从数据库中读取用户数据,所以我们需要创建一个名为user_db的数据库,并创建五张数据表

若对此有疑问的可以参照我的另外一篇博客:SpringBoot整合Spring Security实现用户角色验证以及权限控制

t_user(用户表)

DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user`  (`id` bigint(20) NOT NULL COMMENT '用户id',`username` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`password` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`fullname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户姓名',`mobile` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '手机号',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;INSERT INTO `t_user` VALUES (1, 'zhangsan', '$2a$10$3MYHLGvYkYgCKRAXNeEDTe1g7AznV/Ni7pmXP2zD0V0YS/HosBamu', '张三', '12345674891');

创建完成后如下所示:

t_role(角色表)

DROP TABLE IF EXISTS `t_role`;
CREATE TABLE `t_role`  (`id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`role_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`description` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`create_time` datetime NULL DEFAULT NULL,`update_time` datetime NULL DEFAULT NULL,`status` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,PRIMARY KEY (`id`) USING BTREE,UNIQUE INDEX `unique_role_name`(`role_name`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;INSERT INTO `t_role` VALUES ('1', '管理员', NULL, NULL, NULL, '');

创建完成后如下所示:

t_permission(权限表)

DROP TABLE IF EXISTS `t_permission`;
CREATE TABLE `t_permission`  (`id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`code` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '权限标识符',`description` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '描述',`url` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '请求地址',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;INSERT INTO `t_permission` VALUES ('1', 'p1', '测试资源 1', '/r/r1');
INSERT INTO `t_permission` VALUES ('2', 'p3', '测试资源2', '/r/r2');

创建完成后如下所示:

t_user_role(用户角色表)

DROP TABLE IF EXISTS `t_user_role`;
CREATE TABLE `t_user_role`  (`user_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`role_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`create_time` datetime NULL DEFAULT NULL,`creator` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,PRIMARY KEY (`user_id`, `role_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;INSERT INTO `t_user_role` VALUES ('1', '1', NULL, NULL);

创建完成后如下所示:

t_permission(角色权限表)

DROP TABLE IF EXISTS `t_role_permission`;
CREATE TABLE `t_role_permission`  (`role_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`permission_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,PRIMARY KEY (`role_id`, `permission_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;INSERT INTO `t_role_permission` VALUES ('1', '1');
INSERT INTO `t_role_permission` VALUES ('1', '2');

创建完成后如下所示:

五。注册中心搭建

1.父工程pom.xml文件如下所示:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.security</groupId><artifactId>distributed-security</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Greenwich.RELEASE</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><dependency><groupId>javax.interceptor</groupId><artifactId>javax.interceptor-api</artifactId><version>1.2</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-jwt</artifactId><version>1.0.10.RELEASE</version></dependency><dependency><groupId>org.springframework.security.oauth.boot</groupId><artifactId>spring-security-oauth2-autoconfigure</artifactId><version>2.1.3.RELEASE</version></dependency></dependencies></dependencyManagement><build><finalName>${project.name}</finalName><resources><resource><directory>src/main/resources</directory><filtering>true</filtering><includes><include>**/*</include></includes></resource><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource></resources><plugins><!--<plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin>--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target></configuration></plugin><plugin><artifactId>maven-resources-plugin</artifactId><configuration><encoding>utf-8</encoding><useDefaultDelimiters>true</useDefaultDelimiters></configuration></plugin></plugins></build></project>

2.注册中心pom.xml文件如下所示:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>distributed-security</artifactId><groupId>com.security</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>distributed-security-discovery</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies></project>

3.application.yml文件如下:

spring:application:name: distributed-discoveryserver:port: 53000 #启动端口eureka:server:enable-self-preservation: false    #关闭服务器自我保护,客户端心跳检测15分钟内错误达到80%服务会保护,导致别人还认为是好用的服务eviction-interval-timer-in-ms: 10000 #清理间隔(单位毫秒,默认是60*1000)5秒将客户端剔除的服务在服务注册列表中剔除#shouldUseReadOnlyResponseCache: true #eureka是CAP理论种基于AP策略,为了保证强一致性关闭此切换CP 默认不关闭 false关闭client:register-with-eureka: false  #false:不作为一个客户端注册到注册中心fetch-registry: false      #为true时,可以启动,但报异常:Cannot execute request on any known serverinstance-info-replication-interval-seconds: 10serviceUrl:defaultZone: http://localhost:${server.port}/eureka/instance:hostname: ${spring.cloud.client.ip-address}prefer-ip-address: trueinstance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}

4.启动类代码如下:

package com.security.distributed.discovery;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;/*** @author Administrator* @version 1.0**/
@SpringBootApplication
@EnableEurekaServer
public class DiscoveryServer {public static void main(String[] args) {SpringApplication.run(DiscoveryServer.class,args);}
}

搭建完成后,注册中心项目结构如下:

六。创建UAA授权服务工程

1.环境搭建

1.本工程pom.xml文件内容如下所示:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>distributed-security</artifactId><groupId>com.security</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>distributed-security-uaa</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>com.netflix.hystrix</groupId><artifactId>hystrix-javanica</artifactId></dependency><dependency><groupId>org.springframework.retry</groupId><artifactId>spring-retry</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</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-freemarker</artifactId></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-commons</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-security</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-oauth2</artifactId></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-jwt</artifactId></dependency><dependency><groupId>javax.interceptor</groupId><artifactId>javax.interceptor-api</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies></project>

2.application.yml文件内容如下所示:

spring:application:name: uaa-servicemain:# 允许重复服务allow-bean-definition-overriding: truehttp:encoding:enabled: truecharset: utf-8force: truefreemarker:enabled: truesuffix: .htmlrequest-context-attribute: rccontent-type: text/htmlcharset: utf-8mvc:## global exception ###出现错误时, 直接抛出异常throw-exception-if-no-handler-found: trueresources:add-mappings: falsedatasource:url: jdbc:mysql://localhost:3306/user_db?useUnicode=trueusername: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driverserver:port: 53020tomcat:remote_ip_header: x-forwarded-forprotocol_header: x-forwarded-protouse-forward-headers: trueservlet:context-path: /uaalogging:level:root: debugorg:springframework : infoeureka:client:serviceUrl:defaultZone: http://localhost:53000/eureka/instance:preferIpAddress: trueinstance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}feign:hystrix:enabled: truecompression:request:enabled: truemime-types[0]: text/xmlmime-types[1]: application/xmlmime-types[2]: application/jsonmin-request-size: 2048response:enabled: true

3.启动类代码如下:

package com.security.distributed.uaa;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;/*** @author Administrator* @version 1.0**/
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableFeignClients(basePackages = {"com.security.distributed.uaa"})
public class UAAServer {public static void main(String[] args) {SpringApplication.run(UAAServer.class, args);}
}

4.实体类代码如下:

package com.security.distributed.uaa.model;import lombok.Data;/*** @author Administrator* @version 1.0**/
@Data
public class PermissionDto {private String id;private String code;private String description;private String url;
}
package com.security.distributed.uaa.model;import lombok.Data;/*** @author Administrator* @version 1.0**/
@Data
public class UserDto {private String id;private String username;private String password;private String fullname;private String mobile;
}

5.dao层代码如下:

package com.security.distributed.uaa.dao;import com.security.distributed.uaa.model.PermissionDto;
import com.security.distributed.uaa.model.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;import java.util.ArrayList;
import java.util.List;/*** @author Administrator* @version 1.0**/
@Repository
public class UserDao {@AutowiredJdbcTemplate jdbcTemplate;//根据账号查询用户信息public UserDto getUserByUsername(String username){String sql = "select id,username,password,fullname,mobile,sex from t_user where username = ?";//连接数据库查询用户List<UserDto> list = jdbcTemplate.query(sql, new Object[]{username}, new BeanPropertyRowMapper<>(UserDto.class));if(list !=null && list.size()==1){return list.get(0);}return null;}//根据用户id查询用户权限public List<String> findPermissionsByUserId(String userId){String sql = "SELECT * FROM t_permission WHERE id IN(\n" +"\n" +"SELECT permission_id FROM t_role_permission WHERE role_id IN(\n" +"  SELECT role_id FROM t_user_role WHERE user_id = ? \n" +")\n" +")\n";List<PermissionDto> list = jdbcTemplate.query(sql, new Object[]{userId}, new BeanPropertyRowMapper<>(PermissionDto.class));List<String> permissions = new ArrayList<>();list.forEach(c -> permissions.add(c.getCode()));return permissions;}
}

6.security中UserDetailsService认证代码如下:

package com.security.distributed.uaa.service;import com.security.distributed.uaa.dao.UserDao;
import com.security.distributed.uaa.model.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;import java.util.List;/*** @author Administrator* @version 1.0**/
@Service
public class SpringDataUserDetailsService implements UserDetailsService {@AutowiredUserDao userDao;//根据 账号查询用户信息@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {//将来连接数据库根据账号查询用户信息UserDto userDto = userDao.getUserByUsername(username);if(userDto == null){//如果用户查不到,返回null,由provider来抛出异常return null;}//根据用户的id查询用户的权限List<String> permissions = userDao.findPermissionsByUserId(userDto.getId());//将permissions转成数组String[] permissionArray = new String[permissions.size()];permissions.toArray(permissionArray);//将用户名称、用户密码以及用户用户拥有权限放入UserDetails中UserDetails userDetails = User.withUsername(userDto.getUsername()).password(userDto.getPassword()).authorities(permissionArray).build();return userDetails;}
}

2.授权服务器配置

2.1 EnableAuthorizationServer

可以用 @EnableAuthorizationServer 注解并继承AuthorizationServerConfigurerAdapter来配置OAuth2.0 授权 服务器。 在Config包下创建AuthorizationServer:

@Configuration @EnableAuthorizationServer
public class AuthorizationServer extends AuthorizationServerConfigurerAdapter { //略...
}

AuthorizationServerConfigurerAdapter要求配置以下几个类,这几个类是由Spring创建的独立的配置对象,它们 会被Spring传入AuthorizationServerConfigurer中进行配置。源码如下所示:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//package org.springframework.security.oauth2.config.annotation.web.configuration;import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;public class AuthorizationServerConfigurerAdapter implements AuthorizationServerConfigurer {public AuthorizationServerConfigurerAdapter() {}public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {}public void configure(ClientDetailsServiceConfigurer clients) throws Exception {}public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {}
}
  • ClientDetailsServiceConfigurer:用来配置客户端详情服务(ClientDetailsService),客户端详情信息在这里进行初始化,你能够把客户端详情信息写死在这里或者是通过数据库来存储调取详情信息。
  • AuthorizationServerEndpointsConfigurer:用来配置令牌(token)的访问端点和令牌服务(token services)。
  • AuthorizationServerSecurityConfigurer:用来配置令牌端点的安全约束.

2.2 配置客户端详细信息

ClientDetailsServiceConfigurer 能够使用内存或者JDBC来实现客户端详情服务(ClientDetailsService), ClientDetailsService负责查找ClientDetails,而ClientDetails有几个重要的属性如下列表:

  • clientId:(必须的)用来标识客户的Id。
  • secret:(需要值得信任的客户端)客户端安全码,如果有的话。
  • scope:用来限制客户端的访问范围,如果为空(默认)的话,那么客户端拥有全部的访问范围。
  • authorizedGrantTypes:此客户端可以使用的授权类型,默认为空。
  • authorities:此客户端可以使用的权限(基于Spring Security authorities)。

客户端详情(Client Details)能够在应用程序运行的时候进行更新,可以通过访问底层的存储服务(例如将客户 端详情存储在一个关系数据库的表中,就可以使用 JdbcClientDetailsService)或者通过自己实现 ClientRegistrationService接口(同时你也可以实现 ClientDetailsService 接口)来进行管理。

我们暂时使用内存方式存储客户端详情信息,配置如下:

    //客户端详情服务@Overridepublic void configure(ClientDetailsServiceConfigurer clients)throws Exception {//        clients.withClientDetails(clientDetailsService);clients.inMemory()// 使用in-memory存储.withClient("c1")// client_id.secret(new BCryptPasswordEncoder().encode("secret"))//客户端密钥.resourceIds("res1")//资源列表.authorizedGrantTypes("authorization_code", "password","client_credentials","implicit","refresh_token")// 该client允许的授权类型authorization_code,password,refresh_token,implicit,client_credentials.scopes("all")// 允许的授权范围.autoApprove(false)//false跳转到授权页面//加上验证回调地址.redirectUris("http://www.baidu.com");}

2.3 管理令牌

AuthorizationServerTokenServices接口定义了一些操作使得你可以对令牌进行一些必要的管理,令牌可以被用来 加载身份信息,里面包含了这个令牌的相关权限。

自己可以创建 AuthorizationServerTokenServices 这个接口的实现,则需要继承 DefaultTokenServices这个类, 里面包含了一些有用实现,你可以使用它来修改令牌的格式和令牌的存储。默认的,当它尝试创建一个令牌的时 候,是使用随机值来进行填充的,除了持久化令牌是委托一个 TokenStore 接口来实现以外,这个类几乎帮你做了 所有的事情。并且 TokenStore 这个接口有一个默认的实现,它就是 InMemoryTokenStore ,如其命名,所有的 令牌是被保存在了内存中。除了使用这个类以外,你还可以使用一些其他的预定义实现,下面有几个版本,它们都 实现了TokenStore接口:

  • InMemoryTokenStore:这个版本的实现是被默认采用的,它可以完美的工作在单服务器上(即访问并发量压力不大的情况下,并且它在失败的时候不会进行备份),大多数的项目都可以使用这个版本的实现来进行尝试,你可以在开发的时候使用它来进行管理,因为不会被保存到磁盘中,所以更易于调试。
  • JdbcTokenStore:这是一个基于JDBC的实现版本,令牌会被保存进关系型数据库。使用这个版本的实现时, 你可以在不同的服务器之间共享令牌信息,使用这个版本的时候请注意把"spring-jdbc"这个依赖加入到你的 classpath当中。
  • JwtTokenStore:这个版本的全称是 JSON Web Token(JWT),它可以把令牌相关的数据进行编码(因此对 于后端服务来说,它不需要进行存储,这将是一个重大优势),但是它有一个缺点,那就是撤销一个已经授 权令牌将会非常困难,所以它通常用来处理一个生命周期较短的令牌以及撤销刷新令牌(refresh_token)。 另外一个缺点就是这个令牌占用的空间会比较大,如果你加入了比较多用户凭证信息。JwtTokenStore 不会保存任何数据,但是它在转换令牌值以及授权信息方面与DefaultTokenServices 所扮演的角色是一样的。

1、定义TokenConfig

在config包下定义TokenConfig,我们暂时先使用InMemoryTokenStore,生成一个普通的令牌。

@Configuration
public class TokenConfig { @Bean public TokenStore tokenStore() { return new InMemoryTokenStore(); }
}

2、定义AuthorizationServerTokenServices

在AuthorizationServer中定义AuthorizationServerTokenServices

    @Autowiredprivate TokenStore tokenStore;@Autowiredprivate ClientDetailsService clientDetailsService;//令牌管理服务@Beanpublic AuthorizationServerTokenServices tokenService() {DefaultTokenServices service=new DefaultTokenServices();service.setClientDetailsService(clientDetailsService);//客户端详情服务service.setSupportRefreshToken(true);//支持刷新令牌service.setTokenStore(tokenStore);//令牌存储策略service.setAccessTokenValiditySeconds(7200); // 令牌默认有效期2小时service.setRefreshTokenValiditySeconds(259200); // 刷新令牌默认有效期3天return service;}

2.4 令牌访问端点配置

AuthorizationServerEndpointsConfigurer这个对象的实例可以完成令牌服务以及令牌endpoint配置。

配置授权类型(Grant Types)

AuthorizationServerEndpointsConfigurer通过设定以下属性决定支持的授权类型(Grant Types):

  • authenticationManager:认证管理器,当你选择了资源所有者密码(password)授权类型的时候,请设置这个属性注入一个 AuthenticationManager 对象。
  • userDetailsService:如果你设置了这个属性的话,那说明你有一个自己的 UserDetailsService 接口的实现, 或者你可以把这个东西设置到全局域上面去(例如 GlobalAuthenticationManagerConfigurer 这个配置对 象),当你设置了这个之后,那么 “refresh_token” 即刷新令牌授权类型模式的流程中就会包含一个检查,用 来确保这个账号是否仍然有效,假如说你禁用了这个账户的话。
  • authorizationCodeServices:这个属性是用来设置授权码服务的(即 AuthorizationCodeServices 的实例对 象),主要用于 “authorization_code” 授权码类型模式。
  • implicitGrantService:这个属性用于设置隐式授权模式,用来管理隐式授权模式的状态。
  • tokenGranter:当你设置了这个东西(即 TokenGranter 接口实现),那么授权将会交由你来完全掌控,并 且会忽略掉上面的这几个属性,这个属性一般是用作拓展用途的,即标准的四种授权模式已经满足不了你的 需求的时候,才会考虑使用这个。

配置授权端点的URL(Endpoint URLs):

AuthorizationServerEndpointsConfigurer 这个配置对象有一个叫做 pathMapping() 的方法用来配置端点URL链接,它有两个参数:

  • 第一个参数:String 类型的,这个端点URL的默认链接。
  • 第二个参数:String 类型的,你要进行替代的URL链接。

以上的参数都将以 “/” 字符为开始的字符串,框架的默认URL链接如下列表,可以作为这个 pathMapping() 方法的第一个参数:

  • /oauth/authorize:授权端点。
  • /oauth/token:令牌端点。
  • /oauth/confirm_access:用户确认授权提交端点。
  • /oauth/error:授权服务错误信息端点。
  • /oauth/check_token:用于资源服务访问的令牌解析端点。
  • /oauth/token_key:提供公有密匙的端点,如果你使用JWT令牌的话。

需要注意的是授权端点这个URL应该被Spring Security保护起来只供授权用户访问.

在AuthorizationServer配置令牌访问端点

     @Autowiredprivate AuthorizationCodeServices authorizationCodeServices;@Autowiredprivate AuthenticationManager authenticationManager;//设置授权码模式的授权码如何存取,暂时采用内存方式@Beanpublic AuthorizationCodeServices authorizationCodeServices() {return new InMemoryAuthorizationCodeServices();}@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) {endpoints.authenticationManager(authenticationManager)//认证管理器.authorizationCodeServices(authorizationCodeServices)//授权码服务.tokenServices(tokenService())//令牌管理服务.allowedTokenEndpointRequestMethods(HttpMethod.POST);}

2.5 令牌端点的安全约束

AuthorizationServerSecurityConfigurer:用来配置令牌端点(Token Endpoint)的安全约束,在 AuthorizationServer中配置如下:

    @Overridepublic void configure(AuthorizationServerSecurityConfigurer security){security.tokenKeyAccess("permitAll()")             //(1)oauth/token_key是公开.checkTokenAccess("permitAll()")           //(2)oauth/check_token公开.allowFormAuthenticationForClients()        //(3)表单认证(申请令牌);}

(1)tokenkey这个endpoint当使用JwtToken且使用非对称加密时,资源服务用于获取公钥而开放的,这里指这个 endpoint完全公开。
(2)checkToken这个endpoint完全公开
(3) 允许表单认证

AuthorizationServer完整代码如下:

package com.security.distributed.uaa.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.security.oauth2.provider.code.InMemoryAuthorizationCodeServices;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import javax.sql.DataSource;/*** @author Administrator* @version 1.0* 授权服务配置**/
@Configuration
@EnableAuthorizationServer
public class AuthorizationServer extends AuthorizationServerConfigurerAdapter {@Autowiredprivate TokenStore tokenStore;@Autowiredprivate ClientDetailsService clientDetailsService;@Autowiredprivate AuthorizationCodeServices authorizationCodeServices;@Autowiredprivate AuthenticationManager authenticationManager;//客户端详情服务@Overridepublic void configure(ClientDetailsServiceConfigurer clients)throws Exception {//        clients.withClientDetails(clientDetailsService);clients.inMemory()// 使用in-memory存储.withClient("c1")// client_id.secret(new BCryptPasswordEncoder().encode("secret"))//客户端密钥.resourceIds("res1")//资源列表.authorizedGrantTypes("authorization_code", "password","client_credentials","implicit","refresh_token")// 该client允许的授权类型authorization_code,password,refresh_token,implicit,client_credentials.scopes("all")// 允许的授权范围.autoApprove(false)//false跳转到授权页面//加上验证回调地址.redirectUris("http://www.baidu.com");}//令牌管理服务@Beanpublic AuthorizationServerTokenServices tokenService() {DefaultTokenServices service=new DefaultTokenServices();service.setClientDetailsService(clientDetailsService);//客户端详情服务service.setSupportRefreshToken(true);//支持刷新令牌service.setTokenStore(tokenStore);//令牌存储策略service.setAccessTokenValiditySeconds(7200); // 令牌默认有效期2小时service.setRefreshTokenValiditySeconds(259200); // 刷新令牌默认有效期3天return service;}//设置授权码模式的授权码如何存取,暂时采用内存方式@Beanpublic AuthorizationCodeServices authorizationCodeServices() {return new InMemoryAuthorizationCodeServices();}@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) {endpoints.authenticationManager(authenticationManager)//认证管理器.authorizationCodeServices(authorizationCodeServices)//授权码服务.tokenServices(tokenService())//令牌管理服务.allowedTokenEndpointRequestMethods(HttpMethod.POST);}@Overridepublic void configure(AuthorizationServerSecurityConfigurer security){security.tokenKeyAccess("permitAll()")                    //oauth/token_key是公开.checkTokenAccess("permitAll()")                  //oauth/check_token公开.allowFormAuthenticationForClients()               //表单认证(申请令牌);}}

授权服务配置总结:授权服务配置分成三大块,可以关联记忆。

既然要完成认证,它首先得知道客户端信息从哪儿读取,因此要进行客户端详情配置。

既然要颁发token,那必须得定义token的相关endpoint,以及token如何存取,以及客户端支持哪些类型的 token。

既然暴露除了一些endpoint,那对这些endpoint可以定义一些安全上的约束等。

2.6 web安全配置

**WebSecurityConfig **完整代码如下:

package com.security.distributed.uaa.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;/*** @author Administrator* @version 1.0**/
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {//认证管理器@Beanpublic AuthenticationManager authenticationManagerBean() throws Exception {return super.authenticationManagerBean();}//密码编码器@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}//安全拦截机制(最重要)@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable().authorizeRequests().antMatchers("/r/r1").hasAnyAuthority("p1").antMatchers("/login*").permitAll().anyRequest().authenticated().and().formLogin();}
}

3.密码模式

本项目中使用的是密码模式,所以就简单介绍一下密码模式,需要了解其他模式可自行百度

密码模式交互图如下所示:

(1)资源拥有者将用户名、密码发送给客户端

(2)客户端拿着资源拥有者的用户名、密码向授权服务器请求令牌(access_token),请求如下:

/uaa/oauth/token? client_id=c1&client_secret=secret&grant_type=password&username=shangsan&password=123

参数列表如下:

  • client_id:客户端准入标识。
  • client_secret:客户端秘钥。
  • grant_type:授权类型,填写password表示密码模式
  • username:资源拥有者用户名。
  • password:资源拥有者密码。

(3)授权服务器将令牌(access_token)发送给client

这种模式十分简单,但是却意味着直接将用户敏感信息泄漏给了client,因此这就说明这种模式只能用于client是我 们自己开发的情况下。因此密码模式一般用于我们自己开发的,第一方原生App或第一方单页面应用。

测试

POST http://localhost:53020/uaa/oauth/token

请求参数:

返回结果:

若用户名或者密码错误,返回结果为:

若客户端id或者秘钥错误,返回结果为:

4.JWT令牌

4.1 JWT介绍

令牌采用JWT格式后,用户认证通过会得到一个JWT令牌,JWT令牌中已经包括了用户相关的信 息,客户端只需要携带JWT访问资源服务,资源服务根据事先约定的算法自行完成令牌校验,无需每次都请求认证 服务完成授权

什么是JWT?

JSON Web Token(JWT)是一个开放的行业标准(RFC 7519),它定义了一种简介的、自包含的协议格式,用于 在通信双方传递json对象,传递的信息经过数字签名可以被验证和信任。JWT可以使用HMAC算法或使用RSA的公 钥/私钥对来签名,防止被篡改。

官网:https://jwt.io/

标准:https://tools.ietf.org/html/rfc7519

JWT令牌的优点:

1)jwt基于json,非常方便解析。
2)可以在令牌中自定义丰富的内容,易扩展。
3)通过非对称加密算法及数字签名技术,JWT防止篡改,安全性高。
4)资源服务使用JWT可不依赖认证服务即可完成授权。

缺点:

1)JWT令牌较长,占存储空间比较大。

JWT令牌结构

通过学习JWT令牌结构为自定义jwt令牌打好基础。 JWT令牌由三部分组成,每部分中间使用点(.)分隔,比如:xxxxx.yyyyy.zzzzz

Header
头部包括令牌的类型(即JWT)及使用的哈希算法(如HMAC SHA256或RSA) 一个例子如下: 下边是Header部分的内容

{ “alg”: “HS256”, “typ”: “JWT” }

将上边的内容使用Base64Url编码,得到一个字符串就是JWT令牌的第一部分。

Payload
第二部分是负载,内容也是一个json对象,它是存放有效信息的地方,它可以存放jwt提供的现成字段,比 如:iss(签发者),exp(过期时间戳), sub(面向的用户)等,也可自定义字段。 此部分不建议存放敏感信息,因为此部分可以解码还原原始内容。 最后将第二部分负载使用Base64Url编码,得到一个字符串就是JWT令牌的第二部分。 一个例子:

{ “sub”: “1234567890”, “name”: “456”, “admin”: true }

Signature
第三部分是签名,此部分用于防止jwt内容被篡改。 这个部分使用base64url将前两部分进行编码,编码后使用点(.)连接组成字符串,最后使用header中声明 签名算法进行签名。 一个例子:

HMACSHA256( base64UrlEncode(header) + “.” + base64UrlEncode(payload), secret)

base64UrlEncode(header):jwt令牌的第一部分。
base64UrlEncode(payload):jwt令牌的第二部分。
secret:签名所使用的密钥。

4.2 配置JWT令牌服务

在uaa中配置jwt令牌服务,即可实现生成jwt格式的令牌。

1、TokenConfig

package com.security.distributed.uaa.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;/*** @author Administrator* @version 1.0**/
@Configuration
public class TokenConfig {private String SIGNING_KEY = "uaa123";@Beanpublic TokenStore tokenStore() {//JWT令牌存储方案return new JwtTokenStore(accessTokenConverter());}@Beanpublic JwtAccessTokenConverter accessTokenConverter() {JwtAccessTokenConverter converter = new JwtAccessTokenConverter();converter.setSigningKey(SIGNING_KEY); //对称秘钥,资源服务器使用该秘钥来验证return converter;}/*@Beanpublic TokenStore tokenStore() { return new InMemoryTokenStore(); }*/
}

2、定义JWT令牌服务

//令牌管理服务@Beanpublic AuthorizationServerTokenServices tokenService() {DefaultTokenServices service=new DefaultTokenServices();service.setClientDetailsService(clientDetailsService);//客户端详情服务service.setSupportRefreshToken(true);//支持刷新令牌service.setTokenStore(tokenStore);//令牌存储策略//Jwt令牌增强TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();tokenEnhancerChain.setTokenEnhancers(Arrays.asList(accessTokenConverter));service.setTokenEnhancer(tokenEnhancerChain);service.setAccessTokenValiditySeconds(7200); // 令牌默认有效期2小时service.setRefreshTokenValiditySeconds(259200); // 刷新令牌默认有效期3天return service;}

4.3 JWT令牌生成与校验

jwt令牌生成

响应:

令牌申请成功可以使用/uaa/oauth/check_token校验令牌的有效性,并查询令牌的内容,如下所示:

响应:

5. 完善环境配置

截止目前客户端信息和授权码仍然存储在内存中,生产环境中通过会存储在数据库中,下边完善环境的配置:

user_db数据库创建oauth_client_details表来存放客户端信息:

DROP TABLE IF EXISTS `oauth_client_details`;
CREATE TABLE `oauth_client_details`  (`client_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '客户端标识',`resource_ids` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '接入资源列表',`client_secret` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '客户端秘钥',`scope` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,`authorized_grant_types` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,`web_server_redirect_uri` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,`authorities` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,`access_token_validity` int(11) NULL DEFAULT NULL,`refresh_token_validity` int(11) NULL DEFAULT NULL,`additional_information` longtext CHARACTER SET utf8 COLLATE utf8_bin NULL,`create_time` datetime NULL DEFAULT NULL,`archived` tinyint(4) NULL DEFAULT NULL,`trusted` tinyint(4) NULL DEFAULT NULL,`autoapprove` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Compact;INSERT INTO `oauth_client_details` VALUES ('c1', 'res1', '$2a$10$xuOTVuxMgMLog/rTDhzg9.CK80cUyX7UXlLY29TD3oFVtiXd7xCnq', 'ROLE_ADMIN,ROLE_USER,ROLE_API', 'client_credentials,password,authorization_code,implicit,refresh_token', 'http://www.baidu.com', NULL, 7200, 259200, NULL, '2021-01-14 14:06:19', 0, 0, 'false');
INSERT INTO `oauth_client_details` VALUES ('c2', 'res2', '$2a$10$xuOTVuxMgMLog/rTDhzg9.CK80cUyX7UXlLY29TD3oFVtiXd7xCnq', 'ROLE_API', 'client_credentials,password,authorization_code,implicit,refresh_token', 'http://www.baidu.com', NULL, 31536000, 2592000, NULL, '2021-01-14 14:06:23', 0, 0, 'false');

若采用的授权码模式,还需要创建oauth_code来存放授权码信息:

DROP TABLE IF EXISTS `oauth_code`;
CREATE TABLE `oauth_code`  (`create_time` datetime NULL DEFAULT NULL,`code` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`authentication` blob NULL,INDEX `code_index`(`code`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;INSERT INTO `oauth_code` VALUES (NULL, 'bZL5Ii', 0xACED0005737200416F72672E737072696E676672616D65776F726B2E73656375726974792E6F61757468322E70726F76696465722E4F417574683241757468656E7469636174696F6EBD400B02166252130200024C000D73746F7265645265717565737474003C4C6F72672F737072696E676672616D65776F726B2F73656375726974792F6F61757468322F70726F76696465722F4F4175746832526571756573743B4C00127573657241757468656E7469636174696F6E7400324C6F72672F737072696E676672616D65776F726B2F73656375726974792F636F72652F41757468656E7469636174696F6E3B787200476F72672E737072696E676672616D65776F726B2E73656375726974792E61757468656E7469636174696F6E2E416273747261637441757468656E7469636174696F6E546F6B656ED3AA287E6E47640E0200035A000D61757468656E746963617465644C000B617574686F7269746965737400164C6A6176612F7574696C2F436F6C6C656374696F6E3B4C000764657461696C737400124C6A6176612F6C616E672F4F626A6563743B787000737200266A6176612E7574696C2E436F6C6C656374696F6E7324556E6D6F6469666961626C654C697374FC0F2531B5EC8E100200014C00046C6973747400104C6A6176612F7574696C2F4C6973743B7872002C6A6176612E7574696C2E436F6C6C656374696F6E7324556E6D6F6469666961626C65436F6C6C656374696F6E19420080CB5EF71E0200014C00016371007E00047870737200136A6176612E7574696C2E41727261794C6973747881D21D99C7619D03000149000473697A65787000000002770400000002737200426F72672E737072696E676672616D65776F726B2E73656375726974792E636F72652E617574686F726974792E53696D706C654772616E746564417574686F7269747900000000000001FE0200014C0004726F6C657400124C6A6176612F6C616E672F537472696E673B787074000270317371007E000D74000270337871007E000C707372003A6F72672E737072696E676672616D65776F726B2E73656375726974792E6F61757468322E70726F76696465722E4F41757468325265717565737400000000000000010200075A0008617070726F7665644C000B617574686F72697469657371007E00044C000A657874656E73696F6E7374000F4C6A6176612F7574696C2F4D61703B4C000B726564697265637455726971007E000E4C00077265667265736874003B4C6F72672F737072696E676672616D65776F726B2F73656375726974792F6F61757468322F70726F76696465722F546F6B656E526571756573743B4C000B7265736F7572636549647374000F4C6A6176612F7574696C2F5365743B4C000D726573706F6E7365547970657371007E0016787200386F72672E737072696E676672616D65776F726B2E73656375726974792E6F61757468322E70726F76696465722E426173655265717565737436287A3EA37169BD0200034C0008636C69656E74496471007E000E4C001172657175657374506172616D657465727371007E00144C000573636F706571007E001678707400026331737200256A6176612E7574696C2E436F6C6C656374696F6E7324556E6D6F6469666961626C654D6170F1A5A8FE74F507420200014C00016D71007E00147870737200116A6176612E7574696C2E486173684D61700507DAC1C31660D103000246000A6C6F6164466163746F724900097468726573686F6C6478703F400000000000037708000000040000000274000D726573706F6E73655F74797065740004636F6465740009636C69656E745F696471007E001978737200256A6176612E7574696C2E436F6C6C656374696F6E7324556E6D6F6469666961626C65536574801D92D18F9B80550200007871007E0009737200176A6176612E7574696C2E4C696E6B656448617368536574D86CD75A95DD2A1E020000787200116A6176612E7574696C2E48617368536574BA44859596B8B7340300007870770C000000103F40000000000003740008524F4C455F415049740009524F4C455F5553455274000A524F4C455F41444D494E78017371007E0024770C000000103F40000000000000787371007E001C3F400000000000007708000000100000000078740014687474703A2F2F7777772E62616964752E636F6D707371007E0024770C000000103F4000000000000174000472657331787371007E0024770C000000103F4000000000000171007E001F787372004F6F72672E737072696E676672616D65776F726B2E73656375726974792E61757468656E7469636174696F6E2E557365726E616D6550617373776F726441757468656E7469636174696F6E546F6B656E00000000000001FE0200024C000B63726564656E7469616C7371007E00054C00097072696E636970616C71007E00057871007E0003017371007E00077371007E000B0000000277040000000271007E000F71007E00117871007E0032737200486F72672E737072696E676672616D65776F726B2E73656375726974792E7765622E61757468656E7469636174696F6E2E57656241757468656E7469636174696F6E44657461696C7300000000000001FE0200024C000D72656D6F74654164647265737371007E000E4C000973657373696F6E496471007E000E787074000F303A303A303A303A303A303A303A31740020334541313031353745454238384633373130394536464339443730453641343270737200326F72672E737072696E676672616D65776F726B2E73656375726974792E636F72652E7573657264657461696C732E5573657200000000000001FE0200075A00116163636F756E744E6F6E457870697265645A00106163636F756E744E6F6E4C6F636B65645A001563726564656E7469616C734E6F6E457870697265645A0007656E61626C65644C000B617574686F72697469657371007E00164C000870617373776F726471007E000E4C0008757365726E616D6571007E000E7870010101017371007E0021737200116A6176612E7574696C2E54726565536574DD98509395ED875B0300007870737200466F72672E737072696E676672616D65776F726B2E73656375726974792E636F72652E7573657264657461696C732E5573657224417574686F72697479436F6D70617261746F7200000000000001FE020000787077040000000271007E000F71007E001178707400957B2266756C6C6E616D65223A22E5BCA0E4B889222C226964223A2231222C226D6F62696C65223A223132333435363734383931222C2270617373776F7264223A2224326124313024334D59484C4776596B5967434B5241584E6545445465316737417A6E562F4E6937706D5850327A4430563059532F486F7342616D75222C22757365726E616D65223A227A68616E6773616E227D);

配置授权服务,修改AuthorizationServer,让ClientDetailsService和AuthorizationCodeServices从数据库读取数据,修改后完整代码如下所示:

package com.security.distributed.uaa.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.security.oauth2.provider.code.InMemoryAuthorizationCodeServices;
import org.springframework.security.oauth2.provider.code.JdbcAuthorizationCodeServices;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;import javax.sql.DataSource;
import java.util.Arrays;/*** @author Administrator* @version 1.0* 授权服务配置**/
@Configuration
@EnableAuthorizationServer
public class AuthorizationServer extends AuthorizationServerConfigurerAdapter {@Autowiredprivate TokenStore tokenStore;@Autowiredprivate ClientDetailsService clientDetailsService;@Autowiredprivate AuthorizationCodeServices authorizationCodeServices;@Autowiredprivate AuthenticationManager authenticationManager;@Autowiredprivate JwtAccessTokenConverter accessTokenConverter;@AutowiredPasswordEncoder passwordEncoder;//将客户端信息存储到数据库@Beanpublic ClientDetailsService clientDetailsService(DataSource dataSource) {ClientDetailsService clientDetailsService = new JdbcClientDetailsService(dataSource);((JdbcClientDetailsService) clientDetailsService).setPasswordEncoder(passwordEncoder);return clientDetailsService;}//客户端详情服务@Overridepublic void configure(ClientDetailsServiceConfigurer clients)throws Exception {//        clients.withClientDetails(clientDetailsService);clients.inMemory()// 使用in-memory存储.withClient("c1")// client_id.secret(new BCryptPasswordEncoder().encode("secret"))//客户端密钥.resourceIds("res1")//资源列表.authorizedGrantTypes("authorization_code", "password","client_credentials","implicit","refresh_token")// 该client允许的授权类型authorization_code,password,refresh_token,implicit,client_credentials.scopes("all")// 允许的授权范围.autoApprove(false)//false跳转到授权页面//加上验证回调地址.redirectUris("http://www.baidu.com");}//令牌管理服务@Beanpublic AuthorizationServerTokenServices tokenService() {DefaultTokenServices service=new DefaultTokenServices();service.setClientDetailsService(clientDetailsService);//客户端详情服务service.setSupportRefreshToken(true);//支持刷新令牌service.setTokenStore(tokenStore);//令牌存储策略//Jwt令牌增强TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();tokenEnhancerChain.setTokenEnhancers(Arrays.asList(accessTokenConverter));service.setTokenEnhancer(tokenEnhancerChain);service.setAccessTokenValiditySeconds(7200); // 令牌默认有效期2小时service.setRefreshTokenValiditySeconds(259200); // 刷新令牌默认有效期3天return service;}//设置授权码模式的授权码如何存取,暂时采用内存方式/*@Beanpublic AuthorizationCodeServices authorizationCodeServices() {return new InMemoryAuthorizationCodeServices();}*/@Beanpublic AuthorizationCodeServices authorizationCodeServices(DataSource dataSource) {return new JdbcAuthorizationCodeServices(dataSource);//设置授权码模式的授权码如何存取}@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) {endpoints.authenticationManager(authenticationManager)//认证管理器.authorizationCodeServices(authorizationCodeServices)//授权码服务.tokenServices(tokenService())//令牌管理服务.allowedTokenEndpointRequestMethods(HttpMethod.POST);}@Overridepublic void configure(AuthorizationServerSecurityConfigurer security){security.tokenKeyAccess("permitAll()")                    //oauth/token_key是公开.checkTokenAccess("permitAll()")                  //oauth/check_token公开.allowFormAuthenticationForClients()             //表单认证(申请令牌);}}

重新测试获取jwt令牌并校验。

全部步骤完成后,项目结构如下所示:

七。搭建网关工程

1.环境配置

1.pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>distributed-security</artifactId><groupId>com.security</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>distributed-security-gateway</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>com.netflix.hystrix</groupId><artifactId>hystrix-javanica</artifactId></dependency><dependency><groupId>org.springframework.retry</groupId><artifactId>spring-retry</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-zuul</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-security</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-oauth2</artifactId></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-jwt</artifactId></dependency><dependency><groupId>javax.interceptor</groupId><artifactId>javax.interceptor-api</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies></project>

2.application.yml:

spring:application:name: gateway-servermain:allow-bean-definition-overriding: true
server:port: 53010logging:level:root: infoorg:springframework : infozuul:retryable: trueignored-services: '*'add-host-header: truesensitiveHeaders: '*'routes:#uua-service路由配置uaa-service.stripPrefix: falseuaa-service.path: /uaa/**#order-service路由配置order-service.stripPrefix: falseorder-service.path: /order/**eureka:client:serviceUrl:defaultZone: http://localhost:53000/eureka/instance:preferIpAddress: trueinstance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}management:endpoints:web:exposure:include: refresh,health,info,envfeign:hystrix:enabled: truecompression:request:enabled: truemime-types[0]: text/xmlmime-types[1]: application/xmlmime-types[2]: application/jsonmin-request-size: 2048response:enabled: true

3.启动类代码如下:

package com.security.distributed.gateway;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;/*** @author Administrator* @version 1.0**/
@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class GatewayServer {public static void main(String[] args) {SpringApplication.run(GatewayServer.class, args);}
}

2.token配置

资源服务器由于需要验证并解析令牌,往往可以通过在授权服务器暴露check_token的Endpoint来 完成,而我们在授权服务器使用的是对称加密的jwt,因此知道密钥即可,资源服务与授权服务本就是对称设计, 那我们把授权服务的TokenConfig两个类拷贝过来就行 。

package com.security.distributed.gateway.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;/*** @author Administrator* @version 1.0**/
@Configuration
public class TokenConfig {private String SIGNING_KEY = "uaa123";@Beanpublic TokenStore tokenStore() {//JWT令牌存储方案return new JwtTokenStore(accessTokenConverter());}@Beanpublic JwtAccessTokenConverter accessTokenConverter() {JwtAccessTokenConverter converter = new JwtAccessTokenConverter();converter.setSigningKey(SIGNING_KEY); //对称秘钥,资源服务器使用该秘钥来验证return converter;}}

3.配置资源服务

package com.security.distributed.gateway.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;/*** @author Administrator* @version 1.0**/
@Configuration
public class ResouceServerConfig  {public static final String RESOURCE_ID = "res1";//uaa资源服务配置@Configuration@EnableResourceServerpublic class UAAServerConfig extends ResourceServerConfigurerAdapter {@Autowiredprivate TokenStore tokenStore;@Overridepublic void configure(ResourceServerSecurityConfigurer resources){resources.tokenStore(tokenStore).resourceId(RESOURCE_ID).stateless(true);}@Overridepublic void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/uaa/**").permitAll();}}//order资源//uaa资源服务配置@Configuration@EnableResourceServerpublic class OrderServerConfig extends ResourceServerConfigurerAdapter {@Autowiredprivate TokenStore tokenStore;@Overridepublic void configure(ResourceServerSecurityConfigurer resources){resources.tokenStore(tokenStore).resourceId(RESOURCE_ID).stateless(true);}@Overridepublic void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/order/**").access("#oauth2.hasScope('ROLE_API')");}}//配置其它的资源服务..}

上面定义了两个微服务的资源,其中: UAAServerConfig指定了若请求匹配/uaa/**

网关不进行拦截。 OrderServerConfig指定了若请求匹配/order/** ,也就是访问统一用户服务,接入客户端需要有scope中包含 read,并且authorities(权限)中需要包含ROLE_USER。 由于res1这个接入客户端,read包括ROLE_ADMIN,ROLE_USER,ROLE_API三个权限。

4.安全配置

package com.security.distributed.gateway.config;import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/*** @author Administrator* @version 1.0**/
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/**").permitAll().and().csrf().disable();}
}

5. 转发明文token给微服务

通过Zuul过滤器的方式实现,目的是让下游微服务能够很方便的获取到当前的登录用户信息(明文token)

(1)实现Zuul前置过滤器,完成当前登录用户信息提取,并放入转发微服务的request中

package com.security.distributed.gateway.filter;import com.alibaba.fastjson.JSON;
import com.security.distributed.gateway.common.EncryptUtil;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** @author Administrator* @version 1.0**/
public class AuthFilter extends ZuulFilter {@Overridepublic boolean shouldFilter() {return true;}@Overridepublic String filterType() {return "pre";}@Overridepublic int filterOrder() {return 0;}@Overridepublic Object run() throws ZuulException {RequestContext ctx = RequestContext.getCurrentContext();//从安全上下文中拿 到用户身份对象Authentication authentication = SecurityContextHolder.getContext().getAuthentication();if(!(authentication instanceof OAuth2Authentication)){return null;}OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) authentication;Authentication userAuthentication = oAuth2Authentication.getUserAuthentication();//取出用户身份信息String principal = userAuthentication.getName();//取出用户权限List<String> authorities = new ArrayList<>();//从userAuthentication取出权限,放在authoritiesuserAuthentication.getAuthorities().stream().forEach(c->authorities.add(((GrantedAuthority) c).getAuthority()));OAuth2Request oAuth2Request = oAuth2Authentication.getOAuth2Request();Map<String, String> requestParameters = oAuth2Request.getRequestParameters();Map<String,Object> jsonToken = new HashMap<>(requestParameters);if(userAuthentication!=null){jsonToken.put("principal",principal);jsonToken.put("authorities",authorities);}//把身份信息和权限信息放在json中,加入http的header中,转发给微服务ctx.addZuulRequestHeader("json-token", EncryptUtil.encodeUTF8StringBase64(JSON.toJSONString(jsonToken)));return null;}
}

解析token工具类如下所示:

package com.security.distributed.gateway.common;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Base64;public class EncryptUtil {private static final Logger logger = LoggerFactory.getLogger(EncryptUtil.class);public static String encodeBase64(byte[] bytes){String encoded = Base64.getEncoder().encodeToString(bytes);return encoded;}public static byte[]  decodeBase64(String str){byte[] bytes = null;bytes = Base64.getDecoder().decode(str);return bytes;}public static String encodeUTF8StringBase64(String str){String encoded = null;try {encoded = Base64.getEncoder().encodeToString(str.getBytes("utf-8"));} catch (UnsupportedEncodingException e) {logger.warn("不支持的编码格式",e);}return encoded;}public static String  decodeUTF8StringBase64(String str){String decoded = null;byte[] bytes = Base64.getDecoder().decode(str);try {decoded = new String(bytes,"utf-8");}catch(UnsupportedEncodingException e){logger.warn("不支持的编码格式",e);}return decoded;}public static String encodeURL(String url) {String encoded = null;try {encoded =  URLEncoder.encode(url, "utf-8");} catch (UnsupportedEncodingException e) {logger.warn("URLEncode失败", e);}return encoded;}public static String decodeURL(String url) {String decoded = null;try {decoded = URLDecoder.decode(url, "utf-8");} catch (UnsupportedEncodingException e) {logger.warn("URLDecode失败", e);}return decoded;}public static void main(String [] args){String str = "abcd{'a':'b'}";String encoded = EncryptUtil.encodeUTF8StringBase64(str);String decoded = EncryptUtil.decodeUTF8StringBase64(encoded);System.out.println(str);System.out.println(encoded);System.out.println(decoded);String url = "== wo";String urlEncoded = EncryptUtil.encodeURL(url);String urlDecoded = EncryptUtil.decodeURL(urlEncoded);System.out.println(url);System.out.println(urlEncoded);System.out.println(urlDecoded);}}

(2)将filter纳入spring 容器: 配置AuthFilter

package com.security.distributed.gateway.config;import com.security.distributed.gateway.filter.AuthFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;/*** @author Administrator* @version 1.0**/
@Configuration
public class ZuulConfig {@Beanpublic AuthFilter preFileter() {return new AuthFilter();}@Beanpublic FilterRegistrationBean corsFilter() {final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();final CorsConfiguration config = new CorsConfiguration();config.setAllowCredentials(true);config.addAllowedOrigin("*");config.addAllowedHeader("*");config.addAllowedMethod("*");config.setMaxAge(18000L);source.registerCorsConfiguration("/**", config);CorsFilter corsFilter = new CorsFilter(source);FilterRegistrationBean bean = new FilterRegistrationBean(corsFilter);bean.setOrder(Ordered.HIGHEST_PRECEDENCE);return bean;}
}

全部步骤完成后,项目结构如下所示:

八。创建order资源服务工程

1.环境配置

1.pom.xml:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>distributed-security</artifactId><groupId>com.security</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>distributed-security-order</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-security</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-oauth2</artifactId></dependency><dependency><groupId>javax.interceptor</groupId><artifactId>javax.interceptor-api</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies></project>

2.application.yml:

spring:application:name: order-servicemain:allow-bean-definition-overriding: truehttp:encoding:enabled: truecharset: utf-8force: truefreemarker:enabled: truesuffix: .htmlrequest-context-attribute: rccontent-type: text/htmlcharset: utf-8mvc:throw-exception-if-no-handler-found: trueresources:add-mappings: falseserver:port: 53021tomcat:remote_ip_header: x-forwarded-forprotocol_header: x-forwarded-protouse-forward-headers: trueservlet:context-path: /orderlogging:level:root: debugorg:springframework : infoeureka:client:serviceUrl:defaultZone: http://localhost:53000/eureka/instance:preferIpAddress: trueinstance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}management:endpoints:web:exposure:include: refresh,health,info,envfeign:hystrix:enabled: truecompression:request:enabled: truemime-types[0]: text/xmlmime-types[1]: application/xmlmime-types[2]: application/jsonmin-request-size: 2048response:enabled: true

3.启动类:

package com.security.distributed.order;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/*** @author Administrator* @version 1.0**/
@SpringBootApplication
@EnableDiscoveryClient
public class OrderServer {public static void main(String[] args) {SpringApplication.run(OrderServer.class, args);}
}

2.token配置

TokenConfig与授权服务以及网关一致。

3.配置资源服务

package com.security.distributed.order.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;/*** @author Administrator* @version 1.0**/
@Configuration
@EnableResourceServer
public class ResouceServerConfig extends ResourceServerConfigurerAdapter {public static final String RESOURCE_ID = "res1";@AutowiredTokenStore tokenStore;@Overridepublic void configure(ResourceServerSecurityConfigurer resources) {resources.resourceId(RESOURCE_ID)//资源 id.tokenStore(tokenStore)
//                .tokenServices(tokenService())//验证令牌的服务.stateless(true);}@Overridepublic void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/**").access("#oauth2.hasScope('ROLE_ADMIN')").and().csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);}}

4.安全配置

package com.security.distributed.order.config;import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/*** @author Administrator* @version 1.0**/
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {//安全拦截机制(最重要)@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable().authorizeRequests().antMatchers("/r/**").authenticated()//所有/r/**的请求必须认证通过.anyRequest().permitAll()//除了/r/**,其它的请求可以访问;}
}

4.过滤器配置

项目过滤器,目的是解析网关模块转发过来的token信息:

package com.security.distributed.order.filter;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.security.distributed.order.common.EncryptUtil;
import com.security.distributed.order.model.UserDTO;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;/*** @author Administrator* @version 1.0**/
@Component
public class TokenAuthenticationFilter extends OncePerRequestFilter {@Overrideprotected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {//解析出头中的tokenString token = httpServletRequest.getHeader("json-token");if(token!=null){String json = EncryptUtil.decodeUTF8StringBase64(token);//将token转成json对象JSONObject jsonObject = JSON.parseObject(json);//用户身份信息UserDTO userDTO = new UserDTO();String principal = jsonObject.getString("principal");userDTO.setUsername(principal);//用户权限JSONArray authoritiesArray = jsonObject.getJSONArray("authorities");String[] authorities = authoritiesArray.toArray(new String[authoritiesArray.size()]);//将用户信息和权限填充 到用户身份token对象中UsernamePasswordAuthenticationToken authenticationToken= new UsernamePasswordAuthenticationToken(userDTO,null, AuthorityUtils.createAuthorityList(authorities));authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));//将authenticationToken填充到安全上下文SecurityContextHolder.getContext().setAuthentication(authenticationToken);}filterChain.doFilter(httpServletRequest,httpServletResponse);}
}

5.控制器示例代码

package com.security.distributed.order.controller;import com.security.distributed.order.model.UserDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author Administrator* @version 1.0**/
@RestController
public class OrderController {@GetMapping(value = "/r1")@PreAuthorize("hasAuthority('p1')")//拥有p1权限方可访问此urlpublic String r1(){//获取用户身份信息UserDTO  userDTO = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();return userDTO.getUsername()+"访问资源1";}@GetMapping(value = "/getUserDTO")@PreAuthorize("hasAuthority('p1')")//拥有p1权限方可访问此urlpublic UserDTO getUserDTO(){//获取用户身份信息UserDTO  userDTO = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();return userDTO;}}

九。集成测试

本案例测试过程描述:

1、采用OAuth2.0的密码模式从UAA获取token并校验
2、使用该token通过网关访问订单服务的测试资源

(1)过网关访问uaa的授权及获取令牌,获取token。注意端口是53010,网关的端口。

请求

响应:

(2)校验token

请求:

响应:

(3)使用Token过网关访问订单服务中的测试资源进行测试。

请求1:(需要携带token信息)

响应1:

请求2:(需要携带token信息)

响应2:

(4)破坏token测试

无token测试返回内容:

破坏token测试返回内容:

十。扩展用户信息

1.需求分析

目前jwt令牌存储了用户的身份信息、权限信息,网关将token明文化转发给微服务使用,目前用户身份信息仅包括 了用户的账号,微服务还需要用户的ID、手机号等重要信息。实际项目中用户身份信息中还包含了诸如性别,班级,年龄等等具象化信息。

所以,本案例将提供扩展用户信息的思路和方法,满足微服务使用用户信息的需求。

下边分析JWT令牌中扩展用户信息的方案:

在认证阶段DaoAuthenticationProvider会调用UserDetailService查询用户的信息,这里是可以获取到齐全的用户 信息的。由于JWT令牌中用户身份信息来源于UserDetails,UserDetails中仅定义了username为用户的身份信息, 这里有两个思路:第一是可以扩展UserDetails,使之包括更多的自定义属性,第二也可以扩展username的内容 ,比如存入json数据内容作为username的内容。相比较而言,方案二比较简单还不用破坏UserDetails的结构,我 们采用方案二。

2.修改userDTO

新加入sex属性:

3.修改UserDetailService

从数据库查询到user,将整体user转成json存入userDetails对象。

修改前:

修改后:

4.修改资源服务过虑器

5.测试

重启项目,再次进行测试,发现用户信息已加上sex属性:

请求:

响应:

十一。项目地址

该项目已经上传至百度网盘,有需要的小伙伴可自行下载:

链接:https://pan.baidu.com/s/1IxshMb12FmKrSUbOk4IXzw
提取码:0662

从零开始超详细的Spring Security OAuth2.0实现分布式系统授权(注册中心+网关+认证授权服务(JWT令牌验证)+资源调用服务)相关推荐

  1. Spring Security OAuth2.0认证授权知识概括

    Spring Security OAuth2.0认证授权知识概括 安全框架基本概念 基于Session的认证方式 Spring Security简介 SpringSecurity详解 分布式系统认证方 ...

  2. Spring Security OAuth2.0认证授权

    文章目录 1.基本概念 1.1.什么是认证 1.2 什么是会话 1.3什么是授权 1.4授权的数据模型 1.4 RBAC 1.4.1 基于角色的访问控制 2.基于Session的认证方式 3.整合案例 ...

  3. Spring Security OAuth2.0认证授权三:使用JWT令牌

    历史文章 [Spring Security OAuth2.0认证授权一:框架搭建和认证测试] [Spring Security OAuth2.0认证授权二:搭建资源服务] 前面两篇文章详细讲解了如何基 ...

  4. Spring Security OAuth2.0认证授权五:用户信息扩展到jwt

    历史文章 [Spring Security OAuth2.0认证授权一:框架搭建和认证测试] [Spring Security OAuth2.0认证授权二:搭建资源服务] [Spring Securi ...

  5. OAuth2.0学习(2-1)Spring Security OAuth2.0 开发指南

    开发指南:http://www.cnblogs.com/xingxueliao/p/5911292.html Spring OAuth2.0 提供者实现原理: Spring OAuth2.0提供者实际 ...

  6. Spring Security oauth2.0微信小程序登录

    微信小程序前期开发准备,可以参考这篇文章微信小程序前期准备 1.学习过Spring Secrity oauth2.0的都知道,他有四种登录模式可以选择 authorization code(授权码模式 ...

  7. Spring Security + OAuth2.0

    授权服务器 授权服务器中有4个端点.说明如下: Authorize Endpoint :授权端点,进行授权. Token Endpoint :令牌端点,经过授权拿到对应的Token. lntrospe ...

  8. Spring Security OAuth2.0 token生成与刷新机制源码阅读

    一.介绍 Spring Security Oauth2是目前市面上非常流行的实现了OAuth2.0协议的权限框架.本文会介绍其是如何获取token以及刷新token的. 二.AbstractEndPo ...

  9. Spring Security OAuth2.0笔记

    请求命中filter: spring-security-oauth2-client:5.3.5.RELEASE org.springframework.security.oauth2.client.w ...

最新文章

  1. 使用Python和OpenCV进行文本偏斜校正
  2. (C#)AJAX post方式传值
  3. sqlite导入 mysql_Sqlite向MySql导入数据
  4. 基于JAVA的生产者消费者问题
  5. jsp基础语法【05】_跳转指令
  6. 如何嵌入Live Messenger?
  7. SAP UI5 应用开发教程之四十 - 如何制作包含了 component-preload.js 在内的 SAP UI5 发布版本
  8. memcache单机版安装
  9. 获取Linux命令源代码的方法【ZT】
  10. 1.jenkins持续集成-jenkins安装
  11. mysql搭建及数据迁移教程
  12. 白盒测试哪种测试效果好_白盒测试与黑盒测试区别(简答题)简短一些不要长的谢谢...
  13. 处理器架构 (十三) ARMv6架构下 各微架构的不同
  14. 新路由3 Newifi3 D2 Lede固件
  15. mysql笔记--03DML
  16. 广东省内免费试用 | 医院绩效考核病案首页系统,解决医院上报难题的智能助手
  17. Error in nnet.default(x, y, w, ...) : too many (2651) weights
  18. 中国版权保护中心注册流程(含实名认证)
  19. mysql五日均线代码_5日均线--攻击线
  20. centos测试主机网络极限速度

热门文章

  1. matlab如何提取某一列的数据_装载: Matlab 提取矩阵 某一行 或者 某一列 的方法...
  2. 项目总结:凡事预则立,不预则废!
  3. Excel批量合并单元格
  4. 婚礼上新郎经典发言稿(绝对感动)我流泪了!!!
  5. Opencv-锐化增强算法(USM)
  6. 苹果数据线不能充电_小米讽刺苹果取消充电器,网友翻旧账打脸,始作俑者竟是雷军|充电器|数据线|手机...
  7. 最优惠租云服务器_租一年阿里云服务器多少钱,怎么租更便宜
  8. 【无标题】对于十进制转化为二进制的基础方法
  9. Java缓存面试题——Redis解决方案
  10. 2020搞个 Mac 玩玩