教师登录方法

beetlsql3-dev
Mlxa0324
parent 89e09caa13
commit 1413d73408

@ -1,5 +1,7 @@
package com.ibeetl.admin.core.enums;
import static com.ibeetl.admin.core.service.CorePlatformService.*;
/**
*
* @author lx
@ -7,27 +9,34 @@ package com.ibeetl.admin.core.enums;
public enum MenuEnums {
// 岗位类型
JT_01("管理岗位"),
JT_02("操作岗位"),
JT_01("管理岗位", null),
JT_02("操作岗位", null),
// 管理岗位子类型
JT_S_01("系统管理员"),
JT_S_02("教师管理员"),
JT_S_04("院校管理员"),
JT_S_01("系统管理员", T_ADMIN),
JT_S_02("教师管理员", null),
JT_S_04("院校管理员", T_COLLEGES_ADMIN),
// 操作岗位子类型
JT_S_03("学生"),
JT_S_05("教师");
JT_S_03("学生", T_STUDENT),
JT_S_05("教师", T_TEACHER);
private String name;
private String attributeName;
private
MenuEnums(String name) {
MenuEnums(String name, String attributeName) {
this.name = name;
this.attributeName = attributeName;
}
public String getName() {
return name;
}
public String getAttributeName() {
return attributeName;
}
/**
*

@ -65,9 +65,11 @@ public class CorePlatformService {
public static final String T_TEACHER = "t_teacher";
// 学生session用户信息Key
public static final String T_STUDENT = "t_student";
// 超级管理员session用户信息Key
public static final String T_ADMIN = "t_admin";
/** Token key */
public static final String TOKEN_KEY = "student:token";
public static final String TOKEN_KEY = "core:token";
@Resource HttpRequestLocal httpRequestLocal;
@Resource CoreRoleMenuDao sysRoleMenuDao;

@ -7,7 +7,9 @@ import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.ibeetl.jlw.entity.Student;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.ibeetl.jlw.entity.TokenInterface;
import com.ibeetl.jlw.service.WebPlatformService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -24,8 +26,7 @@ import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import static com.ibeetl.admin.core.service.CorePlatformService.T_STUDENT;
import static com.ibeetl.jlw.web.StudentController.studentMap;
import static cn.jlw.token.TokenService.tokenMap;
/**
* @author TLT
@ -90,8 +91,11 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
if (StringUtils.isNotBlank(token)) {
// 获取 token 中的 memberId或openId
String id;
String attributeName;
try {
id = JWT.decode(token).getAudience().get(0);
DecodedJWT decode = JWT.decode(token);
id = decode.getAudience().get(0);
attributeName = decode.getClaim(id).as(String.class);
} catch (JWTDecodeException j) {
System.out.println("有误的token"+token);
actionLog.delByUUId(httpServletRequest);
@ -105,11 +109,11 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
httpServletResponse.getWriter().write(JSONArray.toJSONString(map));
return false;
}
Student student = null;
TokenInterface tokenInterface = null;
try{
student = ToolUtils.deepCopy(studentMap.get(Long.parseLong(id)));
tokenInterface = ToolUtils.deepCopy(tokenMap.get(attributeName + "_" + id));
}catch (Exception e){}
if (student == null) {
if (tokenInterface == null) {
actionLog.delByUUId(httpServletRequest);
Map<String,Object>map = new HashMap<>();
@ -122,7 +126,7 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
}
// 验证 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) {
@ -137,9 +141,15 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
httpServletResponse.getWriter().write(JSONArray.toJSONString(map));
return false;
}
actionLog.init(httpServletRequest,token,null == student?null:student.getStudentId(),null);
httpServletRequest.setAttribute(T_STUDENT,student);
actionLog.init(httpServletRequest,token,null == tokenInterface?null: Long.valueOf(tokenInterface.getId()),null);
/** SessionKey
* 使 {@link WebPlatformService#getUserInfoFromSessionByIdentity}
*/
// attributeName = tokenInterface.getIdentity().getAttributeName();
if (StringUtils.isNotBlank(attributeName)) {
httpServletRequest.setAttribute(attributeName, tokenInterface);
}
return true;
}
return true;

@ -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;
}

