修改JwtUtil的部分方法
This commit is contained in:
@@ -31,6 +31,11 @@ public enum ResponseEnum {
|
|||||||
PWD_SAME(360, "新密码与原密码相同"),
|
PWD_SAME(360, "新密码与原密码相同"),
|
||||||
LOGIN_EXPIRED(370, "登陆过期"),
|
LOGIN_EXPIRED(370, "登陆过期"),
|
||||||
|
|
||||||
|
JWT_EXPIRED(381, "Token过期"),
|
||||||
|
JWT_MALFORMED(382, "Token格式不对"),
|
||||||
|
JWT_SIGNATURE(383, "Token签名错误"),
|
||||||
|
JWT_NOT_SUPPORT(384, "不支持的Token"),
|
||||||
|
|
||||||
//标签
|
//标签
|
||||||
TAG_NOT_EXIST(401, "标签不存在"),
|
TAG_NOT_EXIST(401, "标签不存在"),
|
||||||
TAG_HAS_EXIST(402, "标签已存在"),
|
TAG_HAS_EXIST(402, "标签已存在"),
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package cn.celess.blog.util;
|
package cn.celess.blog.util;
|
||||||
|
|
||||||
|
import cn.celess.blog.enmu.ResponseEnum;
|
||||||
import cn.celess.blog.entity.User;
|
import cn.celess.blog.entity.User;
|
||||||
import io.jsonwebtoken.Claims;
|
import cn.celess.blog.exception.MyException;
|
||||||
import io.jsonwebtoken.ExpiredJwtException;
|
import io.jsonwebtoken.*;
|
||||||
import io.jsonwebtoken.Jwts;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import io.jsonwebtoken.SignatureAlgorithm;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@@ -19,6 +19,7 @@ import java.util.Map;
|
|||||||
* @Description: JWT工具类
|
* @Description: JWT工具类
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
|
@Log4j2
|
||||||
public class JwtUtil {
|
public class JwtUtil {
|
||||||
private static final String CLAIM_KEY_USERNAME = "sub";
|
private static final String CLAIM_KEY_USERNAME = "sub";
|
||||||
|
|
||||||
@@ -48,46 +49,63 @@ public class JwtUtil {
|
|||||||
.compact();
|
.compact();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean validateToken(String token, User user) {
|
public String updateTokenDate(String token) {
|
||||||
String username = getUsernameFromToken(token);
|
Claims claims = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();
|
||||||
|
return Jwts.builder()
|
||||||
return (username.equals(user.getEmail()) && !isTokenExpired(token));
|
.setClaims(claims)
|
||||||
|
.setExpiration(new Date(claims.getExpiration().getTime() + EXPIRATION_SHORT_TIME))
|
||||||
|
.signWith(SignatureAlgorithm.HS512, SECRET)
|
||||||
|
.compact();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取token是否过期
|
* 获取token是否过期
|
||||||
*/
|
*/
|
||||||
public Boolean isTokenExpired(String token) {
|
public Boolean isTokenExpired(String token) {
|
||||||
try {
|
|
||||||
Date expiration = getExpirationDateFromToken(token);
|
Date expiration = getExpirationDateFromToken(token);
|
||||||
return expiration.before(new Date());
|
return expiration == null || expiration.before(new Date());
|
||||||
} catch (ExpiredJwtException e) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据token获取username
|
* 根据token获取username
|
||||||
*/
|
*/
|
||||||
public String getUsernameFromToken(String token) {
|
public String getUsernameFromToken(String token) {
|
||||||
return getClaimsFromToken(token).getSubject();
|
Claims claims = getClaimsFromToken(token);
|
||||||
|
return claims == null ? null : claims.getSubject();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取token的过期时间
|
* 获取token的过期时间
|
||||||
*/
|
*/
|
||||||
public Date getExpirationDateFromToken(String token) {
|
public Date getExpirationDateFromToken(String token) {
|
||||||
return getClaimsFromToken(token).getExpiration();
|
Claims claims = getClaimsFromToken(token);
|
||||||
|
return claims == null ? null : claims.getExpiration();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解析JWT
|
* 解析JWT
|
||||||
*/
|
*/
|
||||||
private Claims getClaimsFromToken(String token) {
|
private Claims getClaimsFromToken(String token) {
|
||||||
Claims claims = Jwts.parser()
|
Claims claims = null;
|
||||||
|
try {
|
||||||
|
claims = Jwts.parser()
|
||||||
.setSigningKey(SECRET)
|
.setSigningKey(SECRET)
|
||||||
.parseClaimsJws(token)
|
.parseClaimsJws(token)
|
||||||
.getBody();
|
.getBody();
|
||||||
|
} catch (ExpiredJwtException e) {
|
||||||
|
log.info("JWT令牌过期");
|
||||||
|
} catch (UnsupportedJwtException e) {
|
||||||
|
log.info("不支持的JWT令牌");
|
||||||
|
throw new MyException(ResponseEnum.JWT_NOT_SUPPORT);
|
||||||
|
} catch (MalformedJwtException e) {
|
||||||
|
log.info("JWT令牌格式错误");
|
||||||
|
throw new MyException(ResponseEnum.JWT_MALFORMED);
|
||||||
|
} catch (SignatureException e) {
|
||||||
|
log.info("JWT签名错误");
|
||||||
|
throw new MyException(ResponseEnum.JWT_SIGNATURE);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
log.debug("JWT非法参数");
|
||||||
|
}
|
||||||
return claims;
|
return claims;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,51 +2,79 @@ package cn.celess.blog.util;
|
|||||||
|
|
||||||
import cn.celess.blog.BaseTest;
|
import cn.celess.blog.BaseTest;
|
||||||
import cn.celess.blog.entity.User;
|
import cn.celess.blog.entity.User;
|
||||||
|
import io.jsonwebtoken.Jwts;
|
||||||
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
|
import org.junit.FixMethodOrder;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.runners.MethodSorters;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||||
public class JwtUtilTest extends BaseTest {
|
public class JwtUtilTest extends BaseTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
JwtUtil jwtUtil;
|
JwtUtil jwtUtil;
|
||||||
|
@Value("${jwt.secret}")
|
||||||
|
private String secret;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void generateToken() {
|
public void testGenerateToken() {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setEmail("a@celess.cn");
|
user.setEmail("a@celess.cn");
|
||||||
String s = jwtUtil.generateToken(user, true);
|
String s = jwtUtil.generateToken(user, false);
|
||||||
System.out.println(s);
|
|
||||||
assertNotNull(s);
|
assertNotNull(s);
|
||||||
|
String str = null;
|
||||||
|
try {
|
||||||
|
str = jwtUtil.generateToken(null, false);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
assertNull(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void validateToken() {
|
public void testIsTokenExpired() throws InterruptedException {
|
||||||
|
String s = Jwts.builder()
|
||||||
|
.setClaims(null)
|
||||||
|
.setExpiration(new Date(Instant.now().toEpochMilli() + 1000))
|
||||||
|
.signWith(SignatureAlgorithm.HS512, secret)
|
||||||
|
.compact();
|
||||||
|
Thread.sleep(1010);
|
||||||
|
assertTrue(jwtUtil.isTokenExpired(s));
|
||||||
|
assertFalse(jwtUtil.isTokenExpired(jwtUtil.generateToken(new User(), false)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetUsernameFromToken() {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setEmail("a@celess.cn");
|
user.setEmail("a@celess.cn");
|
||||||
assertTrue(jwtUtil.validateToken(createToken(), user));
|
String s = jwtUtil.generateToken(user, false);
|
||||||
|
assertEquals(user.getEmail(), jwtUtil.getUsernameFromToken(s));
|
||||||
|
user.setEmail("example@celess.cn");
|
||||||
|
assertNotEquals(user.getEmail(), jwtUtil.getUsernameFromToken(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isTokenExpired() {
|
public void testGetExpirationDateFromToken() {
|
||||||
assertFalse(jwtUtil.isTokenExpired(createToken()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void getUsernameFromToken() {
|
|
||||||
assertEquals("a@celess.cn", jwtUtil.getUsernameFromToken(createToken()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void getExpirationDateFromToken() {
|
|
||||||
assertNotNull(jwtUtil.getExpirationDateFromToken(createToken()));
|
|
||||||
}
|
|
||||||
|
|
||||||
private String createToken() {
|
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setEmail("a@celess.cn");
|
user.setEmail("a@celess.cn");
|
||||||
return jwtUtil.generateToken(user, true);
|
String s = jwtUtil.generateToken(user, false);
|
||||||
|
assertNotNull(jwtUtil.getExpirationDateFromToken(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateTokenDate() {
|
||||||
|
User user = new User();
|
||||||
|
user.setEmail("a@celess.cn");
|
||||||
|
String s = jwtUtil.generateToken(user, false);
|
||||||
|
Date before = jwtUtil.getExpirationDateFromToken(s);
|
||||||
|
String s1 = jwtUtil.updateTokenDate(s);
|
||||||
|
assertTrue(jwtUtil.getExpirationDateFromToken(s1).getTime() - jwtUtil.getExpirationDateFromToken(s).getTime() > 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user