• 八、创建JWT工具类


         JWTToken 要经过加密才能返回给客户端,包括客户端上传的 Token ,后端项目需要验证核

    实。于是我们需要一个 JWT 工具类,用来 加密 Token 验证 Token 的有效性.
    1、导入依赖项
    1. 1. <dependency>
    2. 2. <groupId>org.apache.shiro</groupId>
    3. 3. <artifactId>shiro-web</artifactId>
    4. 4. <version>1.5.3</version>
    5. 5. </dependency>
    6. 6. <dependency>
    7. 7. <groupId>org.apache.shiro</groupId>
    8. 8. <artifactId>shiro-spring</artifactId>
    9. 9. <version>1.5.3</version>
    10. 10. </dependency>
    11. 11. <dependency>
    12. 12. <groupId>com.auth0</groupId>
    13. 13. <artifactId>java-jwt</artifactId>
    14. 14. <version>3.10.3</version>
    15. 15. </dependency>
    16. 16. <dependency>
    17. 17. <groupId>org.springframework.boot</groupId>
    18. 18. <artifactId>spring-boot-configuration-processor</artifactId>
    19. 19. <optional>true</optional>
    20. 20. </dependency>
    21. 21. <dependency>
    22. 22. <groupId>org.apache.commons</groupId>
    23. 23. <artifactId>commons-lang3</artifactId>
    24. 24. <version>3.11</version>
    25. 25. </dependency>
    26. 26. <dependency>
    27. 27. <groupId>org.apache.httpcomponents</groupId>
    28. 28. <artifactId>httpcore</artifactId>
    29. 29. <version>4.4.13</version>
    30. 30. </dependency>
    31. 31. <dependency>
    32. 32. <groupId>org.springframework.boot</groupId>
    33. 33. <artifactId>spring-boot-starter-aop</artifactId>
    34. 34. </dependency>

    二、定义密钥和过期时间

         我建议大家把密钥和过期时间定义到SpringBoot配置文件中,然后再值注入到JavaBean中,这样维护起来比较方便。

     

    1. emos: 2. jwt: 3. #密钥
    2. 4. secret: abc123456
    3. 5. #令牌过期时间(天)
    4. 6. expire: 5 7. #令牌缓存时间(天数)
    5. 8. cache-expire: 10

    三、创建JWT工具类

     

    1. package com.example.emos.wx.config.shiro;
    2. import cn.hutool.core.date.DateField;
    3. import cn.hutool.core.date.DateUtil;
    4. import com.auth0.jwt.JWT;
    5. import com.auth0.jwt.JWTCreator;
    6. import com.auth0.jwt.JWTVerifier;
    7. import com.auth0.jwt.algorithms.Algorithm;
    8. import com.auth0.jwt.interfaces.DecodedJWT;
    9. import lombok.extern.slf4j.Slf4j;
    10. import org.springframework.beans.factory.annotation.Value;
    11. import org.springframework.boot.autoconfigure.SpringBootApplication;
    12. import org.springframework.stereotype.Component;
    13. import java.util.Date;
    14. @Component
    15. @SpringBootApplication
    16. @Slf4j
    17. public class JwtUtil {
    18. @Value("${emos.jwt.secret}")
    19. private String secret;
    20. @Value("${emos.jwt.expire}")
    21. private int expire;
    22. public String createToken(int userId){
    23. Date date=DateUtil.offset(new Date(), DateField.DAY_OF_YEAR,5);
    24. Algorithm algorithm=Algorithm.HMAC256(secret);
    25. JWTCreator.Builder builder= JWT.create();
    26. String token=builder.withClaim("userId",userId).withExpiresAt(date).sign(algorithm);
    27. return token;
    28. }
    29. public int getUserId(String token){
    30. DecodedJWT jwt=JWT.decode(token);
    31. int userId=jwt.getClaim("userId").asInt();
    32. return userId;
    33. }
    34. public void verifierToken(String token){
    35. Algorithm algorithm=Algorithm.HMAC256(secret);
    36. JWTVerifier verifier=JWT.require(algorithm).build();
    37. verifier.verify(token);
    38. }
    39. }

    四、把令牌封装成认证对象

    我们通过JwtUtil类可以生成 Token ,这个 Token 我们是要返回给客户端的。接下来 我们要把 JWT Shiro框架 对接起来,这样 Shiro框架 就会拦截所有的Http请求,然后验证请求 提交的 Token 是否有效。

      客户端提交的Token不能直接交给Shiro框架,需要先封装成 AuthenticationToken 类型的对象,

    所以我们我们需要先创建 AuthenticationToken 的实现类。

     

    1. package com.example.emos.wx.config.shiro;
    2. import org.apache.shiro.authc.AuthenticationToken;
    3. public class OAuth2Token implements AuthenticationToken {
    4. private String token;
    5. public OAuth2Token(String token) {
    6. this.token = token;
    7. }
    8. @Override
    9. public Object getPrincipal() {
    10. return token;
    11. }
    12. @Override
    13. public Object getCredentials() {
    14. return token;
    15. }
    16. }

  • 相关阅读:
    【重温C++ Primer】第一章、初识C++
    MySQL高可用集群解决方案之:lvs+keepalived+mysql cluster实现负载均衡
    Java类和对象(二)
    Orchestrator 对 MGR MySQL Group Replication的支持
    【软件测试】8年资深测试说出来我们的心声......
    [Maven] maven插件系列之maven-shade-plugin
    Eclipse iceoryx™ - 真正的零拷贝进程间通信
    Git报错解决
    用于时间触发的嵌入式软件的IDE
    改变自己 只需要两年
  • 原文地址:https://blog.csdn.net/weixin_39309918/article/details/127452727