当前位置:   article > 正文

SpringCloud Gateway + Jwt + Oauth2 实现网关的鉴权操作

@enablewebfluxsecurity 引入哪个包

点击上方“芋道源码”,选择“设为星标

管她前浪,还是后浪?

能浪的浪,才是好浪!

每天 10:33 更新文章,每天掉亿点点头发...

源码精品专栏

 

来源:juejin.cn/post/

7000353332824899614


一、背景

随着我们的微服务越来越多,如果每个微服务都要自己去实现一套鉴权操作,那么这么操作比较冗余,因此我们可以把鉴权操作统一放到网关去做,如果微服务自己有额外的鉴权处理,可以在自己的微服务中处理。

基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能

  • 项目地址:https://gitee.com/zhijiantianya/ruoyi-vue-pro

  • 视频教程:https://doc.iocoder.cn/video/

二、需求

1、在网关层完成url层面的鉴权操作。

  • 所有的OPTION请求都放行。

  • 所有不存在请求,直接都拒绝访问。

  • user-provider服务的findAllUsers需要 user.userInfo权限才可以访问。

2、将解析后的jwt token当做请求头传递到下游服务中。3、整合Spring Security Oauth2 Resource Server

基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能

  • 项目地址:https://gitee.com/zhijiantianya/yudao-cloud

  • 视频教程:https://doc.iocoder.cn/video/

三、前置条件

1、搭建一个可用的认证服务器

https://juejin.cn/post/6985411823144615972

2、知道Spring Security Oauth2 Resource Server资源服务器如何使用

https://juejin.cn/post/6985893815500406791

四、项目结构

e70f5526a2f6313a3e0774e6dab6ab79.jpeg
项目结构

五、网关层代码的编写

1、引入jar包

  1. <dependency>
  2.     <groupId>com.alibaba.cloud</groupId>
  3.     <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  4. </dependency>
  5. <dependency>
  6.     <groupId>org.springframework.cloud</groupId>
  7.     <artifactId>spring-cloud-starter-gateway</artifactId>
  8. </dependency>
  9. <dependency>
  10.     <groupId>org.springframework.boot</groupId>
  11.     <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
  12. </dependency>
  13. <dependency>
  14.     <groupId>org.springframework.boot</groupId>
  15.     <artifactId>spring-boot-starter-security</artifactId>
  16. </dependency>
  17. <dependency>
  18.     <groupId>org.springframework.cloud</groupId>
  19.     <artifactId>spring-cloud-starter-loadbalancer</artifactId>
  20. </dependency>

2、自定义授权管理器

自定义授权管理器,判断用户是否有权限访问

此处我们简单判断

  • 放行所有的 OPTION 请求。

  • 判断某个请求(url)用户是否有权限访问。

  • 所有不存在的请求(url)直接无权限访问。

  1. package com.huan.study.gateway.config;
  2. import com.google.common.collect.Maps;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.http.HttpMethod;
  5. import org.springframework.http.server.reactive.ServerHttpRequest;
  6. import org.springframework.security.authentication.AbstractAuthenticationToken;
  7. import org.springframework.security.authorization.AuthorizationDecision;
  8. import org.springframework.security.authorization.ReactiveAuthorizationManager;
  9. import org.springframework.security.core.Authentication;
  10. import org.springframework.security.core.GrantedAuthority;
  11. import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
  12. import org.springframework.security.web.server.authorization.AuthorizationContext;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.util.AntPathMatcher;
  15. import org.springframework.util.PathMatcher;
  16. import org.springframework.util.StringUtils;
  17. import org.springframework.web.server.ServerWebExchange;
  18. import reactor.core.publisher.Mono;
  19. import javax.annotation.PostConstruct;
  20. import java.util.Map;
  21. import java.util.Objects;
  22. /**
  23.  * 自定义授权管理器,判断用户是否有权限访问
  24.  */
  25. @Component
  26. @Slf4j
  27. public class CustomReactiveAuthorizationManager implements ReactiveAuthorizationManager<AuthorizationContext> {
  28.     /**
  29.      * 此处保存的是资源对应的权限,可以从数据库中获取
  30.      */
  31.     private static final Map<String, String> AUTH_MAP = Maps.newConcurrentMap();
  32.     @PostConstruct
  33.     public void initAuthMap() {
  34.         AUTH_MAP.put("/user/findAllUsers""user.userInfo");
  35.         AUTH_MAP.put("/user/addUser""ROLE_ADMIN");
  36.     }
  37.     @Override
  38.     public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, AuthorizationContext authorizationContext) {
  39.         ServerWebExchange exchange = authorizationContext.getExchange();
  40.         ServerHttpRequest request = exchange.getRequest();
  41.         String path = request.getURI().getPath();
  42.         // 带通配符的可以使用这个进行匹配
  43.         PathMatcher pathMatcher = new AntPathMatcher();
  44.         String authorities = AUTH_MAP.get(path);
  45.         log.info("访问路径:[{}],所需要的权限是:[{}]", path, authorities);
  46.         // option 请求,全部放行
  47.         if (request.getMethod() == HttpMethod.OPTIONS) {
  48.             return Mono.just(new AuthorizationDecision(true));
  49.         }
  50.         // 不在权限范围内的url,全部拒绝
  51.         if (!StringUtils.hasText(authorities)) {
  52.             return Mono.just(new AuthorizationDecision(false));
  53.         }
  54.         return authentication
  55.                 .filter(Authentication::isAuthenticated)
  56.                 .filter(a -> a instanceof JwtAuthenticationToken)
  57.                 .cast(JwtAuthenticationToken.class)
  58.                 .doOnNext(token -> {
  59.                     System.out.println(token.getToken().getHeaders());
  60.                     System.out.println(token.getTokenAttributes());
  61.                 })
  62.                 .flatMapIterable(AbstractAuthenticationToken::getAuthorities)
  63.                 .map(GrantedAuthority::getAuthority)
  64.                 .any(authority -> Objects.equals(authority, authorities))
  65.                 .map(AuthorizationDecision::new)
  66.                 .defaultIfEmpty(new AuthorizationDecision(false));
  67.     }
  68. }

