虚幻4 控制台_微服务网关6-网关鉴权 - 碧水云天4

一、网关鉴权

1、问题

当我们在未登录状态下点击“购买课程”按钮时,会显示“未知错误”,查看trade微服务控制台,发现控制台中报错,提示JWT为空,无法鉴权。

7447a7cd8196d32aad1d19e4f56ba6ed.png

2、解决方案

微服务网关中添加自定义全局过滤器,统一处理需要鉴权的服务

3、鉴权逻辑描述

  1. 当客户端第一次请求服务时,服务端对用户进行信息认证(登录)
  2. 认证通过,将用户信息进行加密形成token,返回给客户端
  3. 作为登录凭证以后每次请求,客户端都携带认证的token
  4. 服务端对token进行解密,判断是否有效

0064ff07fe145eeabcd3fd99de01cc3d.png

对于验证用户是否已经登录鉴权的过程可以在网关统一检验。检验的标准就是请求中是否携带token凭证以及token的正确性。

下面的我们自定义一个GlobalFilter,去校验所有的请求参数中是否包含“token”,如何不包含请求

参数“token”则不转发路由,否则执行正常的逻辑。

二、开发鉴权逻辑

1、网关中添加依赖

 <dependency>
     <groupId>com.atguigu</groupId>
     <artifactId>common_util</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <!--排除spring-boot-starter-web,否则和gateway中的webflux冲突-->
     <exclusions>
         <exclusion>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </exclusion>
     </exclusions>
</dependency>

<!--将随着spring-boot-starter-web排除的servlet-api添加回来 -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>

<!--gson-->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
</dependency>

2、排除数据源自动配置

@SpringBootApplication(exclude = )

3、创建过滤器

package com.atguigu.guli.infrastructure.apigateway.filter;

@Component
public class AuthGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = ();
        String path = ().getPath();
    
        //谷粒学院api接口,校验用户必须登录
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        if(("/api/**/auth/**", path)) {
            List<String> tokenList = ().get("token");
    
            //没有token
            if(null == tokenList) {
                ServerHttpResponse response = ();
                return out(response);
            }
    
            //token校验失败
            Boolean isCheck = ((0));
            if(!isCheck) {
                ServerHttpResponse response = ();
                return out(response);
            }
        }
    
        //放行
        return chain.filter(exchange);
    }
    
    //定义当前过滤器的优先级,值越小,优先级越高
    @Override
    public int getOrder() {
        return 0;
    }
    
    private Mono<Void> out(ServerHttpResponse response) {
    
        JsonObject message = new JsonObject();
        ("success", false);
        ("code", 28004);
        ("data", "");
        ("message", "鉴权失败");
        byte[] bytes = ().getBytes();
        DataBuffer buffer = ().wrap(bytes);
        //指定编码,否则在浏览器中会中文乱码
        ().add("Content-Type", "application/json;charset=UTF-8");
        //输出http响应
        return response.writeWith((buffer));
    }
}

测试:在未登录状态下点击立即购买显示“鉴权失败”。

fd5e557ad3f596a2b20639fbf53b4427.png

4、前端修改

guli-site的utils/中修改响应过滤器 ,添加分支:

else if (res.code === 28004) { // 鉴权失败
    window.location.href = '/login'
    return
} 

修改pages/的submitLogin方法:登录后回到原来的页面

// 跳转到首页
// window.location.href = '/'
if (('register') !== -1) {
    window.location.href = '/'
} else {
    (-1)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值