admin 管理员组

文章数量: 887021


2024年1月17日发(作者:html5网站开发教学视频)

//当前的签名的秘钥 private String secret = "cdd"; //token的有效时间

约25min private Long expiration = 1296000L; public String getUsernameFromToken(String token) { String username; try { final Claims claims = getClaimsFromToken(token); username = ject(); } catch (Exception e) { username = null; } return username; } public Date getCreatDateFromToken(String token) { Date created; try { final Claims claims = getClaimsFromToken(token); created = new Date((Long) (CLAIM_KEY_CREATED)); } catch (Exception e) { created = null; } return created; } //得到token的有效期 private Date getExpirationDateFromToken(String token) { Date expiration; try { final Claims claims = getClaimsFromToken(token); expiration = iration(); } catch (Exception e) { expiration = null; } return expiration; } public String getAudienceFromToken(String token) { String audience; try { final Claims claims = getClaimsFromToken(token); audience = (String) (CLAIM_KEY_AUDIENCE); } catch (Exception e) { audience = null; } return audience; } private Claims getClaimsFromToken(String token) { Claims claims; try { claims = () .setSigningKey(secret) .parseClaimsJws(token) .getBody(); } catch (Exception e) { claims = null; } return claims; } //设置过期时间 private Date generateExpeirationDate() { return new Date(tTimeMillis() + expiration * 1000);

} private Boolean isTokenExpired(String token) { final Date expiration = getExpirationDateFromToken(token); return (new Date()); } private Boolean isCreatedAfterTenMinutes(Date created) { int minutes = (int) ((tTimeMillis() - e()) / (1000 * 60)); if (minutes >= 10) { return true; } return false; } private String generateAudience(Device device) { String audience = AUDIENCE_UNKNOWN; if (al()) { audience = AUDIENCE_WEB; } else if (et()) { audience = AUDIENCE_TABLET; } else if (le()) { audience = AUDIENCE_MOBILE; } return audience; } private Boolean ignoreTokenExpiration(String token) { String audience = getAudienceFromToken(token); return (AUDIENCE_(audience) || AUDIENCE_(audience)); } String generateToken(Map claims) { return r() .setClaims(claims) .setExpiration(generateExpeirationDate()) .signWith(512, secret) .compact(); } public String generateToken(UserDetails userDetails) { Map claims = new HashMap<>(); (CLAIM_KEY_USERNAME, rname()); (CLAIM_KEY_CREATED, new Date()); return generateToken(claims); } //判断是否在10分钟后并在有效期内 public Boolean canTokenBeRefreshed(String token) { final Date created = getCreatDateFromToken(token); return token != null && created != null && isCreatedAfterTenMinutes(created) && (!isTokenExpired(token)) || ignoreTokenExpiration(token); } public String refreshToken(String token) { String refreshedToken; try { final Claims claims = getClaimsFromToken(token); (CLAIM_KEY_CREATED, new Date()); refreshedToken = generateToken(claims); } catch (Exception e) { refreshedToken = null; } ("获取要刷新的token: {}", refreshedToken);


本文标签: 网站 开发 签名 有效期 设置