|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
package cn.jlw.token;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
|
|
import cn.jlw.util.SpringContextUtil;
|
|
|
|
|
import cn.jlw.util.ToolUtils;
|
|
|
|
|
import com.auth0.jwt.JWT;
|
|
|
|
|
import com.auth0.jwt.JWTVerifier;
|
|
|
|
@ -8,14 +10,22 @@ import com.auth0.jwt.exceptions.JWTDecodeException;
|
|
|
|
|
import com.auth0.jwt.exceptions.JWTVerificationException;
|
|
|
|
|
import com.ibeetl.admin.core.util.HttpRequestLocal;
|
|
|
|
|
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;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
|
|
|
|
import static com.ibeetl.admin.core.service.CorePlatformService.TOKEN_KEY;
|
|
|
|
|
import static com.ibeetl.jlw.web.StudentController.studentMap;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class TokenService {
|
|
|
|
@ -25,17 +35,82 @@ public class TokenService {
|
|
|
|
|
@Resource
|
|
|
|
|
HttpRequestLocal httpRequestLocal;
|
|
|
|
|
|
|
|
|
|
public String getToken(Student student) {
|
|
|
|
|
//静态参数
|
|
|
|
|
|
|
|
|
|
public static Map<String, TokenInterface> tokenMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
|
//初始化
|
|
|
|
|
public static void init(){
|
|
|
|
|
StudentService studentService = (StudentService) SpringContextUtil.getBean("studentService");
|
|
|
|
|
TeacherService teacherService = (TeacherService) SpringContextUtil.getBean("teacherService");
|
|
|
|
|
List<Student> studentList = studentService.getValues(null);
|
|
|
|
|
List<Teacher> teacherList = teacherService.getValues(null);
|
|
|
|
|
|
|
|
|
|
List<TokenInterface> lists = new ArrayList<>(16);
|
|
|
|
|
if (CollectionUtil.isNotEmpty(studentList)) {
|
|
|
|
|
lists.addAll(studentList);
|
|
|
|
|
}
|
|
|
|
|
if (CollectionUtil.isNotEmpty(studentList)) {
|
|
|
|
|
lists.addAll(teacherList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (TokenInterface tokenInterface : lists) {
|
|
|
|
|
String tokenKey = tokenInterface.getIdentity().getAttributeName() + "_" + tokenInterface.getId();
|
|
|
|
|
tokenMap.put(tokenKey, tokenInterface);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//更新缓存
|
|
|
|
|
public static void updateByIds(String ids){
|
|
|
|
|
StudentService studentService = (StudentService) SpringContextUtil.getBean("studentService");
|
|
|
|
|
TeacherService teacherService = (TeacherService) SpringContextUtil.getBean("teacherService");
|
|
|
|
|
List<Student> studentList = studentService.getByIds(ids);
|
|
|
|
|
List<Teacher> teacherList = teacherService.getByIds(ids);
|
|
|
|
|
|
|
|
|
|
List<TokenInterface> lists = new ArrayList<>(16);
|
|
|
|
|
if (CollectionUtil.isNotEmpty(studentList)) {
|
|
|
|
|
lists.addAll(studentList);
|
|
|
|
|
}
|
|
|
|
|
if (CollectionUtil.isNotEmpty(studentList)) {
|
|
|
|
|
lists.addAll(teacherList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (TokenInterface tokenInterface : lists) {
|
|
|
|
|
String tokenKey = tokenInterface.getIdentity().getAttributeName() + "_" + tokenInterface.getId();
|
|
|
|
|
tokenMap.put(tokenKey, tokenInterface);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取会员信息简述,合并了微信信息
|
|
|
|
|
public static TokenInterface getTokenInterfaceInfo(String tokenId) {
|
|
|
|
|
TokenInterface tokenInterface = ToolUtils.deepCopy(tokenMap.get(tokenId));
|
|
|
|
|
return tokenInterface;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Map<String,Object> getMap(TokenInterface tokenInterface, TokenService tokenService){
|
|
|
|
|
TokenInterface student = ToolUtils.deepCopy(tokenInterface);
|
|
|
|
|
Map<String,Object> map = new HashMap<>();
|
|
|
|
|
//生成token
|
|
|
|
|
String token = tokenService.getToken(student);
|
|
|
|
|
map.put("token",token);
|
|
|
|
|
map.put("member", getTokenInterfaceInfo(tokenInterface.getId()));
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getToken(TokenInterface tokenInterface) {
|
|
|
|
|
|
|
|
|
|
String token="";
|
|
|
|
|
token= JWT.create()
|
|
|
|
|
.withAudience(student.getStudentId().toString())
|
|
|
|
|
.sign(Algorithm.HMAC256((student.getAddTime().getTime())+""));
|
|
|
|
|
.withAudience(tokenInterface.getId())
|
|
|
|
|
.withClaim(tokenInterface.getId(), tokenInterface.getIdentity().getAttributeName())
|
|
|
|
|
.sign(Algorithm.HMAC256((tokenInterface.getAddTime().getTime()) + ""));
|
|
|
|
|
|
|
|
|
|
token = token+"."+System.currentTimeMillis(); //加时间戳区分登陆不同设备的同一个账户
|
|
|
|
|
//加时间戳区分登陆不同设备的同一个账户
|
|
|
|
|
token = token+"."+System.currentTimeMillis();
|
|
|
|
|
|
|
|
|
|
httpRequestLocal.setSessionValue(TOKEN_KEY, token);//丢到session里面
|
|
|
|
|
//丢到session里面
|
|
|
|
|
httpRequestLocal.setSessionValue(TOKEN_KEY, token);
|
|
|
|
|
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
@ -56,19 +131,19 @@ public class TokenService {
|
|
|
|
|
j.printStackTrace();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
Student student = null;
|
|
|
|
|
TokenInterface tokenInterface = null;
|
|
|
|
|
try{
|
|
|
|
|
student = ToolUtils.deepCopy(studentMap.get(Long.parseLong(id)));
|
|
|
|
|
tokenInterface = ToolUtils.deepCopy(tokenMap.get(Long.parseLong(id)));
|
|
|
|
|
}catch (Exception e){}
|
|
|
|
|
|
|
|
|
|
// 验证 token
|
|
|
|
|
JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256((student.getAddTime().getTime())+"")).build();
|
|
|
|
|
JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256((tokenInterface.getAddTime().getTime())+"")).build();
|
|
|
|
|
try {
|
|
|
|
|
jwtVerifier.verify(token);
|
|
|
|
|
} catch (JWTVerificationException e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
Object o[] = {student,null};
|
|
|
|
|
Object o[] = {tokenInterface , null};
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|