提交 965ef9b3 编写于 作者: Tyl666's avatar Tyl666

【JWT鉴权续期接口】上传api、entity、service代码

上级 15f735d6
......@@ -6,11 +6,13 @@ import cn.ibizlab.core.uaa.extensions.service.SysAppService;
import cn.ibizlab.core.uaa.extensions.service.UAACoreService;
import cn.ibizlab.core.uaa.service.ISysUserService;
import cn.ibizlab.util.domain.IBZUSER;
import cn.ibizlab.util.domain.Token;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.helper.CachedBeanCopier;
import cn.ibizlab.util.security.*;
import cn.ibizlab.util.service.AuthenticationUserService;
import cn.ibizlab.util.service.IBZUSERService;
import cn.ibizlab.util.service.TokenReflashService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -19,10 +21,13 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.http.ResponseEntity;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
/**
......@@ -56,6 +61,9 @@ public class ClientAuthenticationResource
@Autowired
private ISysUserService userService;;
@Autowired
private TokenReflashService tokenReflashService;
@Value("${ibiz.auth.pwencrymode:0}")
private int pwencrymode;
......@@ -73,6 +81,26 @@ public class ClientAuthenticationResource
// 返回 token
return ResponseEntity.ok().body(new AuthenticationInfo(token,user2));
}
@PostMapping(value = "v7/reflashToken")
public String reflashToken(@Validated @RequestBody @NotNull(message = "token不能为空") String oldToken) {
// 查询过期时间
final Date created = jwtTokenUtil.getExpirationDateFromToken(oldToken);
// 查询token里面的用户名
String username = jwtTokenUtil.getUsernameFromToken(oldToken);
// 根据用户名取缓存的用户对象
AuthenticationUser user = userDetailsService.loadUserByUsername(username);
if (jwtTokenUtil.validateToken(oldToken, user)) {
throw new BadRequestAlertException("token已失效", "", "");
}
Token tok = tokenReflashService.getToken(oldToken);
if (ObjectUtils.isEmpty(tok)) {
return oldToken;
} else {
String newToken = jwtTokenUtil.generateToken(user);
tok = tokenReflashService.setToken(newToken, oldToken);
return tok.getNewToken();
}
}
@PostMapping(value = "v7/changepwd")
public ResponseEntity<Boolean> changepwd(@Validated @RequestBody JSONObject jsonObject){
......
package cn.ibizlab.util.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Token {
private String newToken;
private String oldToken;
private Date date;
}
package cn.ibizlab.util.service;
import cn.ibizlab.util.domain.Token;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.Date;
@Slf4j
@Service
public class TokenReflashService {
@CachePut(value = "ibzuaa_users", key = "'token:'+#p0")
public Token setToken(String newToken, String oldToken) {
Token tok = new Token(newToken, oldToken, new Date());
return tok;
}
@Cacheable(value = "ibzuaa_users", key = "'token:'+#p0")
public Token getToken(String oldToken) {
return null;
}
@CacheEvict(value = "ibzuaa_users", key = "'token:'+#p0")
public Token removeToken(String token) {
return null;
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册