|
|
|
@ -9,9 +9,9 @@ import com.auth0.jwt.algorithms.Algorithm;
|
|
|
|
|
import com.auth0.jwt.exceptions.JWTDecodeException;
|
|
|
|
|
import com.auth0.jwt.exceptions.JWTVerificationException;
|
|
|
|
|
import com.ibeetl.admin.core.util.HttpRequestLocal;
|
|
|
|
|
import com.ibeetl.jlw.entity.AbstractToken;
|
|
|
|
|
import com.ibeetl.jlw.entity.Student;
|
|
|
|
|
import com.ibeetl.jlw.entity.Teacher;
|
|
|
|
|
import com.ibeetl.jlw.entity.TokenInterface;
|
|
|
|
|
import com.ibeetl.jlw.service.StudentService;
|
|
|
|
|
import com.ibeetl.jlw.service.TeacherService;
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
@ -37,7 +37,7 @@ public class TokenService {
|
|
|
|
|
|
|
|
|
|
//静态参数
|
|
|
|
|
|
|
|
|
|
public static Map<String, TokenInterface> tokenMap = new ConcurrentHashMap<>();
|
|
|
|
|
public static Map<String, AbstractToken> tokenMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
|
//初始化
|
|
|
|
|
public static void init(){
|
|
|
|
@ -46,7 +46,7 @@ public class TokenService {
|
|
|
|
|
List<Student> studentList = studentService.getValues(null);
|
|
|
|
|
List<Teacher> teacherList = teacherService.getValues(null);
|
|
|
|
|
|
|
|
|
|
List<TokenInterface> lists = new ArrayList<>(16);
|
|
|
|
|
List<AbstractToken> lists = new ArrayList<>(16);
|
|
|
|
|
if (CollectionUtil.isNotEmpty(studentList)) {
|
|
|
|
|
lists.addAll(studentList);
|
|
|
|
|
}
|
|
|
|
@ -54,9 +54,9 @@ public class TokenService {
|
|
|
|
|
lists.addAll(teacherList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (TokenInterface tokenInterface : lists) {
|
|
|
|
|
String tokenKey = tokenInterface.getIdentity().getAttributeName() + "_" + tokenInterface.getId();
|
|
|
|
|
tokenMap.put(tokenKey, tokenInterface);
|
|
|
|
|
for (AbstractToken abstractToken : lists) {
|
|
|
|
|
String tokenKey = abstractToken.getIdentity().getAttributeName() + "_" + abstractToken.getId();
|
|
|
|
|
tokenMap.put(tokenKey, abstractToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -67,7 +67,7 @@ public class TokenService {
|
|
|
|
|
List<Student> studentList = studentService.getByIds(ids);
|
|
|
|
|
List<Teacher> teacherList = teacherService.getByIds(ids);
|
|
|
|
|
|
|
|
|
|
List<TokenInterface> lists = new ArrayList<>(16);
|
|
|
|
|
List<AbstractToken> lists = new ArrayList<>(16);
|
|
|
|
|
if (CollectionUtil.isNotEmpty(studentList)) {
|
|
|
|
|
lists.addAll(studentList);
|
|
|
|
|
}
|
|
|
|
@ -75,36 +75,36 @@ public class TokenService {
|
|
|
|
|
lists.addAll(teacherList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (TokenInterface tokenInterface : lists) {
|
|
|
|
|
String tokenKey = tokenInterface.getIdentity().getAttributeName() + "_" + tokenInterface.getId();
|
|
|
|
|
tokenMap.put(tokenKey, tokenInterface);
|
|
|
|
|
for (AbstractToken abstractToken : lists) {
|
|
|
|
|
String tokenKey = abstractToken.getIdentity().getAttributeName() + "_" + abstractToken.getId();
|
|
|
|
|
tokenMap.put(tokenKey, abstractToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取会员信息简述,合并了微信信息
|
|
|
|
|
public static TokenInterface getTokenInterfaceInfo(String tokenId) {
|
|
|
|
|
TokenInterface tokenInterface = ToolUtils.deepCopy(tokenMap.get(tokenId));
|
|
|
|
|
return tokenInterface;
|
|
|
|
|
public static AbstractToken getTokenInterfaceInfo(String tokenId) {
|
|
|
|
|
AbstractToken abstractToken = ToolUtils.deepCopy(tokenMap.get(tokenId));
|
|
|
|
|
return abstractToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Map<String,Object> getMap(TokenInterface tokenInterface, TokenService tokenService){
|
|
|
|
|
TokenInterface student = ToolUtils.deepCopy(tokenInterface);
|
|
|
|
|
public static Map<String,Object> getMap(AbstractToken abstractToken, TokenService tokenService){
|
|
|
|
|
AbstractToken student = ToolUtils.deepCopy(abstractToken);
|
|
|
|
|
Map<String,Object> map = new HashMap<>();
|
|
|
|
|
//生成token
|
|
|
|
|
String token = tokenService.getToken(student);
|
|
|
|
|
map.put("token",token);
|
|
|
|
|
map.put("member", getTokenInterfaceInfo(tokenInterface.getId()));
|
|
|
|
|
map.put("member", getTokenInterfaceInfo(abstractToken.getId()));
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getToken(TokenInterface tokenInterface) {
|
|
|
|
|
public String getToken(AbstractToken abstractToken) {
|
|
|
|
|
|
|
|
|
|
String token="";
|
|
|
|
|
token= JWT.create()
|
|
|
|
|
.withAudience(tokenInterface.getId())
|
|
|
|
|
.withClaim(tokenInterface.getId(), tokenInterface.getIdentity().getAttributeName())
|
|
|
|
|
.sign(Algorithm.HMAC256((tokenInterface.getAddTime().getTime()) + ""));
|
|
|
|
|
.withAudience(abstractToken.getId())
|
|
|
|
|
.withClaim(abstractToken.getId(), abstractToken.getIdentity().getAttributeName())
|
|
|
|
|
.sign(Algorithm.HMAC256((abstractToken.getAddTime().getTime()) + ""));
|
|
|
|
|
|
|
|
|
|
//加时间戳区分登陆不同设备的同一个账户
|
|
|
|
|
token = token+"."+System.currentTimeMillis();
|
|
|
|
@ -131,19 +131,19 @@ public class TokenService {
|
|
|
|
|
j.printStackTrace();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
TokenInterface tokenInterface = null;
|
|
|
|
|
AbstractToken abstractToken = null;
|
|
|
|
|
try{
|
|
|
|
|
tokenInterface = ToolUtils.deepCopy(tokenMap.get(Long.parseLong(id)));
|
|
|
|
|
abstractToken = ToolUtils.deepCopy(tokenMap.get(Long.parseLong(id)));
|
|
|
|
|
}catch (Exception e){}
|
|
|
|
|
|
|
|
|
|
// 验证 token
|
|
|
|
|
JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256((tokenInterface.getAddTime().getTime())+"")).build();
|
|
|
|
|
JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256((abstractToken.getAddTime().getTime())+"")).build();
|
|
|
|
|
try {
|
|
|
|
|
jwtVerifier.verify(token);
|
|
|
|
|
} catch (JWTVerificationException e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
Object[] o = {tokenInterface , null};
|
|
|
|
|
Object[] o = {abstractToken, null};
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|