5.管理员登录功能实现

飞一样的编程
飞一样的编程
擅长邻域:Java,MySQL,Linux,nginx,springboot,mongodb,微信小程序,vue

分类: springboot uni-app vue 专栏: 带小白springboot3+vue3+uniapp 标签: springboot3 vue3 uniapp

2026-01-17 15:56:14 97浏览

管理员登录功能实现

参考文章,这是我去年录制 springboot2 项目开发的时候写的笔记,现在录制 springboot3 的话,其实也适用。

https://jf3q.com/article/detail/10853

jwt 工具类

修改一下这个 jwt 工具类


public class JwtUtils {


    /**
     * 生成token字符串  xxx.yyy.zzzz
     * @param username
     * @return
     */
    public static  String getToken(String username,String role){

        return Jwts.builder().setHeaderParam("typ", "JWT")
                //过期时间  一个小时后此token就失效了
                .setExpiration(new Date(System.currentTimeMillis()+3600000))
                .setSubject(username)
                .claim("role",role)
                .signWith(SignatureAlgorithm.HS256, "jf3q-jwt").compact();
    }
    //从token中获取username
    public static String getUserName(String token){

        String username = null;
        try {
            username = Jwts.parser().setSigningKey("jf3q-jwt").parseClaimsJws(token).getBody().getSubject();
        } catch (ExpiredJwtException e) {
            throw new RuntimeException("token已经过期");
        } catch (UnsupportedJwtException e) {
            throw new RuntimeException("不支持的token");
        } catch (MalformedJwtException e) {
            throw new RuntimeException("token令牌格式不对");
        } catch (SignatureException e) {
            throw new RuntimeException("token签名问题");
        } catch (IllegalArgumentException e) {
            throw new RuntimeException("参数不合法-密钥不对");
        }


        return username;

    }


    //从token中获取role
    public static String getRole(String token){

        String role = null;
        try {
            role = Jwts.parser().setSigningKey("jf3q-jwt").parseClaimsJws(token).getBody().get("role").toString();
        } catch (ExpiredJwtException e) {
            throw new RuntimeException("token已经过期");
        } catch (UnsupportedJwtException e) {
            throw new RuntimeException("不支持的token");
        } catch (MalformedJwtException e) {
            throw new RuntimeException("token令牌格式不对");
        } catch (SignatureException e) {
            throw new RuntimeException("token签名问题");
        } catch (IllegalArgumentException e) {
            throw new RuntimeException("参数不合法-密钥不对");
        }


        return role;

    }
}

md5 工具类

/**
 * MD5工具类
 * 提供MD5加密相关功能
 */
public class Md5Utils {
        /**
         * MD5加密方法
         * @param str 要加密的字符串
         * @return 加密后的字符串
         */
        public static String encrypt(String str) {
            try {
                // 创建MessageDigest实例,指定使用MD5算法
                MessageDigest md = MessageDigest.getInstance("MD5");

                // 计算MD5哈希值
                byte[] digest = md.digest(str.getBytes());

                // 将字节数组转换为十六进制字符串
                StringBuilder sb = new StringBuilder();
                for (byte b : digest) {
                    sb.append(String.format("%02x", b));
                }

                return sb.toString();
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException("MD5加密算法不存在", e);
            }
        }

    public static void main(String[] args) {
        String password = "xx1234";
        String encryptedPassword = encrypt(password);
        System.out.println("原始密码: " + password);
        System.out.println("加密后的密码: " + encryptedPassword);
    }

}

注意 noclass 报错

jwt 生成得 token 返回前端的时候报错。我们是 json 格式的数据返回。

  <!-- 解决 Java 11+ 中缺少 JAXB API 的问题 -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.1</version>
        </dependency>

是因为缺少依赖

好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

您可能感兴趣的博客

客服QQ 1913284695