后台网关鉴权

  1. jwtUtil
public class JwtUtil {

    //有效期为
    public static final Long JWT_TTL = 3600000L;// 60 * 60 *1000  一个小时
    //设置秘钥盐
    public static final String JWT_KEY = "xk";

    /**
     * 创建token
     * @param id
     * @param subject
     * @param ttlMillis
     * @return
     */
    public static String createJWT(String id, String subject, Long ttlMillis) {

        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);
        if(ttlMillis==null){
            ttlMillis=JwtUtil.JWT_TTL;
        }
        long expMillis = nowMillis + ttlMillis;
        Date expDate = new Date(expMillis);
        SecretKey secretKey = generalKey();

        JwtBuilder builder = Jwts.builder()
                .setId(id)              //唯一的ID
                .setSubject(subject)   // 主题  可以是JSON数据
                .setIssuer("admin")     // 签发者
                .setIssuedAt(now)      // 签发时间
                .signWith(signatureAlgorithm, secretKey) //使用HS256对称加密算法签名, 第二个参数为秘钥
                .setExpiration(expDate);// 设置过期时间
        return builder.compact();
    }
    /**
     * 解析
     *
     * @param jwt
     * @return
     * @throws Exception
     */
    public static Claims parseJWT(String jwt) throws Exception {
        SecretKey secretKey = generalKey();
        return Jwts.parser()
                .setSigningKey(secretKey)
                .parseClaimsJws(jwt)
                .getBody();
    }
}
  1. 系统服务登录代码
public Result login(@RequestBody Admin admin) {
    boolean flag = adminService.login(admin);
      if(flag){
          Map<String, String> info = new HashMap<>();
          info.put("username", admin.getLoginName());
          String jwt = JwtUtil.createJWT(UUID.randomUUID().toString(), admin.getLoginName(), null);
          info.put("token", jwt);
          return new Result(true, StatusCode.OK, "登录成功", info);
      }
      return new Result(false, StatusCode.ERROR, "登录失败");
}
  1. 网关服务解析jwt
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
	   ServerHttpRequest request = exchange.getRequest();
	   ServerHttpResponse response = exchange.getResponse();
	   //判断是否是登录请求
	   if (request.getURI().getPath().contains("/admin/login")) {
	       return chain.filter(exchange);
	   }
	   // 获取token信息
	   HttpHeaders headers = request.getHeaders();
	   String jwtToken = headers.getFirst("token");
	   // 判断令牌是否存在
	   if (StringUtils.isEmpty(jwtToken)) {
	       // 返回错误信息
	       response.setStatusCode(HttpStatus.UNAUTHORIZED);
	       return response.setComplete();
	   }
	   // 解析jwt令牌
	   try {
	       JwtUtil.parseJWT(jwtToken);
	   } catch (Exception e) {
	       e.printStackTrace();
	       // 返回错误信息
	       response.setStatusCode(HttpStatus.UNAUTHORIZED);
	       return response.setComplete();
	   }
	   // 放行
	   return chain.filter(exchange);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值