3、token认证失败、或超时的处理

  1. package com.huan.study.gateway.config;
  2. import org.springframework.core.io.buffer.DataBuffer;
  3. import org.springframework.core.io.buffer.DataBufferUtils;
  4. import org.springframework.http.HttpStatus;
  5. import org.springframework.security.core.AuthenticationException;
  6. import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
  7. import org.springframework.web.server.ServerWebExchange;
  8. import reactor.core.publisher.Mono;
  9. import java.nio.charset.StandardCharsets;
  10. /**
  11.  * 认证失败异常处理
  12.  */
  13. public class CustomServerAuthenticationEntryPoint implements ServerAuthenticationEntryPoint {
  14.     @Override
  15.     public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException ex) {
  16.         return Mono.defer(() -> Mono.just(exchange.getResponse()))
  17.                 .flatMap(response -> {
  18.                     response.setStatusCode(HttpStatus.UNAUTHORIZED);
  19.                     String body = "{\"code\":401,\"msg\":\"token不合法或过期\"}";
  20.                     DataBuffer buffer = response.bufferFactory().wrap(body.getBytes(StandardCharsets.UTF_8));
  21.                     return response.writeWith(Mono.just(buffer))
  22.                             .doOnError(error -> DataBufferUtils.release(buffer));
  23.                 });
  24.     }
  25. }

4、用户没有权限的处理

  1. package com.huan.study.gateway.config;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.core.io.buffer.DataBuffer;
  5. import org.springframework.core.io.buffer.DataBufferUtils;
  6. import org.springframework.http.HttpStatus;
  7. import org.springframework.http.server.reactive.ServerHttpRequest;
  8. import org.springframework.http.server.reactive.ServerHttpResponse;
  9. import org.springframework.security.access.AccessDeniedException;
  10. import org.springframework.security.web.server.authorization.ServerAccessDeniedHandler;
  11. import org.springframework.web.server.ServerWebExchange;
  12. import reactor.core.publisher.Mono;
  13. import java.nio.charset.StandardCharsets;
  14. /**
  15.  * 无权限访问异常
  16.  */
  17. @Slf4j
  18. public class CustomServerAccessDeniedHandler implements ServerAccessDeniedHandler {
  19.     @Override
  20.     public Mono<Void> handle(ServerWebExchange exchange, AccessDeniedException denied) {
  21.         ServerHttpRequest request = exchange.getRequest();
  22.         return exchange.getPrincipal()
  23.                 .doOnNext(principal -> log.info("用户:[{}]没有访问:[{}]的权限.", principal.getName(), request.getURI()))
  24.                 .flatMap(principal -> {
  25.                     ServerHttpResponse response = exchange.getResponse();
  26.                     response.setStatusCode(HttpStatus.FORBIDDEN);
  27.                     String body = "{\"code\":403,\"msg\":\"您无权限访问\"}";
  28.                     DataBuffer buffer = response.bufferFactory().wrap(body.getBytes(StandardCharsets.UTF_8));
  29.                     return response.writeWith(Mono.just(buffer))
  30.                             .doOnError(error -> DataBufferUtils.release(buffer));
  31.                 });
  32.     }
  33. }

