[英]Spring Boot JWT Authentication: trigger a method after login and logout
[英]How can logout using spring boot jwt
I am using this example https://dzone.com/articles/spring-boot-security-json-web-tokenjwt-hello-world for creating spring boot rest api with json web token (JWT). 但我没有找到任何 api 使用 io.jsonwebtoken maven 依赖项强制注销。
我在 pom 中使用这个依赖:
groupId io.jsonwebtoken artifactId jjwt version 0.9.1
任何人都可以告诉我这种依赖关系,是否提供任何注销或撤销令牌 api。 如果没有,请提供使用此过程强制注销的任何解决方案。
注销可以做几件事:
window.sessionStorage.removeItem("token") // 用于 session 存储
或者
window.localstorage.removeItem("token") // 用于本地存储
参考他们: https://developer.mozilla.org/ru/docs/Web/API/Window/sessionStorage https://developer.mozilla.org/ru/docs/Web/API/Window/localStorage
我在 angular 中的示例: https://github.com/dmcheremisin/TodoApp/blob/master/frontend/src/app/service/jwt-authentication.service.ts
如果您需要允许进一步使用令牌 - 您刷新它,否则拒绝。
示例刷新方法:
public String refreshToken(String token) {
final Date createdDate = new Date();
final Date expirationDate = calculateExpirationDate(createdDate);
final Claims claims = getAllClaimsFromToken(token);
claims.setIssuedAt(createdDate);
claims.setExpiration(expirationDate);
return Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS512, secret).compact();
}
此代码片段来自我使用相同库 jjwt 的存储库: https://github.com/dmcheremisin/TodoApp/blob/master/backend/src/main/java/com/todo/app/util/JwtTokenUtil.Z93F725A07423FE1C889F448B33D21F46
相关文章: https://medium.com/devgorilla/how-to-log-out-when-using-jwt-a8c7823e8a6
我相信代币有有效期。 您可以简单地缩短到期时间,这样如果令牌被黑客入侵,那么它在到期后就没有用了
我们可以通过更改密钥来实现这一点。 通常我们为所有用户维护一个密钥,因此如果我们更改密钥,它将撤销所有用户的访问权限。 我们可以为每个用户维护唯一的密钥,并且根据注销的请求,我们可以删除/更改使用关联的密钥。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.