美文网首页
JwtUtils 的集成

JwtUtils 的集成

作者: yuaixing003 | 来源:发表于2023-11-12 14:40 被阅读0次

倒包:

<!-- JWT依赖--><dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.9.1</version></dependency>

<!-- no more than 2.3.3--><dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.3.3</version></dependency>

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId></dependency>

工具类:

public class JwtUtils {

private static String signKey ="yixiatech";

private static Long expire =1626662400000L;

/**

    * 生成JWT令牌

    * @param claims JWT第二部分负载 payload 中存储的内容

    * @return

*/

    public static String generateJwt(Map claims){

String jwt =Jwts.builder()

.addClaims(claims)

.signWith(SignatureAlgorithm.HS256,signKey)

.setExpiration(new Date(System.currentTimeMillis() +expire))

.compact();

return jwt;

}

/**

    * 解析JWT令牌

    * @param jwt JWT令牌

    * @return JWT第二部分负载 payload 中存储的内容

    */

    public static Claims parseJWT(String jwt){

Claims claims =Jwts.parser()

.setSigningKey(signKey)

.parseClaimsJws(jwt)

.getBody();

return claims;

}

}

控制器代码:

@Slf4j

@RestController

public class LoginController {

@Autowired

    private EmpService empService;

@PostMapping("/login")

public Result login(@RequestBody Emp emp){

Emp e =empService.login(emp);

log.info("员工登录: {}",e);

//登录成功,生成令牌,下发令牌

        if (e !=null){

Mapclaims =new HashMap<>();

claims.put("id",e.getEmpId());

claims.put("name",e.getName());

claims.put("username",e.getUsername());

String jwt =JwtUtils.generateJwt(claims);//jwt包含了当前登录的员工信息

            return Result.success(jwt);

}

//登录失败, 返回错误信息

        return Result.error("用户名或密码错误");

}

}

当我们在Filter类上面加了@WebFilter注解之后,接下来我们还需要在启动类上面加上一个注解@ServletComponentScan,通过这个@ServletComponentScan注解来开启SpringBoot项目对于Servlet组件的支持。

具体操作的类

import javax.servlet.*;

import javax.servlet.annotation.WebFilter;

import java.io.IOException;

@WebFilter(urlPatterns ="/*")

public class EmpFilter implements Filter {

@Override //初始化方法, 只调用一次

    public void init(FilterConfig filterConfig)throws ServletException {

System.out.println("init 初始化方法执行了");

}

@Override //拦截到请求之后调用, 调用多次

    public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException {

System.out.println("Demo 拦截到了请求...放行前逻辑");

chain.doFilter(request,response);

System.out.println("Demo 拦截到了请求...放行后逻辑");

}

@Override //销毁方法, 只调用一次

    public void destroy() {

System.out.println("destroy 销毁方法执行了");

}

}

对象转json

依赖

<!--fastJSON--><dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.76</version></dependency>

代码

Result error = Result.error("NOT_LOGIN");

//手动转换 对象--json --------> 阿里巴巴fastJSON

String notLogin = JSONObject.toJSONString(error);

相关文章

  • Java的JwtUtils

    用到了springsecurity, jwt 参考大佬的视频手敲的代码

  • java中使用jwt

    官网导包: 新建类JwtUtils 使用 到此完成。

  • 【3】企业集成EAI概要

    企业应用集成(EAI)可以包括表示集成、数据集成、控制集成和业务流程集成等多个层次和方面。 1.表示集成表示集成也...

  • iOS开发 - SMSSDK(短信验证)

    Mob 的SMS短信验证集成和使用 1.集成##### 集成可以用cocoapods来集成,也可以手动集成,这里就...

  • 集成Facebook广告、分享集成笔记

    集成Facebook广告、分享集成笔记 1、集成Facebook广告2、集成Facebook分享 Facebook...

  • iOS原生集成H5+详细流程

    iOS原生集成H5+ 集成方式 独立应用方式集成 Widget方式集成 WebView方式集成 可以打开官方链接:...

  • SpringBoot 集成

    SpringBoot 集成 redis SpringBoot集成mongodb SpringBoot集成Beetl...

  • 竹木纤维集成墙面好不好

    竹木纤维集成墙面优点 集成墙板十大品牌排名竹木纤维集成墙面用户集成墙面价格表 竹木纤维集成墙面 新型集成墙面装饰板...

  • springboot集成webflux

    说明: springboot集成webflux可以自己手动集成,也可以用官方的快速集成工具 1.官方快速集成 官方...

  • iOS 友盟分享

    前言:学习笔记大致流程: (只记录 pod集成)1.集成SDK 1.1 自动集成 (Pod) [集成链接](...

网友评论

      本文标题:JwtUtils 的集成

      本文链接:https://www.haomeiwen.com/subject/ukxrwdtx.html