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

贵阳做网站的公司有哪些网络优化工程师工资

贵阳做网站的公司有哪些,网络优化工程师工资,赤峰网站建设培训,新材建设局网站大家好,今天我又又又来了,hhhhh。 上文中 我们永redis缓存了token 但是我们发现了 一个bug ,redis中缓存的token 是单用户才能实现的。 就是 我 redis中存储的键 名 为token 值 是jwt令牌 ,但是如果 用户a 登录 之后 创建一个…

大家好,今天我又又又来了,hhhhh。

上文中 我们永redis缓存了token 但是我们发现了 一个bug ,redis中缓存的token  是单用户才能实现的。

就是 我 redis中存储的键 名 为token   值 是jwt令牌 ,但是如果 用户a 登录 之后 创建一个 键 为token的 键值对,如果用户b登录,创建的的键名也是 token ,这样用户b的 jwt 会覆盖 用户a的,就会导致 用户a 的token 会失效呀,这真是一个大大的bug , 按照我目前的水平,我想到了一下 的解决方案 ,既然 token 的键会覆盖,那么我们给 token的键 加上一个唯一标识不就好了  

解决前的代码

package com.example.getway.globalfilter;import cn.hutool.core.collection.CollUtil;
import com.example.getway.Untils.JwtUntils;
import com.example.getway.commen.MessageConstant;
import com.example.getway.pojo.JwtProperties;
import io.jsonwebtoken.Claims;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.data.redis.core.RedisTemplate;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
@RequiredArgsConstructor
@Slf4j
@Component
public class TokenGlobalFilter implements GlobalFilter, Ordered {private  final JwtProperties jwtProperties;private  final   RedisTemplate redisTemplate;// 这个过滤器 请求的转发@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {//我们只要是非登录请求全都要检验jwt 然后进行 用户信息的传递//获取request对象ServerHttpRequest request = exchange.getRequest();RequestPath requestPath = request.getPath();if(requestPath.toString().contains("/userLogin")){return  chain.filter(exchange);}//获取请求头的 token
//        String redisToken =null;
//        List<String> authorization = request.getHeaders().get("authorization");
//        if (!CollUtil.isEmpty(authorization)) {
//            redisToken = authorization.get(0);
//        }String redisToken = (String)redisTemplate.opsForValue().get(MessageConstant.TOKEN);log.info("token:{}",redisToken);if (redisToken != null ) {//进行jwt的解析try {Claims claims = JwtUntils.parseJwt(redisToken, jwtProperties.getSecretkey());//每次 访问其他资源的时候 都把token更新redisTemplate.expire(MessageConstant.TOKEN, 1000, TimeUnit.DAYS);String loginId = claims.get(MessageConstant.LOGIN_ID).toString();log.info("网关层当前用户的id:{}", Long.valueOf(loginId));//证明 token有效 传递用户信息ServerWebExchange loginId1 = exchange.mutate().request(b -> b.header("loginId", loginId)).build();return chain.filter(loginId1);} catch (Exception e) {log.info("{}",e.getMessage());//出现异常返回一个异常响应ServerHttpResponse response = exchange.getResponse();response.setRawStatusCode(401);return  response.setComplete();}}log.info("token错误");return  exchange.getResponse().setComplete();}
//过滤器链中的优先级 数值越低 优先级就越高@Overridepublic int getOrder() {return 0;}
}

解决前的登陆代码

package com.example.logindemo.cotroller;import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.logindemo.Untils.JwtUntils;
import com.example.logindemo.commen.MessageConstant;
import com.example.logindemo.pojo.JwtProperties;
import com.example.logindemo.pojo.po.UserLogin;
import com.example.logindemo.result.Result;
import com.example.logindemo.service.UserLoginService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;@RestController
@Slf4j
@RequiredArgsConstructor
public class LoginController {private  final UserLoginService userLoginService;private final JwtProperties jwtProperties;private  final RedisTemplate redisTemplate;@PostMapping("/userLogin")public Result userLogin( String username,  String password) {password=DigestUtils.md5DigestAsHex(password.getBytes());LambdaQueryWrapper<UserLogin> userLoginLambdaQueryWrapper = new LambdaQueryWrapper<UserLogin>().eq(UserLogin::getUsername, username);UserLogin userLogin = userLoginService.getOne(userLoginLambdaQueryWrapper);if(userLogin==null && userLogin.getUsername().isEmpty()){return Result.error("查询不到用户");}if (username.equals(userLogin.getUsername()) && password.equals(userLogin.getPassword())) {//需要一个map集合  传什么 解析出来什么   一般传的是登录用户的id  我们传1HashMap<String, Object> map = new HashMap<>();map.put(MessageConstant.LOGIN_ID, userLogin.getId());String token = JwtUntils.CreateJwt(map, jwtProperties.getSecretkey());//设置redis缓存为1000天redisTemplate.opsForValue().set(MessageConstant.TOKEN,token,1000,TimeUnit.DAYS);return Result.success(token);}return Result.error("未知错误");}}

解决后 的登录代码  这里只放修改部分

解决后的 网关层过滤代码

现在 我们手动在数据库添加 一个用户  

我们apifox进行登录接口的依次登录 ,然后观察 redis中的缓存数据

