You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
339 lines
15 KiB
Java
339 lines
15 KiB
Java
|
1 year ago
|
package com.sztzjy.linkCommerce.controller.stu;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
import cn.hutool.core.convert.Convert;
|
||
|
|
import cn.hutool.core.util.IdUtil;
|
||
|
|
import com.sztzjy.linkCommerce.annotation.AnonymousAccess;
|
||
|
|
import com.sztzjy.linkCommerce.annotation.OperateLog;
|
||
|
|
import com.sztzjy.linkCommerce.config.exception.UnAuthorizedException;
|
||
|
|
import com.sztzjy.linkCommerce.config.security.JwtUser;
|
||
|
|
import com.sztzjy.linkCommerce.config.security.LoginResult;
|
||
|
|
import com.sztzjy.linkCommerce.config.security.TokenProvider;
|
||
|
|
import com.sztzjy.linkCommerce.entity.StuUser;
|
||
|
|
import com.sztzjy.linkCommerce.entity.StuUserExample;
|
||
|
|
import com.sztzjy.linkCommerce.entity.ZYUserInfo;
|
||
|
|
import com.sztzjy.linkCommerce.mapper.StuUserMapper;
|
||
|
|
import com.sztzjy.linkCommerce.service.StuUserService;
|
||
|
|
import com.sztzjy.linkCommerce.util.*;
|
||
|
|
import io.swagger.annotations.Api;
|
||
|
|
import io.swagger.annotations.ApiOperation;
|
||
|
|
import io.swagger.annotations.ApiParam;
|
||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
|
import org.springframework.beans.factory.annotation.Value;
|
||
|
|
import org.springframework.http.HttpStatus;
|
||
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||
|
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||
|
|
import org.springframework.security.core.Authentication;
|
||
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||
|
|
import org.springframework.util.CollectionUtils;
|
||
|
|
import org.springframework.util.StringUtils;
|
||
|
|
import org.springframework.web.bind.annotation.*;
|
||
|
|
|
||
|
|
|
||
|
|
import javax.annotation.Resource;
|
||
|
|
import javax.servlet.http.HttpServletRequest;
|
||
|
|
import javax.servlet.http.HttpServletResponse;
|
||
|
|
import java.io.IOException;
|
||
|
|
import java.util.*;
|
||
|
|
|
||
|
|
|
||
|
|
@Api(tags = "用户管理")
|
||
|
|
@RequestMapping("api/user")
|
||
|
|
@RestController
|
||
|
|
public class UserController {
|
||
|
|
|
||
|
|
|
||
|
|
@Resource
|
||
|
|
private StuUserService stuUserService;
|
||
|
|
@Resource
|
||
|
|
private HttpServletRequest request;
|
||
|
|
@Autowired
|
||
|
|
private StuUserMapper userMapper;
|
||
|
|
@Resource
|
||
|
|
private AuthenticationManagerBuilder authenticationManagerBuilder;
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private RedisUtil redisUtil;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
@Value("${item.name}")
|
||
|
|
String itemName;
|
||
|
|
@Autowired
|
||
|
|
private StuUserMapper stuUserMapper;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
@AnonymousAccess
|
||
|
|
@ApiOperation("用户登录")
|
||
|
|
@PostMapping("login")
|
||
|
|
@OperateLog(description = "登录授权")
|
||
|
|
public ResultDataEntity<LoginResult> login(@ApiParam("用户名") String username,
|
||
|
|
@ApiParam("加密后的密码") String passwordEncode,
|
||
|
|
@ApiParam("智云携带的token")@RequestParam(required = false) String TOKEN,
|
||
|
|
@RequestParam(required = false) Integer type,
|
||
|
|
HttpServletResponse response) throws IOException {
|
||
|
|
JwtUser jwtUser;
|
||
|
|
String password;
|
||
|
|
|
||
|
|
if(org.apache.commons.lang3.StringUtils.isBlank(TOKEN)){ //子系统登录
|
||
|
|
try {
|
||
|
|
password = RsaUtil.decryptByPrivateKey(passwordEncode);
|
||
|
|
} catch (Exception e) {
|
||
|
|
return new ResultDataEntity(HttpStatus.BAD_REQUEST, "密码错误");
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
StuUserExample example = new StuUserExample();
|
||
|
|
StuUserExample.Criteria criteria = example.createCriteria();
|
||
|
|
criteria.andStudentIdEqualTo(username);
|
||
|
|
List<StuUser> stuUsers = userMapper.selectByExample(example);
|
||
|
|
if(stuUsers.isEmpty()){
|
||
|
|
throw new UnAuthorizedException("账号不存在");
|
||
|
|
}else {
|
||
|
|
StuUser stuUser = stuUsers.get(0);
|
||
|
|
LoginResult loginResult = new LoginResult();
|
||
|
|
if(org.apache.commons.lang3.StringUtils.isBlank(stuUser.getPassword()) && "123qwe".equals(password)){
|
||
|
|
stuUser.setPassword("123qwe");
|
||
|
|
if(org.apache.commons.lang3.StringUtils.isBlank(stuUser.getClassId())){
|
||
|
|
stuUser.setRoleId(3);
|
||
|
|
}else {
|
||
|
|
stuUser.setRoleId(4);
|
||
|
|
}
|
||
|
|
userMapper.updateByPrimaryKeySelective(stuUser);
|
||
|
|
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
|
||
|
|
Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
|
||
|
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||
|
|
jwtUser = (JwtUser) authentication.getPrincipal();
|
||
|
|
String token = TokenProvider.createToken(jwtUser);
|
||
|
|
loginResult.setClassId(Convert.toInt(stuUser.getClassId()));
|
||
|
|
loginResult.setUserId(stuUser.getUserId());
|
||
|
|
loginResult.setUsername(stuUser.getStudentId());
|
||
|
|
loginResult.setName(stuUser.getName());
|
||
|
|
loginResult.setRoleId(stuUser.getRoleId());
|
||
|
|
loginResult.setSchoolName(stuUser.getSchoolName());
|
||
|
|
loginResult.setSchoolId(Integer.valueOf(stuUser.getSchoolId()));
|
||
|
|
loginResult.setAccessToken(token);
|
||
|
|
loginResult.setStudentId(stuUser.getStudentId());
|
||
|
|
// redisUtil.set(itemName+"-token:"+jwtUser.getUserId(),token,43200);
|
||
|
|
//
|
||
|
|
// //redisUtil.set(itemName+"-userId:"+jwtUser.getUserId(),System.currentTimeMillis(),36000);
|
||
|
|
// // 设置 loginTime
|
||
|
|
// Long newLoginTime = System.currentTimeMillis();
|
||
|
|
// Long newLastActiveTime = System.currentTimeMillis();
|
||
|
|
// redisUtil.hset(itemName+"-userId:"+jwtUser.getUserId(), "loginTime", newLoginTime,43200);
|
||
|
|
//
|
||
|
|
//
|
||
|
|
// // 设置 lastActiveTime
|
||
|
|
// redisUtil.hset(itemName+"-userId:"+jwtUser.getUserId(), "lastActiveTime", newLastActiveTime,43200);
|
||
|
|
|
||
|
|
|
||
|
|
return new ResultDataEntity<>(HttpStatus.OK,loginResult);
|
||
|
|
}else {
|
||
|
|
if(password.equals(stuUser.getPassword())){
|
||
|
|
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
|
||
|
|
Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
|
||
|
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||
|
|
jwtUser = (JwtUser) authentication.getPrincipal();
|
||
|
|
String token = TokenProvider.createToken(jwtUser);
|
||
|
|
loginResult.setClassId(Convert.toInt(stuUser.getClassId()));
|
||
|
|
loginResult.setUserId(stuUser.getUserId());
|
||
|
|
loginResult.setName(stuUser.getName());
|
||
|
|
loginResult.setUsername(stuUser.getStudentId());
|
||
|
|
loginResult.setRoleId(stuUser.getRoleId());
|
||
|
|
loginResult.setSchoolId(Integer.valueOf(stuUser.getSchoolId()));
|
||
|
|
loginResult.setStudentId(stuUser.getStudentId());
|
||
|
|
loginResult.setAccessToken(token);
|
||
|
|
loginResult.setSchoolName(stuUser.getSchoolName());
|
||
|
|
// redisUtil.set(itemName+"-token:"+jwtUser.getUserId(),token,43200);
|
||
|
|
// // redisUtil.set(itemName+"-userId:"+jwtUser.getUserId(),System.currentTimeMillis(),3600);
|
||
|
|
//
|
||
|
|
// Long newLoginTime = System.currentTimeMillis();
|
||
|
|
// Long newLastActiveTime = System.currentTimeMillis();
|
||
|
|
//
|
||
|
|
// // 设置 loginTime
|
||
|
|
// redisUtil.hset(itemName+"-userId:"+jwtUser.getUserId(), "loginTime", newLoginTime,43200);
|
||
|
|
//
|
||
|
|
//
|
||
|
|
// // 设置 lastActiveTime
|
||
|
|
// redisUtil.hset(itemName+"-userId:"+jwtUser.getUserId(), "lastActiveTime", newLastActiveTime,43200);
|
||
|
|
|
||
|
|
return new ResultDataEntity<>(HttpStatus.OK,loginResult);
|
||
|
|
}else {
|
||
|
|
throw new UnAuthorizedException("密码错误");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
throw new UnAuthorizedException("密码错误");
|
||
|
|
}
|
||
|
|
}else { //单点登录
|
||
|
|
if (!StringUtils.hasText(username) && !StringUtils.hasText(passwordEncode) && !StringUtils.hasText(TOKEN)) {
|
||
|
|
throw new IllegalArgumentException("请提供登录凭据");
|
||
|
|
}
|
||
|
|
if (StringUtils.hasText(TOKEN)) {
|
||
|
|
|
||
|
|
if (type==null)
|
||
|
|
{
|
||
|
|
type = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (type ==1 )
|
||
|
|
{
|
||
|
|
jwtUser = TokenProvider.getJWTUserOne(TOKEN);
|
||
|
|
}else {
|
||
|
|
jwtUser = TokenProvider.getJWTUserByZhiYun(TOKEN);
|
||
|
|
}
|
||
|
|
|
||
|
|
// jwtUser = TokenProvider.getJWTUserByZhiYun(TOKEN);
|
||
|
|
|
||
|
|
if (jwtUser == null) {
|
||
|
|
throw new UnAuthorizedException("token无效");
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
try {
|
||
|
|
password = RsaUtil.decryptByPrivateKey(passwordEncode);
|
||
|
|
} catch (Exception e) {
|
||
|
|
throw new UnAuthorizedException("密码错误");
|
||
|
|
}
|
||
|
|
|
||
|
|
jwtUser = TzApi.foreignExchangeTradingLogin(username, password);
|
||
|
|
if (jwtUser == null) {
|
||
|
|
throw new UnAuthorizedException("用户名或密码错误");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
String studentId = jwtUser.getStudentId();
|
||
|
|
if (!StringUtils.hasText(studentId)) {
|
||
|
|
studentId= jwtUser.getUsername();
|
||
|
|
if (!StringUtils.hasText(studentId)) {
|
||
|
|
studentId= jwtUser.getName();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
//根据StudentId查询用户数据 并且赋予userId
|
||
|
|
StuUserExample stuUserExample = new StuUserExample();
|
||
|
|
stuUserExample.createCriteria().andStudentIdEqualTo(studentId);
|
||
|
|
List<StuUser> stuUserList = stuUserMapper.selectByExample(stuUserExample);
|
||
|
|
String userId = null;
|
||
|
|
if (CollectionUtils.isEmpty(stuUserList)) {
|
||
|
|
StuUser student = new StuUser();
|
||
|
|
userId = IdUtil.randomUUID();
|
||
|
|
|
||
|
|
student.setUserId(userId);
|
||
|
|
student.setStudentId(studentId);
|
||
|
|
if (StringUtils.hasText(jwtUser.getName())){
|
||
|
|
student.setName(jwtUser.getName());
|
||
|
|
}else {
|
||
|
|
student.setName(studentId);
|
||
|
|
}
|
||
|
|
student.setPassword(jwtUser.getPassword());
|
||
|
|
student.setRoleId(jwtUser.getRoleId());
|
||
|
|
student.setClassId(String.valueOf(jwtUser.getClassId()));
|
||
|
|
student.setClassName(jwtUser.getClassName());
|
||
|
|
student.setMajor(jwtUser.getMajorName());
|
||
|
|
student.setSchoolId(String.valueOf(jwtUser.getSchoolId()));
|
||
|
|
student.setSchoolName(jwtUser.getSchoolName());
|
||
|
|
stuUserMapper.insertSelective(student);
|
||
|
|
}
|
||
|
|
if (StringUtils.hasText(userId)){
|
||
|
|
jwtUser.setUserId(userId);
|
||
|
|
}else {
|
||
|
|
jwtUser.setUserId(stuUserList.get(0).getUserId());
|
||
|
|
}
|
||
|
|
|
||
|
|
Authentication authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities());
|
||
|
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||
|
|
String token = TokenProvider.createToken(jwtUser);
|
||
|
|
//
|
||
|
|
// redisUtil.set(itemName+"-token:"+jwtUser.getUserId(),token,43200);
|
||
|
|
//
|
||
|
|
// // redisUtil.set(itemName+"-userId:"+jwtUser.getUserId(),System.currentTimeMillis(),3600);
|
||
|
|
//
|
||
|
|
// Long newLoginTime = System.currentTimeMillis();
|
||
|
|
// Long newLastActiveTime = System.currentTimeMillis();
|
||
|
|
//
|
||
|
|
// // 设置 loginTime
|
||
|
|
// redisUtil.hset(itemName+"-userId:"+jwtUser.getUserId(), "loginTime", newLoginTime,43200);
|
||
|
|
//
|
||
|
|
// // 设置 lastActiveTime
|
||
|
|
// redisUtil.hset(itemName+"-userId:"+jwtUser.getUserId(), "lastActiveTime", newLastActiveTime,43200);
|
||
|
|
|
||
|
|
return new ResultDataEntity<LoginResult>(HttpStatus.OK,LoginResult.create(jwtUser, token));
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
@ApiOperation("明文密码公钥加密方法(该方法仅在开发阶段开放用于测试接口)")
|
||
|
|
@AnonymousAccess
|
||
|
|
@GetMapping("encrypt")
|
||
|
|
public ResultDataEntity<String> encrypt(@ApiParam("明文密码") String pwd) throws Exception {
|
||
|
|
return new ResultDataEntity<>(HttpStatus.OK,"加密成功",RsaUtil.encryptByPublicKey(pwd));
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
@ApiOperation("同步学生信息")
|
||
|
|
@PostMapping("synchronizationStudentInfo")
|
||
|
|
public ResultDataEntity synchronizationStudentInfo() {
|
||
|
|
JwtUser user = TokenProvider.getJWTUser(request);
|
||
|
|
stuUserService.insertAll(user);
|
||
|
|
return new ResultDataEntity(HttpStatus.OK);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
@ApiOperation("开放接口,自动同步学生信息")
|
||
|
|
@PostMapping("synchronizationStudentInfoByAuto")
|
||
|
|
@AnonymousAccess
|
||
|
|
public String synchronizationStudentInfoByAuto(@RequestBody List<ZYUserInfo> zyUserInfoList) {
|
||
|
|
// JwtUser user = TokenProvider.getJWTUser(request);
|
||
|
|
// stuUserService.insertAllByAuto(studentList);
|
||
|
|
List<StuUser> users = new ArrayList<>();
|
||
|
|
for (int i = 0; i < zyUserInfoList.size(); i++) {
|
||
|
|
ZYUserInfo zyUserInfo = zyUserInfoList.get(i);
|
||
|
|
String username = zyUserInfo.getUsername(); // studentid
|
||
|
|
String name = zyUserInfo.getName();
|
||
|
|
String password = zyUserInfo.getPassword();
|
||
|
|
Integer roleId = zyUserInfo.getRoleId(); //3教师 4学生
|
||
|
|
|
||
|
|
StuUserExample userInfoExample = new StuUserExample();
|
||
|
|
userInfoExample.createCriteria().andStudentIdEqualTo(username);
|
||
|
|
List<StuUser> userInfos = stuUserMapper.selectByExample(userInfoExample);
|
||
|
|
if(userInfos.isEmpty() || userInfos==null){
|
||
|
|
StuUser stuUser = new StuUser();
|
||
|
|
stuUser.setUserId(IdUtil.randomUUID());
|
||
|
|
stuUser.setStudentId(zyUserInfo.getUsername());
|
||
|
|
if (!StringUtils.hasText(name)){
|
||
|
|
stuUser.setName(username);
|
||
|
|
}else {
|
||
|
|
stuUser.setName(name);
|
||
|
|
}
|
||
|
|
// stuUser.setUsername(username);
|
||
|
|
stuUser.setPassword(password);
|
||
|
|
stuUser.setRoleId(roleId);
|
||
|
|
stuUser.setClassId(zyUserInfo.getClassId().toString());
|
||
|
|
// stuUser.setPhone(zyUserInfo.getPhone());
|
||
|
|
stuUser.setClassName(zyUserInfo.getClassName());
|
||
|
|
stuUser.setMajor(zyUserInfo.getMajorName());
|
||
|
|
stuUser.setSchoolId(zyUserInfo.getSchoolId().toString());
|
||
|
|
stuUser.setSchoolName(zyUserInfo.getSchoolName());
|
||
|
|
stuUser.setCreateTime(new Date());
|
||
|
|
users.add(stuUser);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (users.isEmpty()) {
|
||
|
|
return "账号已全部存在";
|
||
|
|
}
|
||
|
|
users.forEach(stuUser -> {
|
||
|
|
stuUserMapper.insertSelective(stuUser);
|
||
|
|
});
|
||
|
|
return "自动同步学生信息成功!"+"同步"+users.size()+"条用户数据";
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
}
|