@ -3,6 +3,7 @@ package com.ibeetl.jlw.entity;
import cn.jlw.validate.ValidateConfig;
import com.ibeetl.admin.core.annotation.Dict;
import com.ibeetl.admin.core.entity.BaseEntity;
import com.ibeetl.admin.core.enums.MenuEnums;
import org.beetl.sql.annotation.entity.AutoID;
import org.beetl.sql.fetch.annotation.Fetch;
import org.beetl.sql.fetch.annotation.FetchOne;
@ -16,7 +17,7 @@ import java.util.Date;
* gen by Spring Boot2 Admin 2021-06-19
*/
@Fetch
public class Student extends BaseEntity{
public class Student extends BaseEntity implements TokenInterface {
//ID
@NotNull(message = "请选择需要修改的学生", groups =ValidateConfig.UPDATE.class)
@ -251,4 +252,14 @@ public class Student extends BaseEntity{
public void setSchoolClass(SchoolClass schoolClass) {
this.schoolClass = schoolClass;
}
@Override
public String getId() {
return this.getStudentId().toString();
}
@Override
public MenuEnums getIdentity() {
return MenuEnums.JT_S_03;
}
}

@ -3,8 +3,8 @@ package com.ibeetl.jlw.entity;
import cn.jlw.validate.ValidateConfig;
import com.ibeetl.admin.core.annotation.Dict;
import com.ibeetl.admin.core.entity.BaseEntity;
import com.ibeetl.admin.core.enums.MenuEnums;
import org.beetl.sql.annotation.entity.AutoID;
import org.beetl.sql.annotation.entity.SeqID;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@ -14,7 +14,7 @@ import java.util.Date;
*
* gen by Spring Boot2 Admin 2021-06-19
*/
public class Teacher extends BaseEntity{
public class Teacher extends BaseEntity implements TokenInterface{
//ID
@NotNull(message = "请选择需要修改的教师", groups =ValidateConfig.UPDATE.class)
@ -233,4 +233,13 @@ public class Teacher extends BaseEntity{
return code;
}
@Override
public String getId() {
return getTeacherId().toString();
}
@Override
public MenuEnums getIdentity() {
return MenuEnums.JT_S_05;
}
}

@ -0,0 +1,31 @@
package com.ibeetl.jlw.entity;
import com.ibeetl.admin.core.enums.MenuEnums;
import java.util.Date;
/**
* Token
*
* @author mlx
*/
public interface TokenInterface {
/**
* ID
* @return
*/
String getId();
/**
*
* @return
*/
MenuEnums getIdentity();
/**
*
* @return
*/
Date getAddTime();
}

@ -643,7 +643,7 @@ public class StudentService extends CoreBaseService<Student>{
}
public List<Student> getValues (Object paras){
return sqlManager.select(SqlId.of("jlw.student.getStudentValues"),Student.class,paras);
return sqlManager.select(SqlId.of("jlw.student.getStudentValues"), Student.class, paras);
}
public List<Student> getValuesByQuery (StudentQuery studentQuery){

@ -1,6 +1,7 @@
package com.ibeetl.jlw.service;
import cn.hutool.core.util.ObjectUtil;
import cn.jlw.util.ToolUtils;
import com.ibeetl.admin.console.service.UserConsoleService;
import com.ibeetl.admin.core.dao.CoreUserRoleDao;
@ -51,7 +52,7 @@ import static com.ibeetl.jlw.web.IpAddressController.ipAddressMap;
@Service
@Transactional
public class TeacherService extends CoreBaseService<Teacher>{
public class TeacherService extends CoreBaseService<Teacher> {
@Resource private TeacherDao teacherDao;
@ -476,7 +477,7 @@ public class TeacherService extends CoreBaseService<Teacher>{
}
public List<Teacher> getValues (Object paras){
return sqlManager.select(SqlId.of("jlw.teacher.getTeacherValues"),Teacher.class,paras);
return sqlManager.select(SqlId.of("jlw.teacher.getTeacherValues"),Teacher.class, paras);
}
@ -676,4 +677,10 @@ public class TeacherService extends CoreBaseService<Teacher>{
}
public List<Teacher> getByIds(String teacherIds) {
return teacherDao.createLambdaQuery()
.andIn(Teacher::getTeacherId,
Arrays.asList(ObjectUtil.defaultIfNull(teacherIds, "").split(",")))
.select();
}
}

@ -35,6 +35,7 @@ public class WebPlatformService {
switch (EnumUtil.fromString(MenuEnums.class, user.getJobType1())) {
case JT_S_01: {
// 什么也不做, 用户在登录的时候已经把用户信息放进Session中啦
httpRequestLocal.setSessionValue(T_ADMIN, user);
}break;
case JT_S_02: {
httpRequestLocal.setSessionValue(T_TEACHER, teacherService.getByUserId(userId));
@ -55,7 +56,7 @@ public class WebPlatformService {
public Object getUserInfoFromSessionByIdentity(@NotNull(message = "登录用户不能为空!") CoreUser user) {
switch (EnumUtil.fromString(MenuEnums.class, user.getJobType1())) {
case JT_S_01: {
return httpRequestLocal.getSessionValue(ACCESS_CURRENT_USER);
return httpRequestLocal.getSessionValue(T_ADMIN);
}
case JT_S_02: {
return httpRequestLocal.getSessionValue(T_TEACHER);

@ -1,10 +1,8 @@
package com.ibeetl.jlw.web;
import cn.jlw.token.TokenService;
import com.ibeetl.jlw.entity.Quartz;
import com.ibeetl.jlw.service.AttributeService;
import com.ibeetl.jlw.service.IpAddressService;
import com.ibeetl.jlw.service.QuartzService;
import com.ibeetl.jlw.service.StudentService;
import com.ibeetl.jlw.service.*;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Controller;
@ -25,6 +23,7 @@ public class ApplicationRunnerController implements ApplicationRunner {
@Resource private IpAddressService ipAddressService;
@Resource private StudentService studentService;
@Resource private TeacherService teacherService;
public static boolean isOnline = false;
@ -46,7 +45,8 @@ public class ApplicationRunnerController implements ApplicationRunner {
try {
StudentController.init(studentService);
// 更新Token
TokenService.init();
} catch (Exception e) {
e.printStackTrace();
}

@ -178,7 +178,7 @@ public class ExamController{
Teacher teacher = new Teacher();
teacher.setUserId(coreUser.getId());
teacher = teacherService.getValues(teacher).get(0);
teacher = (Teacher) teacherService.getValues(teacher).get(0);
examQuery.setTeacherId(teacher.getTeacherId());
examQuery.setUserId(coreUser.getId());

@ -47,8 +47,8 @@ import java.util.*;
import static cn.jlw.Interceptor.AuthenticationInterceptor.getToken;
import static cn.jlw.Interceptor.AuthenticationInterceptor.indexTokenMap;
import static cn.jlw.token.TokenService.updateByIds;
import static com.ibeetl.admin.core.service.CorePlatformService.tokenSessionMap;
import static com.ibeetl.jlw.web.StudentController.updateByIds;
@Controller
public class IndexController {
@ -513,6 +513,7 @@ public class IndexController {
JsonResult jsonResult = teacherService.add(teacher);
teacher = teacherService.queryById(Long.parseLong(jsonResult.getData().toString()));
coreUser = userService.getUserById(teacher.getUserId());
}
}
}
@ -721,9 +722,29 @@ public class IndexController {
jsonResult.setMsg(msg);
if("JT_S_02".equals(user.getJobType1())){//老师
// 登录信息存放Session
webPlatformService.setUserInfoToSessionByIdentity(info.getUser());
this.platformService.setLoginUser(info.getUser(), info.getCurrentOrg(), info.getOrgs(), uSystem);
Teacher teacher = teacherService.getByUserId(user.getId());
String token = tokenService.getToken(teacher);
if(StringUtils.isNotBlank(token)){
String keyId = UUID.randomUUID().toString();
indexTokenMap.put(keyId,token);
jsonResult.setData(keyId);
//断token的尾巴
if(StringUtils.isNotBlank(token)){
if(token.split("\\.").length > 3){
String loginTime = token.split("\\.")[3];
token = token.replace("."+loginTime,"");
}
}
// 登录信息存放Session
webPlatformService.setUserInfoToSessionByIdentity(info.getUser());
tokenSessionMap.put(token,new Object[]{user, info.getCurrentOrg(), info.getOrgs(), uSystem});
return jsonResult;
}
}else if("JT_S_03".equals(user.getJobType1())){//学生
Student s = studentService.getByUserId(user.getId());
@ -828,12 +849,12 @@ public class IndexController {
if(request.getHeader("Referer").contains("/server")){
redirectUrl = request.getHeader("Referer").substring(0,request.getHeader("Referer").indexOf("/server"));
}else {
redirectUrl = request.getHeader("Referer").substring(0,request.getHeader("Referer").indexOf("/Login"));
redirectUrl = request.getHeader("Referer").substring(0,request.getHeader("Referer").lastIndexOf("/") + 1);
}
String token = getToken(request);
UniversitiesCollegesJurisdictionExperimentalSystem uSystem = (UniversitiesCollegesJurisdictionExperimentalSystem)platformService.getOther(token);
// UniversitiesCollegesJurisdictionExperimentalSystem uSystem = (UniversitiesCollegesJurisdictionExperimentalSystem)platformService.getOther(token);
try {
Cookie c1 = new Cookie("JSESSIONID", request.getSession().getId());

@ -3,7 +3,6 @@ package com.ibeetl.jlw.web;
import cn.jlw.Interceptor.*;
import cn.jlw.token.TokenService;
import cn.jlw.util.JupyterNotebookUtils;
import cn.jlw.util.SpringContextUtil;
import cn.jlw.util.ToolUtils;
import cn.jlw.validate.ValidateConfig;
import com.alibaba.fastjson.JSON;
@ -43,9 +42,10 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import static cn.jlw.token.TokenService.getMap;
import static cn.jlw.token.TokenService.updateByIds;
import static com.ibeetl.jlw.web.IpAddressController.ipAddressMap;
@ -110,29 +110,6 @@ public class StudentController{
@Resource
FileService fileService;
//静态参数
public static Map<Long,Student> studentMap = new ConcurrentHashMap<>();
//初始化
public static void init(StudentService studentService){
List<Student>studentList = studentService.getValues(null);
for (int i=0;null != studentList && i<studentList.size();i++){
studentMap.put(studentList.get(i).getStudentId(),studentList.get(i));
}
}
//更新缓存
public static void updateByIds(String studentIds){
StudentService studentService = (StudentService) SpringContextUtil.getBean("studentService");
List<Student> studentList = studentService.getByIds(studentIds);
for(int i=0;null != studentList && i<studentList.size();i++){
studentMap.put(studentList.get(i).getStudentId(),studentList.get(i));
}
}
/* 前端接口 */
/**
@ -166,7 +143,7 @@ public class StudentController{
return JsonResult.failMessage("密码错误,请重新输入或使用短信验证码登陆");
}else {
//登陆
map = getMap(s,tokenService);
map = getMap(s, tokenService);
}
}
}
@ -195,26 +172,6 @@ public class StudentController{
}
}
public static Map<String,Object> getMap(Student s,TokenService tokenService){
Student student = ToolUtils.deepCopy(s);
Map<String,Object> map = new HashMap<>();
//生成token
String token = tokenService.getToken(student);
map.put("token",token);
map.put("member",getStudentInfo(student.getStudentId()));
return map;
}
//获取会员信息简述,合并了微信信息
public static Student getStudentInfo(Long studentId){
Student student = ToolUtils.deepCopy(studentMap.get(studentId));
// TODO 添加乱七八糟的东西
return student;
}
//学生端首页所需数据
@GetMapping(API + "/indexInfo.do")

@ -1071,7 +1071,7 @@ public class TeacherController{
@GetMapping(MODEL + "/getValues.json")
@ResponseBody
public JsonResult<List<Teacher>>getValues(TeacherQuery param) {
List<Teacher>list = teacherService.getValues(param);
List<Teacher> list = teacherService.getValues(param);
return JsonResult.success(list);
}

@ -53,5 +53,5 @@ dynamic.beetlsql.cs.dbStyle = org.beetl.sql.core.db.MySqlStyle
dynamic.beetlsql.cs.dynamicConnectionSource = master,ds2
# \u6A21\u62DF\u7528\u6237\u767B\u5F55 \u53EA\u80FD\u7528\u5728\u672C\u5730\u6D4B\u8BD5\u65F6\u5019\u4F7F\u7528
user.id=1
user.orgId=1
#user.id=1
#user.orgId=1
Loading…
Cancel
Save