5、将token信息传递到下游服务器中

  1. package com.huan.study.gateway.config;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
  5. import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
  6. import org.springframework.http.server.reactive.ServerHttpRequest;
  7. import org.springframework.security.core.context.ReactiveSecurityContextHolder;
  8. import org.springframework.security.core.context.SecurityContext;
  9. import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
  10. import org.springframework.web.server.ServerWebExchange;
  11. import org.springframework.web.server.WebFilter;
  12. import org.springframework.web.server.WebFilterChain;
  13. import reactor.core.publisher.Mono;
  14. /**
  15.  * 将token信息传递到下游服务中
  16.  *
  17.  * @author huan.fu 2021/8/25 - 下午2:49
  18.  */
  19. public class TokenTransferFilter implements WebFilter {
  20.     private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
  21.     static {
  22.         OBJECT_MAPPER.registerModule(new Jdk8Module());
  23.         OBJECT_MAPPER.registerModule(new JavaTimeModule());
  24.     }
  25.     @Override
  26.     public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
  27.         return ReactiveSecurityContextHolder.getContext()
  28.                 .map(SecurityContext::getAuthentication)
  29.                 .cast(JwtAuthenticationToken.class)
  30.                 .flatMap(authentication -> {
  31.                     ServerHttpRequest request = exchange.getRequest();
  32.                     request = request.mutate()
  33.                             .header("tokenInfo", toJson(authentication.getPrincipal()))
  34.                             .build();
  35.                     ServerWebExchange newExchange = exchange.mutate().request(request).build();
  36.                     return chain.filter(newExchange);
  37.                 });
  38.     }
  39.     public String toJson(Object obj) {
  40.         try {
  41.             return OBJECT_MAPPER.writeValueAsString(obj);
  42.         } catch (JsonProcessingException e) {
  43.             return null;
  44.         }
  45.     }
  46. }

6、网关层面的配置

  1. package com.huan.study.gateway.config;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.core.convert.converter.Converter;
  6. import org.springframework.core.io.FileSystemResource;
  7. import org.springframework.core.io.Resource;
  8. import org.springframework.security.authentication.AbstractAuthenticationToken;
  9. import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
  10. import org.springframework.security.config.web.server.SecurityWebFiltersOrder;
  11. import org.springframework.security.config.web.server.ServerHttpSecurity;
  12. import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
  13. import org.springframework.security.oauth2.jwt.Jwt;
  14. import org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder;
  15. import org.springframework.security.oauth2.jwt.ReactiveJwtDecoder;
  16. import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
  17. import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter;
  18. import org.springframework.security.oauth2.server.resource.authentication.ReactiveJwtAuthenticationConverterAdapter;
  19. import org.springframework.security.oauth2.server.resource.web.server.ServerBearerTokenAuthenticationConverter;
  20. import org.springframework.security.web.server.SecurityWebFilterChain;
  21. import reactor.core.publisher.Mono;
  22. import java.io.IOException;
  23. import java.nio.file.Files;
  24. import java.security.KeyFactory;
  25. import java.security.NoSuchAlgorithmException;
  26. import java.security.interfaces.RSAPublicKey;
  27. import java.security.spec.InvalidKeySpecException;
  28. import java.security.spec.X509EncodedKeySpec;
  29. import java.util.Base64;
  30. /**
  31.  * 资源服务器配置
  32.  */
  33. @Configuration
  34. @EnableWebFluxSecurity
  35. public class ResourceServerConfig {
  36.     @Autowired
  37.     private CustomReactiveAuthorizationManager customReactiveAuthorizationManager;
  38.     @Bean
  39.     public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
  40.         http.oauth2ResourceServer()
  41.                 .jwt()
  42.                     .jwtAuthenticationConverter(jwtAuthenticationConverter())
  43.                     .jwtDecoder(jwtDecoder())
  44.                     .and()
  45.                 // 认证成功后没有权限操作
  46.                 .accessDeniedHandler(new CustomServerAccessDeniedHandler())
  47.                 // 还没有认证时发生认证异常,比如token过期,token不合法
  48.                 .authenticationEntryPoint(new CustomServerAuthenticationEntryPoint())
  49.                 // 将一个字符串token转换成一个认证对象
  50.                 .bearerTokenConverter(new ServerBearerTokenAuthenticationConverter())
  51.                     .and()
  52.         .authorizeExchange()
  53.                 // 所有以 /auth/** 开头的请求全部放行
  54.                 .pathMatchers("/auth/**""/favicon.ico").permitAll()
  55.                 // 所有的请求都交由此处进行权限判断处理
  56.                 .anyExchange()
  57.                     .access(customReactiveAuthorizationManager)
  58.                     .and()
  59.                 .exceptionHandling()
  60.                     .accessDeniedHandler(new CustomServerAccessDeniedHandler())
  61.                     .authenticationEntryPoint(new CustomServerAuthenticationEntryPoint())
  62.                     .and()
  63.                 .csrf()
  64.                     .disable()
  65.         .addFilterAfter(new TokenTransferFilter(), SecurityWebFiltersOrder.AUTHENTICATION);
  66.         return http.build();
  67.     }
  68.     /**
  69.      * 从jwt令牌中获取认证对象
  70.      */
  71.     public Converter<Jwt, ? extends Mono<? extends AbstractAuthenticationToken>> jwtAuthenticationConverter() {
  72.         // 从jwt 中获取该令牌可以访问的权限
  73.         JwtGrantedAuthoritiesConverter authoritiesConverter = new JwtGrantedAuthoritiesConverter();
  74.         // 取消权限的前缀,默认会加上SCOPE_
  75.         authoritiesConverter.setAuthorityPrefix("");
  76.         // 从那个字段中获取权限
  77.         authoritiesConverter.setAuthoritiesClaimName("scope");
  78.         JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
  79.         // 获取 principal name
  80.         jwtAuthenticationConverter.setPrincipalClaimName("sub");
  81.         jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(authoritiesConverter);
  82.         return new ReactiveJwtAuthenticationConverterAdapter(jwtAuthenticationConverter);
  83.     }
  84.     /**
  85.      * 解码jwt
  86.      */
  87.     public ReactiveJwtDecoder jwtDecoder() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
  88.         Resource resource = new FileSystemResource("/Users/huan/code/study/idea/spring-cloud-alibaba-parent/gateway-oauth2/new-authoriza-server-public-key.pem");
  89.         String publicKeyStr = String.join("", Files.readAllLines(resource.getFile().toPath()));
  90.         byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyStr);
  91.         X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
  92.         KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  93.         RSAPublicKey rsaPublicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec);
  94.         return NimbusReactiveJwtDecoder.withPublicKey(rsaPublicKey)
  95.                 .signatureAlgorithm(SignatureAlgorithm.RS256)
  96.                 .build();
  97.     }
  98. }

