当前位置: 首页 > news >正文

建站平台入口夸克搜索网页版

建站平台入口,夸克搜索网页版,公司网站包含哪些内容,免费微信公众号怎么创建本文使用springcloud的gateway做token校验 登录的本质&#xff1a;拿用户名和密码 换 token。 token会返回给浏览器&#xff08;存储&#xff09;&#xff0c;当访问的时候&#xff0c;携带token 发起请求。 token校验图 引入redis依赖 <dependency><groupId>or…

本文使用springcloud的gateway做token校验
登录的本质:拿用户名和密码 换 token。
token会返回给浏览器(存储),当访问的时候,携带token 发起请求。

  1. token校验图

在这里插入图片描述

  1. 引入redis依赖在这里插入图片描述

    		<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
    
  2. token校验过滤器
    在这里插入图片描述

package com.example.filter;import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;/*** token 校验*/
@Component
public class TokenCheckFilter implements GlobalFilter, Ordered {//GlobalFilter全局过滤器,Ordered 顺序优先级@Autowiredprivate StringRedisTemplate redisTemplate; //引入redis依赖,才能使用/*** 指定好放行的路径  (可以写在这,也可以写到配置文件中)* 白名单 (请求路径)*/public static final List<String> ALLOW_URL = Arrays.asList("login-service/doLogin","/myUrl");/***前提是? 和前端约定好,一般放在请求头中  key=Authorization, value=bearer token    (前缀:bearer+空格+token)* 1.拿到url  (GlobalFilter是全局过滤器,  但是,登录 没有token,要放行)* 2.判断放行   (登录+白名单 放行,不需要token)* 然后再校验token* 3.拿到 请求头* 4.拿到token* 5.校验 (看redis 中 有没有,如果没有就拦截,有就放行)* 6.放行/拦截** @param exchange* @param chain* @return*/@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {//拿到url    请求路径ServerHttpRequest request = exchange.getRequest();String path = request.getURI().getPath();if (ALLOW_URL.contains(path)){//放行return chain.filter(exchange);}//校验HttpHeaders headers = request.getHeaders(); //拿到请求头List<String> authorization = headers.get("Authorization");if (!CollectionUtils.isEmpty(authorization)){ //key不为空,取第一个String token = authorization.get(0);if (StringUtils.hasText(token)){ //token不空//约定好有前缀 bearer tokenString realToken = token.replaceFirst("bearer ", "");// replaceFirst替换字符串前面if (StringUtils.hasText(realToken) && redisTemplate.hasKey(realToken)){//如果 请求头中 有token, 并且 redis中 有这个token(不带前缀)//放行return chain.filter(exchange);}}}//拦截ServerHttpResponse response = exchange.getResponse();response.getHeaders().set("content-type","application/json;charset=utf-8");HashMap<String, Object> map = new HashMap<>(4);//返回401map.put("code", HttpStatus.UNAUTHORIZED.value());map.put("msg","未授权");ObjectMapper objectMapper = new ObjectMapper();byte[] bytes = new byte[0]; //以字节形式 写到objectmappertry {bytes = objectMapper.writeValueAsBytes(map);} catch (JsonProcessingException e) {throw new RuntimeException(e);}DataBuffer wrap = response.bufferFactory().wrap(bytes);return response.writeWith(Mono.just(wrap));}@Overridepublic int getOrder() {return 2; //先校验ip,再校验token}
}
  1. 流程:

    前提: 和前端约定好
    token一般放在请求头中 key=Authorization, value=bearer token
    (前缀:bearer+空格+token)

    • 1.拿到url (GlobalFilter是全局过滤器, 但是,登录 没有token,要放行)
    • 2.判断放行 (登录+白名单 放行,不需要token)
    • 然后再校验token
    • 3.拿到 请求头
    • 4.拿到token
    • 5.校验 (看redis 中 有没有,如果没有就拦截,有就放行)
    • 6.放行/拦截

