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

网站做宣传域名什么好免费seo优化工具

网站做宣传域名什么好,免费seo优化工具,电子商务网站建设参考文献书籍,怎么查网站备案信息为实现Jwt简单的权限管理,我们需要用Jwt工具来生成token,也需要用Jwt来解码token,同时需要添加Jwt拦截器来决定放行还是拦截。下面来实现: 1、gradle引入Jwt、hutool插件 implementation com.auth0:java-jwt:3.10.3implementatio…

为实现Jwt简单的权限管理,我们需要用Jwt工具来生成token,也需要用Jwt来解码token,同时需要添加Jwt拦截器来决定放行还是拦截。下面来实现:

1、gradle引入Jwt、hutool插件

    implementation 'com.auth0:java-jwt:3.10.3'implementation 'cn.hutool:hutool-all:5.3.7'

2、Jwt工具类,提供静态方法生成token,和根据请求携带的token查找user信息

package com.zzz.simple_blog_backend.utils;import ......@Component
public class JwtTokenUtils {@Autowiredprivate UserService userService;private static UserService userServiceStatic;@PostConstruct//在spring容器初始化后执行该方法public void setUserService() {userServiceStatic = userService;}//生成Tokenpublic static String genToken(String userId,String passwordSign) {return JWT.create().withAudience(userId)//放入载荷.withExpiresAt(DateUtil.offsetHour(new Date(), 2))//2小时后过期.sign(Algorithm.HMAC256(passwordSign));//密码签名作为密钥}//通过token获取当前登录用户信息public static User getCurrentUser() {String token = null;HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();//1、获取tokentoken = request.getHeader("token");if (StrUtil.isBlank(token)) {token = request.getParameter("token");}if (StrUtil.isBlank(token)) {throw new RuntimeException("没有token,请重新登录");}String userId;User user;try {userId = JWT.decode(token).getAudience().get(0);} catch (Exception e) {throw new RuntimeException("token验证失败,请重新登录!!!");}user = userServiceStatic.findById(Integer.parseInt(userId));if(user==null) {throw new RuntimeException("用户id不存在,请重新登录!!!");}//3、用密码签名,解码判断tokentry {JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(user.getPassword())).build();jwtVerifier.verify(token);} catch (JWTVerificationException e) {throw new CustomException(001, "token验证失败,请重新登录!!!");}return user;}
}

3、Jwt拦截器

SpringBoot添加拦截器,excludePathPatterns可以指定不拦截的页面,RestController指定了请求前缀,控制器类要用@RestController代替@ControlleraddInterceptor添加了jwtInterceptor拦截器。

package com.zzz.simple_blog_backend.config;import ...@Configuration
public class WebConfig implements WebMvcConfigurer{@Autowiredprivate JwtInterceptor jwtInterceptor;@Overridepublic void configurePathMatch(PathMatchConfigurer configurer) {//指定restcontroller统一接口前缀configurer.addPathPrefix("/api", clazz -> clazz.isAnnotationPresent(RestController.class));}@Overridepublic void addInterceptors(InterceptorRegistry registry) {// 加自定义拦截器  给特定请求放行registry.addInterceptor(jwtInterceptor).addPathPatterns("/api/**").excludePathPatterns("/api/user/login","/api/user/register");}
}

仔细的童靴会发现JwtTokenUtils.getCurrentUser()方法和JwtInterceptor的拦截方法很像,主要区别就在if(user.getRole()!=0)的判断。所以JwtInterceptor只会给管理员放行,如果需要给普通用户放行而未登录用户不放行,那请求路径先添加到excludePathPatterns,并且控制类对应的响应方法第一句就先调用JwtTokenUtils.getCurrentUser()判断是否用户已登录。

package com.zzz.simple_blog_backend.config;import ......@Component
public class JwtInterceptor implements HandlerInterceptor{@Autowiredprivate UserService userService;@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {//1、获取tokenString token = request.getHeader("token");if (StrUtil.isBlank(token)) {token = request.getParameter("token");}if (StrUtil.isBlank(token)) {throw new RuntimeException("没有token,请重新登录");}//2、开始认证    解码token,获得userIdString userId;User user;try {userId = JWT.decode(token).getAudience().get(0);} catch (Exception e) {throw new RuntimeException("token验证失败,请重新登录!!!");}user = userService.findById(Integer.parseInt(userId));if(user==null) {throw new RuntimeException("用户id不存在,请重新登录!!!");}if(user.getRole()!=0) {throw new RuntimeException("非管理员账号,无权访问!!!");}//3、用密码签名,解码判断tokentry {JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(user.getPassword())).build();jwtVerifier.verify(token);} catch (JWTVerificationException e) {throw new RuntimeException("token验证失败,请重新登录!!!");}//token验证成功,放行return true;
//        return HandlerInterceptor.super.preHandle(request, response, handler);}
}

4、前后端登录操作

登录后 后端返回带token不带密码的user数据