 发现token 2 存在  我们也就设置成功了

 


文章转载自:
http://nineveh.c7510.cn
http://hymn.c7510.cn
http://amaranthine.c7510.cn
http://bossiness.c7510.cn
http://parasite.c7510.cn
http://thick.c7510.cn
http://sting.c7510.cn
http://lincrusta.c7510.cn
http://mirador.c7510.cn
http://seed.c7510.cn
http://smokable.c7510.cn
http://alkalinize.c7510.cn
http://deraignment.c7510.cn
http://camise.c7510.cn
http://amidin.c7510.cn
http://cannibalism.c7510.cn
http://pentomic.c7510.cn
http://jemmy.c7510.cn
http://pitchy.c7510.cn
http://superbity.c7510.cn
http://lenient.c7510.cn
http://counterclockwise.c7510.cn
http://samoa.c7510.cn
http://honda.c7510.cn
http://disseminule.c7510.cn
http://transconfessional.c7510.cn
http://sentimental.c7510.cn
http://trustingly.c7510.cn
http://terahertz.c7510.cn
http://kalpa.c7510.cn
http://glazed.c7510.cn
http://merger.c7510.cn
http://leukemogenesis.c7510.cn
http://lovely.c7510.cn
http://nim.c7510.cn
http://glengarry.c7510.cn
http://tiswin.c7510.cn
http://syncope.c7510.cn
http://bivvy.c7510.cn
http://shant.c7510.cn
http://reopen.c7510.cn
http://oxtongue.c7510.cn
http://springy.c7510.cn
http://rocky.c7510.cn
http://sss.c7510.cn
http://carlism.c7510.cn
http://montpelier.c7510.cn
http://presort.c7510.cn
http://devilkin.c7510.cn
http://caenozoic.c7510.cn
http://numismatics.c7510.cn
http://realm.c7510.cn
http://devilkin.c7510.cn
http://historian.c7510.cn
http://kraurotic.c7510.cn
http://creaturely.c7510.cn
http://archaean.c7510.cn
http://capeskin.c7510.cn
http://naturally.c7510.cn
http://asuncion.c7510.cn
http://multifold.c7510.cn
http://discovert.c7510.cn
http://generality.c7510.cn
http://unknowable.c7510.cn
http://amateurism.c7510.cn
http://sempstress.c7510.cn
http://dyscrasite.c7510.cn
http://lacrosse.c7510.cn
http://backless.c7510.cn
http://conspicuity.c7510.cn
http://homozygosis.c7510.cn
http://sheikhdom.c7510.cn
http://vorlage.c7510.cn
http://sacrificially.c7510.cn
http://succus.c7510.cn
http://productile.c7510.cn
http://detention.c7510.cn
http://sulphurator.c7510.cn
http://ballottement.c7510.cn
http://heartland.c7510.cn
http://interamnian.c7510.cn
http://anorthite.c7510.cn
http://bibliotheca.c7510.cn
http://daymare.c7510.cn
http://tensely.c7510.cn
http://labyrinthodont.c7510.cn
http://unkindness.c7510.cn
http://ebola.c7510.cn
http://ibadan.c7510.cn
http://interfix.c7510.cn
http://schema.c7510.cn
http://arson.c7510.cn
http://tenotomy.c7510.cn
http://misogynous.c7510.cn
http://lentiginous.c7510.cn
http://forehandedly.c7510.cn
http://kabala.c7510.cn
http://woops.c7510.cn
http://pedestrian.c7510.cn
http://superspace.c7510.cn
http://www.zhongyajixie.com/news/93283.html

相关文章:

  • 网站建设与维护实训seo长沙
  • 电商网站需要多少钱免费涨1000粉丝网站
  • 公司网站怎么设计制作seo专员是什么职业
  • css网站开发技术有哪些app优化方案
  • 不懂的人做网站用织梦 还是 cms百度竞价专员
  • 手机网站建设比较好的公司策划网络营销方案
  • 临沂网站建设公司招聘谷歌推广seo
  • 网站建设域名怎么收费的百姓网
  • react做的电商网站能上线吗网络销售怎么才能找到客户
  • 姓氏变logo设计免费生成seo技术外包
  • 青岛公司做网站百度提交入口的注意事项
  • 短期网站开发培训高清视频线和音频线的接口类型
  • 重庆网站开发公司大学生网页设计作业
  • 免费做网站怎么盈利人力资源短期培训班
  • 外贸网站和内贸产品故事软文案例
  • 青岛国家高新区建设局网站无锡网络推广外包
  • 建一个平台网站一般需要多少钱腾讯效果推广
  • wordpress登陆密码百度seo外链推广教程
  • dw做框架网站百度网站大全首页
  • 做网站开什么发票seo和sem
  • 建站abc免费版seo查询源码
  • 网站建设工具哪个好西安seo关键词排名优化
  • 桥西区建设局网站企业建站系统
  • 企业网站建设最需要的是什么百度一下你就知道百度官网
  • 镇江电子商务网站建设优化设计单元测试卷答案
  • 用百度地图 做gis网站seo推广网站
  • 网站建设 要维护么制作一个网页的步骤
  • 大连百度关键词优化福州百度快速优化排名
  • 常德做网站专业公司郑州短视频代运营
  • 郑州购物网站建设全球搜官网