前言
在之前的代码上,再加上token等其他功能。
一、JwtTokenUtil
import com.meeting.entity.User;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Component
public class JwtTokenUtil {
private static final String CLAIM_KEY_USERNAME = "username";
private static final String CLAIM_KEY_EMAIL = "email";
private static final String CLAIM_KEY_ID = "id";
private static final String CLAIM_KEY_CREATED = "created";
private static final String CLAIM_KEY_ROLES = "roles";
private static final String SECRET = "secret";
private static final int EXPIRATION = 604800;
public String getUsernameFromToken(String token) {
String username;
try {
username = getClaimsFromToken(token).getSubject();
} catch (Exception e) {
username = null;
}
return username;
}
public Date getCreatedDateFromToken(String token) {
Date created;
try {
final Claims claims = getClaimsFromToken(token);
created = new Date((Long) claims.get(CLAIM_KEY_CREATED));
} catch (Exception e) {
created = null;
}
return created;
}
public Date getExpirationDateFromToken(String token) {
Date expiration;
try {
final Claims claims = getClaimsFromToken(token);
expiration = claims.getExpiration();
} catch (Exception e) {
expiration = null;
}
return expiration;
}
public String getEmailFromToken(String token) {
String email;
try {
final Claims claims = getClaimsFromToken(token);
email = (String) claims.get(CLAIM_KEY_EMAIL);
} catch (Exception e) {
email = null;
}
return email;
}
private Claims getClaimsFromToken(String token) {
Claims claims;
try {
claims = Jwts
.parser()
.setSigningKey(SECRET)
.parseClaimsJws(token)
.getBody();
} catch (Exception e) {
claims = null;
}
return claims;
}
private Date generateExpirationDate() {
return new Date(System.currentTimeMillis() + EXPIRATION * 1000L);
}
private Boolean isTokenExpired(String token) {
final Date expiration = getExpirationDateFromToken(token);
return expiration.before(new Date());
}
public String generateToken(User user) {
Map<String, Object> claims = new HashMap<>();
claims.put(CLAIM_KEY_USERNAME, user.getUsername());
claims.put(CLAIM_KEY_CREATED, new Date());
claims.put(CLAIM_KEY_ID, user.getId());
claims.put(CLAIM_KEY_EMAIL, user.getEmail());
claims.put(CLAIM_KEY_ROLES, user.getAuthorities());
return generateToken(claims);
}
public String generateToken(Map<String, Object> claims) {
return Jwts.builder()
.setClaims(claims)
.setExpiration(generateExpirationDate())
.signWith(SignatureAlgorithm.HS512, SECRET)
.compact();
}
public Boolean canTokenBeRefreshed(String token) {
return !isTokenExpired(token);
}
public String refreshToken(String token) {
String refreshedToken;
try {
final Claims claims = getClaimsFromToken(token);
claims.put(CLAIM_KEY_CREATED, new Date());
refreshedToken = generateToken(claims);
} catch (Exception e) {
refreshedToken = null;
}
return refreshedToken;
}
public Boolean validateToken(String token) {
return !isTokenExpired(token);
}
}
二、全局异常处理
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@ControllerAdvice
public class GlobalExceptionHandler {
@ResponseBody
@ExceptionHandler(value = {Exception.class})
public Map<String, Object> exceptionHandler(Exception exception) {
// Todo 日志记录
System.out.println(exception.getMessage());
Map<String, Object> map = new HashMap<>();
map.put("code", 400);
map.put("message", exception.getMessage());
return map;
}
}
在yaml文件下添加web.resources.add-mappings设置为false。
spring:
datasource:
url:
username:
password:
driver-class-name: com.mysql.cj.jdbc.Driver
mvc:
# 出现异常时,直接抛出异常
throw-exception-if-no-handler-found: true
web:
resources:
# 不要为工程中的资源文件建立映射
add-mappings: false