Initial commit
This commit is contained in:
		@@ -13,7 +13,7 @@
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>org.springframework.boot</groupId>
 | 
			
		||||
            <artifactId>spring-boot-starter-web</artifactId>
 | 
			
		||||
            <scope>provided </scope>
 | 
			
		||||
<!--            <scope>provided</scope>-->
 | 
			
		||||
        </dependency>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>io.jsonwebtoken</groupId>
 | 
			
		||||
@@ -27,5 +27,11 @@
 | 
			
		||||
            <groupId>com.alibaba</groupId>
 | 
			
		||||
            <artifactId>fastjson</artifactId>
 | 
			
		||||
        </dependency>
 | 
			
		||||
        <!--JWT-->
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>io.jsonwebtoken</groupId>
 | 
			
		||||
            <artifactId>jjwt</artifactId>
 | 
			
		||||
        </dependency>
 | 
			
		||||
 | 
			
		||||
    </dependencies>
 | 
			
		||||
</project>
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,102 @@
 | 
			
		||||
package com.atguigu.common.jwt;
 | 
			
		||||
 | 
			
		||||
import io.jsonwebtoken.*;
 | 
			
		||||
import org.springframework.util.StringUtils;
 | 
			
		||||
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * ClassName: JitHelper
 | 
			
		||||
 * Package: com.atguigu.common.jwt
 | 
			
		||||
 * JWT工具类
 | 
			
		||||
 *
 | 
			
		||||
 * @author yovinchen
 | 
			
		||||
 * @Create 2023/6/10 16:12
 | 
			
		||||
 */
 | 
			
		||||
public class JwtHelper {
 | 
			
		||||
 | 
			
		||||
    private static final long tokenExpiration = 365L * 24 * 60 * 60 * 1000;
 | 
			
		||||
    private static final String tokenSignKey = "123456";
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 根据用户 id 和用户名称, 生成token的字符串
 | 
			
		||||
     *
 | 
			
		||||
     * @param userId
 | 
			
		||||
     * @param username
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    public static String createToken(Long userId, String username) {
 | 
			
		||||
        String token = Jwts.builder()
 | 
			
		||||
                //分类
 | 
			
		||||
                .setSubject("AUTH-USER")
 | 
			
		||||
 | 
			
		||||
                //设置Token有效时长
 | 
			
		||||
                .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))
 | 
			
		||||
 | 
			
		||||
                //设置主体部分
 | 
			
		||||
                .claim("userId", userId)
 | 
			
		||||
                .claim("username", username)
 | 
			
		||||
 | 
			
		||||
                //签名部分
 | 
			
		||||
                .signWith(SignatureAlgorithm.HS512, tokenSignKey)
 | 
			
		||||
                .compressWith(CompressionCodecs.GZIP)
 | 
			
		||||
                .compact();
 | 
			
		||||
        return token;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 从生成的Token中获取id
 | 
			
		||||
     *
 | 
			
		||||
     * @param token
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    public static Long getUserId(String token) {
 | 
			
		||||
        try {
 | 
			
		||||
            if (StringUtils.isEmpty(token)) return null;
 | 
			
		||||
 | 
			
		||||
            Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
 | 
			
		||||
            Claims claims = claimsJws.getBody();
 | 
			
		||||
            Integer userId = (Integer) claims.get("userId");
 | 
			
		||||
            return userId.longValue();
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 从Token中获取用户名称
 | 
			
		||||
     *
 | 
			
		||||
     * @param token
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    public static String getUsername(String token) {
 | 
			
		||||
        try {
 | 
			
		||||
            if (StringUtils.isEmpty(token)) return "";
 | 
			
		||||
 | 
			
		||||
            Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
 | 
			
		||||
            Claims claims = claimsJws.getBody();
 | 
			
		||||
            return (String) claims.get("username");
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 测试
 | 
			
		||||
     *
 | 
			
		||||
     * @param args
 | 
			
		||||
     */
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        String token = JwtHelper.createToken(1L, "admin");
 | 
			
		||||
        System.out.println(token);
 | 
			
		||||
        String username = JwtHelper.getUsername(token);
 | 
			
		||||
        Long userId = JwtHelper.getUserId(token);
 | 
			
		||||
 | 
			
		||||
        System.out.println("username = " + username);
 | 
			
		||||
        System.out.println("userId = " + userId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -18,7 +18,7 @@ public enum ResultCodeEnum {
 | 
			
		||||
    FAIL(201, "失败"),
 | 
			
		||||
    SERVICE_ERROR(2012, "服务异常"),
 | 
			
		||||
    DATA_ERROR(204, "数据异常"),
 | 
			
		||||
 | 
			
		||||
    LOGIN_ERROR(205, "认证失败"),
 | 
			
		||||
    LOGIN_AUTH(208, "未登陆"),
 | 
			
		||||
    PERMISSION(209, "没有权限");
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,33 @@
 | 
			
		||||
package com.atguigu.common.utils;
 | 
			
		||||
 | 
			
		||||
import java.security.MessageDigest;
 | 
			
		||||
import java.security.NoSuchAlgorithmException;
 | 
			
		||||
 | 
			
		||||
public final class MD5 {
 | 
			
		||||
 | 
			
		||||
    public static String encrypt(String strSrc) {
 | 
			
		||||
        try {
 | 
			
		||||
            char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
 | 
			
		||||
                    '9', 'a', 'b', 'c', 'd', 'e', 'f'};
 | 
			
		||||
            byte[] bytes = strSrc.getBytes();
 | 
			
		||||
            MessageDigest md = MessageDigest.getInstance("MD5");
 | 
			
		||||
            md.update(bytes);
 | 
			
		||||
            bytes = md.digest();
 | 
			
		||||
            int j = bytes.length;
 | 
			
		||||
            char[] chars = new char[j * 2];
 | 
			
		||||
            int k = 0;
 | 
			
		||||
            for (byte b : bytes) {
 | 
			
		||||
                chars[k++] = hexChars[b >>> 4 & 0xf];
 | 
			
		||||
                chars[k++] = hexChars[b & 0xf];
 | 
			
		||||
            }
 | 
			
		||||
            return new String(chars);
 | 
			
		||||
        } catch (NoSuchAlgorithmException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
            throw new RuntimeException("MD5加密出错!!+" + e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        System.out.println(MD5.encrypt("111111"));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,30 @@
 | 
			
		||||
package com.atguigu.common.utils;
 | 
			
		||||
 | 
			
		||||
import com.atguigu.common.result.Result;
 | 
			
		||||
import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
import org.springframework.http.HttpStatus;
 | 
			
		||||
import org.springframework.http.MediaType;
 | 
			
		||||
 | 
			
		||||
import javax.servlet.http.HttpServletResponse;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * ClassName: ResponseUtil
 | 
			
		||||
 * Package: com.atguigu.common.utils
 | 
			
		||||
 *
 | 
			
		||||
 * @author yovinchen
 | 
			
		||||
 * @Create 2023/6/10 23:42
 | 
			
		||||
 */
 | 
			
		||||
public class ResponseUtil {
 | 
			
		||||
 | 
			
		||||
    public static void out(HttpServletResponse response, Result r) {
 | 
			
		||||
        ObjectMapper mapper = new ObjectMapper();
 | 
			
		||||
        response.setStatus(HttpStatus.OK.value());
 | 
			
		||||
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
 | 
			
		||||
        try {
 | 
			
		||||
            mapper.writeValue(response.getWriter(), r);
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user