    @PostMapping("user/login")@ResponseBodypublic CommonResult<Object> login(@RequestBody User user){user = userService.findByUsernameAndPassword(user);if (user == null) {return CommonResult.failed(001, Message.createMessage("用户名或密码错误!!!"));} else {//生成用户对应的TokenString token = JwtTokenUtils.genToken(user.getId().toString(), user.getPassword());user.setToken(token);//不传输密码user.setPassword("");return CommonResult.success(user);}}

前端保存带token的user

//axios的post请求成功后操作localStorage.setItem("user",JSON.stringify(response.data.data));//保存用户信息

5、前端每次请求都携带token信息

假设已安装axios,Axios.interceptors.request拦截用户所有请求,添加token信息后再发送请求,这样后端就可以判断了。

import Axios from 'axios'Vue.prototype.$http = Axios;
//添加向后端发起请求的服务器地址前缀
Axios.defaults.baseURL=AIOS_BASE_URL;   // "http://127.0.0.1/api"
//设置请求超时时间
Axios.defaults.timeout=5000;//axios拦截器
//对接口request拦截
Axios.interceptors.request.use(function(config){//发起增删改查请求时,带上token认证var user = localStorage.getItem("user");if(user){config.headers["token"] = JSON.parse(user).token;}return config;
})
//携带证书 session id 跨域请求的话需要
Axios.defaults.withCredentials = true

总结

Jwt实现权限管理的原理是登录成功后 后端端生成token密钥,随着用户信息发送给客户端,客户端接受并保存信息到本地localStorage。以后每次需要权限验证时,根据客户端返回的携带token信息,后端进行拦截并解码校验,通过则放行,否则抛异常。如果要抛异常时,返回错误信息给前端,请看链接。

http://www.dinnco.com/news/36883.html

相关文章:

  • 在线diy网站西安核心关键词排名
  • 李青青做网站 公司主要做应用领域搜索引擎优化到底是优化什么
  • 三网一体网站建设品牌推广策划方案怎么写
  • 网站开发补充协议 违约宁德市人社局
  • 没营业执照怎么做网站想要推广网页
  • 文山州住房和城乡建设网站2023年免费b站推广大全
  • 哈尔滨信息网招聘温州网站优化推广方案
  • 贵州微信网站建设百度快照投诉中心人工电话
  • seo培训公司南京网络优化培训
  • 麦田建设工程网站seo系统教程
  • dedecms 招聘网站百度竞价排名收费标准
  • 电脑公司网站管理系统cba最新消息
  • 一个网站的构建百度学术官网
  • 网站首页index.html南宁百度快速优化
  • 重庆长寿网站设计公司哪家好河南网站顾问
  • 怎么知道网站的ftp广州百度推广优化排名
  • 网站系统测试计划即刻搜索
  • 站长要维护网站百度百科创建
  • 做汽车网站一句话让客户主动找你
  • 网站后台shopadmin输在哪里免费的网站推广
  • 广告公司怎么设置网站关键字南京市网站
  • 孝感注册公司肇庆seo外包公司
  • 网站建设如何记账南京百度
  • 企业邮箱地址怎么填seo比较好的公司
  • 随州做网站的公司学生网页设计模板
  • wordpress 网站首页可以打开_其他页面打不开网络营销公司是做什么的
  • 包年seo和整站优化google关键词查询工具
  • 定制网站开发公司seo高手培训
  • lamp网站开发黄金组...我想学做互联网怎么入手
  • 连云港网站建设手机百度云网页版登录