关于gateway与oauth2的兼容问题处理

文章目录

前言

令牌鉴权方案的选择上,比较流行的有spring security + 自定义的令牌(比如JWT令牌)、spring security+oauth2,假如使用了spring security + oauth2的方案,在使用上oauth2提供有ResourceServerTokenServices用于解决资源服务对令牌的权限的校验,当访问资源服务的时候,token令牌经过ResourceServerTokenServices的loadAuthentication方法完成验证。

但是当服务无需做非常细致的服务划分的时候,也就是将一个项目当做一个整体资源,这个时候就涉及到在网关校验即可,其他微服务无需二次校验。

问题

目前流行的网关方案是spring gateway(spring框架),但是gateway为了实现高效,底层使用Spring WebFlux,Spring WebFlux是Spring 5.0引入的新的响应式框架,区别于SpringMVC,它不需要依赖Servlet API,它是完全异步非阻塞的,所以引入oauth2-resource的时候会发现启动报错,配置需要基于servlet。

解决方案

手写RPC

手写http访问oauth2的check_token接口实现对token令牌的校验,实现起来比较简单,可以用在网关的过滤器上。下面举个例子

复制代码
  WebClient webClient = WebClient.create("http://localhost:9900");
        webClient.post()
                .uri("/oauth/check_token?token=" + token.substring(7))
                .header("Authorization", basic)
                .exchange()
                .flatMap(response -> {
                    HttpStatus status = response.statusCode();
                    if (status.is2xxSuccessful()) {
                        return response.bodyToMono(Map.class);
                    } else {
                        return response.createException().flatMap(Mono::error);
                    }
                }).subscribe(responseBody -> {
            log.info("post请求响应数据:{}", responseBody);
            if (Objects.isNull(responseBody) || responseBody.isEmpty()) {
                return;
            }
        }, error -> {
            log.error("post请求发生异常:", error);
        });

采用spring feign调用

据说在高版本需要对feign调用进行改造完成响应式的feign,因为spring自身并没有提供解决方案,暂时不在考虑范围。目前基于jdk8的最高版本也就是2.7.18,并没有这困扰。

下面Mono是用于测试响应式的feign调用

复制代码
@FeignClient(name = ServerNameConstant.AUTHORITY_CENTER, path = "/authorityCenter/auth", fallback = AuthRemoteFallback.class)
public interface AuthRemote {

    /**
     * 获取用户信息和权限
     *
     * @param in token
     * @return 用户信息
     * @author zfj
     * @date 2024/1/19
     */
    @PostMapping("/getUser")
    ResponseVO<UserResDTO> getUser(@RequestBody GetUserReqDTO in);

    /**
     * 获取用户信息和权限
     *
     * @param in token
     * @return 用户信息
     * @author zfj
     * @date 2024/1/19
     */
    @PostMapping("/getUserMono")
    Mono<ResponseVO<UserResDTO>> getUserMono(@RequestBody GetUserReqDTO in);

}

网关过滤器中调用

复制代码
ResponseVO<UserResDTO> res = oauthRemote.getUser(new GetUserReqDTO(token));

采用JWT解析

JWT具有去中心化的特点,也就能实现无需访问oauth2的鉴权服务就能自行解析令牌,但是也会带来风险,就是秘钥泄露,所以这种方案不能用于安全性要求高的服务。

oauth2也针对JWT提供一个方案(假设不是用resource依赖包,等你升级到spring 3版本以后就能知道了,低版本还可以用spring cloud oauth2,高版本就必须用resource),这个方案考虑到秘钥安全,采用数字自签证书实现公钥的分发,就能很好的避免伪造令牌问题(除非私钥也泄密,那就另外采取一套措施保护私钥)。

相关推荐
摘星编程2 天前
Nginx 502 Bad Gateway:从 upstream 日志到 FastCGI 超时复盘
网络·nginx·gateway·php-fpm·fastcgi
网硕互联的小客服2 天前
504 Gateway Timeout:服务器作为网关或代理时未能及时获得响应如何处理?
运维·服务器·gateway
Pierre_3 天前
通过SpringCloud Gateway实现API接口镜像请求(陪跑)网关功能
spring·spring cloud·gateway
kk在加油4 天前
智能门卫:Gateway
gateway
小安同学iter5 天前
Spring Cloud Gateway 网关(五)
java·开发语言·spring cloud·微服务·gateway
JAVA学习通5 天前
Spring Cloud ------ Gateway
java·spring cloud·gateway
weixin_449568707 天前
访问Nginx 前端页面,接口报502 Bad Gateway
前端·nginx·gateway
yangmf204011 天前
LDAP 认证系列(四):Gateway LDAP 认证
大数据·elasticsearch·搜索引擎·gateway·ldap
银迢迢12 天前
SpringCloud微服务技术自用笔记
java·spring cloud·微服务·gateway·sentinel
孤狼程序员16 天前
【Spring Cloud 微服务】2.守护神网关Gateway
spring cloud·微服务·gateway