Commit 619b4e5f by GongDaCheng

设置Token失效时间

parent 22deff34
...@@ -48,7 +48,7 @@ public class AuthenticationInterceptor implements HandlerInterceptor { ...@@ -48,7 +48,7 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
if (userLoginToken.required()) { if (userLoginToken.required()) {
// 执行认证 // 执行认证
if (token == null) { if (token == null) {
throw new RuntimeException("无token,请重新登录"); throw new RuntimeException("403");
} }
// 获取 token 中的 user id // 获取 token 中的 user id
String userId; String userId;
......
...@@ -5,6 +5,8 @@ import com.auth0.jwt.algorithms.Algorithm; ...@@ -5,6 +5,8 @@ import com.auth0.jwt.algorithms.Algorithm;
import com.jinyun.web.entity.User; import com.jinyun.web.entity.User;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Date;
/** /**
* @author jinbin * @author jinbin
...@@ -14,7 +16,9 @@ import org.springframework.stereotype.Service; ...@@ -14,7 +16,9 @@ import org.springframework.stereotype.Service;
public class TokenService { public class TokenService {
public String getToken(User user) { public String getToken(User user) {
String token=""; String token="";
token= JWT.create().withAudience(user.getId())// 将 user id 保存到 token 里面 Date expires = new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000);
token= JWT.create().withExpiresAt(expires)
.withAudience(user.getId())// 将 user id 保存到 token 里面
.sign(Algorithm.HMAC256(user.getPassword()));// 以 password 作为 token 的密钥 .sign(Algorithm.HMAC256(user.getPassword()));// 以 password 作为 token 的密钥
return token; return token;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论