7、网关yaml配置文件

  1. spring:
  2.   application:
  3.     name: gateway-auth
  4.   cloud:
  5.     nacos:
  6.       discovery:
  7.         server-addr: localhost:8847
  8.     gateway:
  9.       routes:
  10.         - id: user-provider
  11.           uri: lb://user-provider
  12.           predicates:
  13.             - Path=/user/**
  14.           filters:
  15.             - RewritePath=/user(?<segment>/?.*), $\{segment}
  16.     compatibility-verifier:
  17.       # 取消SpringCloud SpringCloudAlibaba SpringBoot 等的版本检查
  18.       enabled: false
  19. server:
  20.   port: 9203
  21. debug: true

六、演示

1、客户端 gateway 在认证服务器拥有的权限为 user.userInfo

85b4fe9e66feba38e92b2211f26a99d7.jpeg
客户端gateway拥有的权限

2、user-provider服务提供了一个api findAllUsers,它会返回 系统中存在的用户(假的数据) 和 解码后的token信息。

3、在网关层面,findAllUsers 需要的权限为 user.userInfo,正好 gateway这个客户端有这个权限,所以可以访问。

七、代码路径

https://gitee.com/huan1993/spring-cloud-alibaba-parent/tree/master/gateway-oauth2



欢迎加入我的知识星球,一起探讨架构,交流源码。加入方式,长按下方二维码噢

3f0dc8bcb62e856753f0780f0942ea89.png

已在知识星球更新源码解析如下:

25782807e64f8e52c816111d9cd05130.jpeg

8c780e3b41825fd6cf176fec975c332f.jpeg

d80309c4cc9b017aa9969413a1f33ee8.jpeg

be99154ff8c28b7cf15a6eb50153c433.jpeg

最近更新《芋道 SpringBoot 2.X 入门》系列,已经 101 余篇,覆盖了 MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。

提供近 3W 行代码的 SpringBoot 示例,以及超 4W 行代码的电商微服务项目。

获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。

  1. 文章有帮助的话,在看,转发吧。
  2. 谢谢支持哟 (*^__^*)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/68898
推荐阅读
相关标签
  

闽ICP备14008679号