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.
dianshang-houduan/src/main/java/com/sztzjy/linkCommerce/controller/stu/LoginController.java

219 lines
10 KiB
Java

1 year ago
package com.sztzjy.linkCommerce.controller.stu;
import cn.hutool.core.util.IdUtil;
1 year ago
import cn.hutool.system.UserInfo;
import com.sztzjy.linkCommerce.annotation.AnonymousAccess;
import com.sztzjy.linkCommerce.config.Constant;
import com.sztzjy.linkCommerce.config.exception.UnAuthorizedException;
1 year ago
import com.sztzjy.linkCommerce.config.security.JwtUser;
import com.sztzjy.linkCommerce.config.security.LoginResult;
1 year ago
import com.sztzjy.linkCommerce.config.security.TokenProvider;
import com.sztzjy.linkCommerce.entity.SchoolClass;
import com.sztzjy.linkCommerce.entity.SchoolClassExample;
1 year ago
import com.sztzjy.linkCommerce.entity.Userinfo;
import com.sztzjy.linkCommerce.entity.UserinfoExample;
import com.sztzjy.linkCommerce.mapper.SchoolClassMapper;
1 year ago
import com.sztzjy.linkCommerce.mapper.UserinfoMapper;
import com.sztzjy.linkCommerce.service.SchoolService;
import com.sztzjy.linkCommerce.service.UserInfoService;
import com.sztzjy.linkCommerce.util.ResultDataEntity;
1 year ago
import com.sztzjy.linkCommerce.util.ResultEntity;
import com.sztzjy.linkCommerce.util.RsaUtil;
import com.sztzjy.linkCommerce.util.TzApi;
1 year ago
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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;
1 year ago
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
1 year ago
@RestController
@RequestMapping("/api/user")
@Api(tags = "yz-登录相关")
public class LoginController {
@Resource
private AuthenticationManagerBuilder authenticationManagerBuilder;
@Autowired
UserinfoMapper userinfoMapper;
@Autowired
SchoolClassMapper schoolClassMapper;
1 year ago
@PostMapping("/login")
@ApiOperation("登录接口")
@AnonymousAccess
public ResultEntity login(@RequestParam(required = false) String passwordEncode,
@RequestParam(required = false) String userName,
HttpServletRequest request,
@RequestParam(required = false) String TOKEN) {
JwtUser jwtUser;
String passWord;
if (StringUtils.isBlank(TOKEN)) {
try {
passWord = RsaUtil.decryptByPrivateKey(passwordEncode);
} catch (Exception e) {
return new ResultEntity(HttpStatus.BAD_REQUEST, "密码错误");
}
try{
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);
// 1、子系统直接登录
UserinfoExample stuUserExample = new UserinfoExample();
stuUserExample.createCriteria().andUsernameEqualTo(userName).andPasswordEqualTo(passWord);
List<Userinfo> userinfos = userinfoMapper.selectByExample(stuUserExample);
//不存在返回错误,正确放行
if (userinfos.isEmpty()) {
return new ResultEntity(HttpStatus.BAD_REQUEST, "密码错误");
}
// 保存用户的登录信息
Map<String, Object> map = new HashMap<>();
Userinfo user = userinfos.get(0);
map.put("name", user.getName());
map.put("username", user.getUsername());
map.put("roleId", String.valueOf(user.getRole()));
map.put("schoolId", user.getSchoolId());
map.put("classId", user.getSchoolClassId());
if(user.getRole()==4){
SchoolClass schoolClass = schoolClassMapper.selectByPrimaryKey(user.getSchoolClassId());
if (schoolClass == null) {
map.put("className",null);
}else {
map.put("className",schoolClass.getClassName());
}
}
1 year ago
map.put("userId", user.getUserId());
map.put("token", token);
return new ResultEntity(HttpStatus.OK, map);
}catch (Exception e){
return new ResultEntity(HttpStatus.BAD_REQUEST, "账号不存在");
}
}else {
if (!org.springframework.util.StringUtils.hasText(userName) && !org.springframework.util.StringUtils.hasText(passwordEncode) && !org.springframework.util.StringUtils.hasText(TOKEN)) {
throw new IllegalArgumentException("请提供登录凭据");
}
if (org.springframework.util.StringUtils.hasText(TOKEN)) {
jwtUser = TokenProvider.getJWTUserByZhiYun(TOKEN);
if (jwtUser == null) {
throw new UnAuthorizedException("token 无效!");
}
passWord = jwtUser.getPassword();
userName = jwtUser.getUsername();
if (StringUtils.isBlank(userName)){
userName = jwtUser.getUsername();
}
if (StringUtils.isBlank(userName)){
userName = jwtUser.getName();
}
if (!org.springframework.util.StringUtils.hasText(passWord)) {
throw new UnAuthorizedException("token 中缺少用户密码信息!");
}
try {
jwtUser = TzApi.foreignExchangeTradingLogin(userName, passWord, Constant.SYSTEMNAME);
if (jwtUser == null) {
throw new UnAuthorizedException("登录失败,用户不存在!");
}
if (jwtUser.getRoleId()==4)
{
Date authorizeTime = jwtUser.getAuthorizeTime();
Date authorizeEndTime = jwtUser.getAuthorizeEndTime();
if (authorizeTime == null || authorizeEndTime == null) {
throw new UnAuthorizedException("系统未授权!");
}
Date now = new Date();
if (now.before(authorizeTime) || now.after(authorizeEndTime)) {
throw new UnAuthorizedException("系统授权已过期!");
}
}
Authentication authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
String token = TokenProvider.createToken(jwtUser);
UserinfoExample stuUserExample = new UserinfoExample();
stuUserExample.createCriteria().andUsernameEqualTo(userName);
List<Userinfo> userinfos = userinfoMapper.selectByExample(stuUserExample);
//不存在返回错误,正确放行
if (userinfos.isEmpty()) {
return new ResultEntity(HttpStatus.BAD_REQUEST, "密码错误");
}
Map<String, Object> map = new HashMap<>();
Userinfo user = userinfos.get(0);
map.put("name", user.getName());
map.put("username", user.getUsername());
map.put("roleId", String.valueOf(user.getRole()));
map.put("schoolId", user.getSchoolId());
map.put("classId", user.getSchoolClassId());
if(user.getRole()==4){
SchoolClass schoolClass = schoolClassMapper.selectByPrimaryKey(user.getSchoolClassId());
if (schoolClass == null) {
map.put("className",null);
}else {
map.put("className",schoolClass.getClassName());
}
}
map.put("userId", user.getUserId());
map.put("token", token);
return new ResultEntity(HttpStatus.OK, map);
} catch (UnAuthorizedException e) {
throw e; // 抛出授权类异常,保留原语义
} catch (Exception e) {
// 抛出其他登录异常,比如远程接口失败、系统错误等
throw new RuntimeException("系统登录校验失败", e);
}
}
throw new RuntimeException("系统登录校验失败");
1 year ago
}
}
@PostMapping("/getClassList")
@ApiOperation("获取班级列表")
@AnonymousAccess
public Map<String,String> getClassList(@RequestParam String schoolMajorId){
Map<String,String> map=new HashMap<>();
SchoolClassExample schoolClassExample = new SchoolClassExample();
schoolClassExample.createCriteria().andSchoolMajorIdEqualTo(schoolMajorId);
List<SchoolClass> schoolClasses = schoolClassMapper.selectByExample(schoolClassExample);
for (int i = 0; i < schoolClasses.size(); i++) {
SchoolClass schoolClass = schoolClasses.get(i);
map.put(schoolClass.getSchoolClassId(),schoolClass.getClassName());
}
return map;
}
1 year ago
}