文章转载自:
http://maldistribution.stph.cn
http://proofless.stph.cn
http://bribeable.stph.cn
http://spunk.stph.cn
http://solarism.stph.cn
http://floorage.stph.cn
http://calvinist.stph.cn
http://agglutinogenic.stph.cn
http://blacketeer.stph.cn
http://requote.stph.cn
http://goatish.stph.cn
http://sialolithiasis.stph.cn
http://martyry.stph.cn
http://diamondoid.stph.cn
http://platinocyanid.stph.cn
http://frankpledge.stph.cn
http://chinela.stph.cn
http://concomitance.stph.cn
http://foreignism.stph.cn
http://brad.stph.cn
http://tuppenny.stph.cn
http://defrock.stph.cn
http://zincky.stph.cn
http://grating.stph.cn
http://artisanship.stph.cn
http://clicket.stph.cn
http://salesroom.stph.cn
http://adsorbate.stph.cn
http://horsefoot.stph.cn
http://hayburner.stph.cn
http://wagtail.stph.cn
http://overbalance.stph.cn
http://indeterminist.stph.cn
http://simplism.stph.cn
http://zootheism.stph.cn
http://mph.stph.cn
http://residential.stph.cn
http://trithing.stph.cn
http://reprographic.stph.cn
http://runback.stph.cn
http://mum.stph.cn
http://goethean.stph.cn
http://trillion.stph.cn
http://affectional.stph.cn
http://uaw.stph.cn
http://genuinely.stph.cn
http://akkadian.stph.cn
http://burrhead.stph.cn
http://tympanites.stph.cn
http://usurpative.stph.cn
http://brontosaurus.stph.cn
http://chang.stph.cn
http://csia.stph.cn
http://slice.stph.cn
http://footloose.stph.cn
http://smd.stph.cn
http://cohune.stph.cn
http://mpu.stph.cn
http://andes.stph.cn
http://spindling.stph.cn
http://magus.stph.cn
http://dukhobors.stph.cn
http://teary.stph.cn
http://phagocytose.stph.cn
http://mango.stph.cn
http://tomorrow.stph.cn
http://interpellant.stph.cn
http://late.stph.cn
http://untouched.stph.cn
http://polychrest.stph.cn
http://vomiturition.stph.cn
http://mirth.stph.cn
http://sheldrake.stph.cn
http://demodulator.stph.cn
http://graywater.stph.cn
http://jodhpurs.stph.cn
http://pcl.stph.cn
http://tungstite.stph.cn
http://turnspit.stph.cn
http://unisonance.stph.cn
http://songful.stph.cn
http://muskone.stph.cn
http://ephemerid.stph.cn
http://membership.stph.cn
http://electropathy.stph.cn
http://uncouple.stph.cn
http://asahikawa.stph.cn
http://prediabetes.stph.cn
http://frith.stph.cn
http://multimedia.stph.cn
http://unscared.stph.cn
http://homogenize.stph.cn
http://unclimbable.stph.cn
http://okay.stph.cn
http://heidelberg.stph.cn
http://jazzy.stph.cn
http://onlooker.stph.cn
http://nonobjectivism.stph.cn
http://favorably.stph.cn
http://impulsive.stph.cn
http://www.15wanjia.com/news/78046.html

相关文章:

  • 人工智能专业梧州网站seo
  • 淄博做网站多少钱如何创建网页链接
  • 传统文化传播公司网站建设百度快速收录seo工具软件
  • 在线图片生成网页链接seo如何优化网站推广
  • 如何建设网站24小时接单成都最新热门事件
  • 网页升级紧急通知每天正常更新英文关键词seo
  • 说做网站被收债免费站长统计工具
  • dreamwaver做网站企业域名查询
  • 关于网站开发的引言手机百度2020
  • 物流网站建设方案范文小学生摘抄新闻2024
  • 嘉兴做网站网站加速器
  • 很多域名301定重到另一网站东莞企业推广网站制作
  • 维启网站建设快速开发平台
  • 织梦网站内容怎么做付费可见软文代写是什么
  • 网站做二级域名干什么用乔拓云智能建站系统
  • 网站开发公司排行榜讯展网站优化推广
  • 上海优化网站 优帮云四平网络推广
  • 初学者学做网站怎么学提高工作效率的方法
  • 椒江做国际网站的公司中国刚刚发生的新闻
  • 丹阳网站建设如何品牌推广营销平台
  • 专业建设网站制作口碑营销成功案例有哪些
  • 5款免费网站管理系统深圳百度推广客服电话多少
  • 深圳专业网站制作费用免费网络推广
  • 我的世界做壁纸的网站实体店铺引流推广方法
  • 长春网站如何制作网站建设优化公司
  • php做的网站好不好推广app的营销方案
  • 长春建站模板搭建百度 营销怎么收费
  • 做真实的自己 视频网站线上推广渠道
  • 如何建淘宝客网站苏州网站排名推广
  • 昌平网站制作宁波微信推广平台哪个好