|
@@ -6,30 +6,36 @@ import com.qmth.boot.api.annotation.BOOL;
|
|
import com.qmth.boot.api.constant.ApiConstant;
|
|
import com.qmth.boot.api.constant.ApiConstant;
|
|
import com.qmth.teachcloud.common.SignatureEntityTest;
|
|
import com.qmth.teachcloud.common.SignatureEntityTest;
|
|
import com.qmth.teachcloud.common.bean.dto.UserAuthenticationDto;
|
|
import com.qmth.teachcloud.common.bean.dto.UserAuthenticationDto;
|
|
|
|
+import com.qmth.teachcloud.common.bean.result.LoginResult;
|
|
import com.qmth.teachcloud.common.config.DictionaryConfig;
|
|
import com.qmth.teachcloud.common.config.DictionaryConfig;
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
import com.qmth.teachcloud.common.entity.*;
|
|
import com.qmth.teachcloud.common.entity.*;
|
|
|
|
+import com.qmth.teachcloud.common.enums.AppSourceEnum;
|
|
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
import com.qmth.teachcloud.common.service.*;
|
|
import com.qmth.teachcloud.common.service.*;
|
|
import com.qmth.teachcloud.common.util.AuthThirdUtil;
|
|
import com.qmth.teachcloud.common.util.AuthThirdUtil;
|
|
import com.qmth.teachcloud.common.util.JacksonUtil;
|
|
import com.qmth.teachcloud.common.util.JacksonUtil;
|
|
|
|
+import com.qmth.teachcloud.common.util.Result;
|
|
|
|
+import com.qmth.teachcloud.common.util.ResultUtil;
|
|
import com.qmth.teachcloud.report.business.bean.params.LoginThirdParam;
|
|
import com.qmth.teachcloud.report.business.bean.params.LoginThirdParam;
|
|
-import com.qmth.teachcloud.report.business.cache.WhuUserAuthCacheUtil;
|
|
|
|
|
|
+import com.qmth.teachcloud.report.business.cache.ThirdUserAuthCacheUtil;
|
|
|
|
+import com.qmth.teachcloud.report.business.service.ReportCommonService;
|
|
import io.swagger.annotations.*;
|
|
import io.swagger.annotations.*;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.validation.annotation.Validated;
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
-import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
import java.net.URLDecoder;
|
|
import java.net.URLDecoder;
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
import java.text.MessageFormat;
|
|
import java.text.MessageFormat;
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
@@ -64,10 +70,17 @@ public class SsoApiController {
|
|
@Resource
|
|
@Resource
|
|
SysRoleService sysRoleService;
|
|
SysRoleService sysRoleService;
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
+ TeachcloudCommonService teachcloudCommonService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ ReportCommonService reportCommonService;
|
|
|
|
+
|
|
@ApiOperation(value = "单点登录接口")
|
|
@ApiOperation(value = "单点登录接口")
|
|
@ApiResponses({@ApiResponse(code = 200, message = "单点登录接口", response = Object.class)})
|
|
@ApiResponses({@ApiResponse(code = 200, message = "单点登录接口", response = Object.class)})
|
|
@RequestMapping(value = "/login", method = RequestMethod.POST)
|
|
@RequestMapping(value = "/login", method = RequestMethod.POST)
|
|
@Aac(auth = BOOL.FALSE)
|
|
@Aac(auth = BOOL.FALSE)
|
|
|
|
+ @Transactional
|
|
public void login(@ApiParam(value = "接收登录数据信息", required = true) @RequestBody String result, HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
public void login(@ApiParam(value = "接收登录数据信息", required = true) @RequestBody String result, HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
Optional.ofNullable(result).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("数据为空"));
|
|
Optional.ofNullable(result).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("数据为空"));
|
|
String decodeJson = URLDecoder.decode(result, SystemConstant.CHARSET_NAME);
|
|
String decodeJson = URLDecoder.decode(result, SystemConstant.CHARSET_NAME);
|
|
@@ -84,38 +97,83 @@ public class SsoApiController {
|
|
.eq(SysUser::getEnable, true);
|
|
.eq(SysUser::getEnable, true);
|
|
SysUser sysUser = sysUserService.getOne(sysUserQueryWrapper);
|
|
SysUser sysUser = sysUserService.getOne(sysUserQueryWrapper);
|
|
if (Objects.isNull(sysUser)) {//新增
|
|
if (Objects.isNull(sysUser)) {//新增
|
|
- //查询学院id
|
|
|
|
- QueryWrapper<SysOrg> sysOrgQueryWrapper = new QueryWrapper<>();
|
|
|
|
- sysOrgQueryWrapper.lambda().eq(SysOrg::getSchoolId, basicSchool.getId())
|
|
|
|
- .eq(SysOrg::getName, loginThirdParam.getOrgName())
|
|
|
|
- .eq(SysOrg::getEnable, true);
|
|
|
|
- SysOrg sysOrg = sysOrgService.getOne(sysOrgQueryWrapper);
|
|
|
|
- Optional.ofNullable(sysOrg).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("学院数据为空"));
|
|
|
|
-
|
|
|
|
sysUser = new SysUser(basicSchool.getId(),
|
|
sysUser = new SysUser(basicSchool.getId(),
|
|
loginThirdParam.getLoginName(),
|
|
loginThirdParam.getLoginName(),
|
|
Objects.nonNull(loginThirdParam.getRealName()) ? loginThirdParam.getRealName() : loginThirdParam.getLoginName(),
|
|
Objects.nonNull(loginThirdParam.getRealName()) ? loginThirdParam.getRealName() : loginThirdParam.getLoginName(),
|
|
- Objects.nonNull(loginThirdParam.getMobileNumber()) ? loginThirdParam.getMobileNumber() : null,
|
|
|
|
- sysOrg.getId());
|
|
|
|
- sysUserService.save(sysUser);
|
|
|
|
-
|
|
|
|
- //新增用户和角色关系
|
|
|
|
- QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();
|
|
|
|
- sysRoleQueryWrapper.lambda().eq(SysRole::getType, loginThirdParam.getRole())
|
|
|
|
- .eq(SysRole::getEnable, true);
|
|
|
|
- SysRole sysRole = sysRoleService.getOne(sysRoleQueryWrapper);
|
|
|
|
- Optional.ofNullable(sysRole).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("角色数据为空"));
|
|
|
|
-
|
|
|
|
- SysUserRole sysUserRole = new SysUserRole(sysUser.getId(), sysRole.getId());
|
|
|
|
- sysUserRoleService.save(sysUserRole);
|
|
|
|
|
|
+ Objects.nonNull(loginThirdParam.getMobileNumber()) ? loginThirdParam.getMobileNumber() : null);
|
|
|
|
+ } else {
|
|
|
|
+ sysUser.setSchoolId(basicSchool.getId());
|
|
|
|
+ if (Objects.nonNull(loginThirdParam.getRealName())) {
|
|
|
|
+ sysUser.setRealName(loginThirdParam.getRealName());
|
|
|
|
+ }
|
|
|
|
+ if (Objects.nonNull(loginThirdParam.getMobileNumber())) {
|
|
|
|
+ sysUser.setMobileNumber(loginThirdParam.getMobileNumber());
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ //查询学院id
|
|
|
|
+ QueryWrapper<SysOrg> sysOrgQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ sysOrgQueryWrapper.lambda().eq(SysOrg::getSchoolId, basicSchool.getId())
|
|
|
|
+ .eq(SysOrg::getName, loginThirdParam.getOrgName())
|
|
|
|
+ .eq(SysOrg::getEnable, true);
|
|
|
|
+ SysOrg sysOrg = sysOrgService.getOne(sysOrgQueryWrapper);
|
|
|
|
+ Optional.ofNullable(sysOrg).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("学院数据为空"));
|
|
|
|
+ sysUser.setOrgId(sysOrg.getId());
|
|
|
|
+ sysUserService.saveOrUpdate(sysUser);
|
|
|
|
+
|
|
|
|
+ //新增用户和角色关系
|
|
|
|
+ QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ sysRoleQueryWrapper.lambda().eq(SysRole::getType, loginThirdParam.getRole())
|
|
|
|
+ .eq(SysRole::getEnable, true);
|
|
|
|
+ SysRole sysRole = sysRoleService.getOne(sysRoleQueryWrapper);
|
|
|
|
+ Optional.ofNullable(sysRole).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("角色数据为空"));
|
|
|
|
+
|
|
|
|
+ SysUserRole sysUserRole = new SysUserRole(sysUser.getId(), sysRole.getId());
|
|
|
|
+ sysUserRoleService.save(sysUserRole);
|
|
|
|
|
|
UserAuthenticationDto userAuthenticationDto = new UserAuthenticationDto(String.valueOf(sysUser.getId()), SystemConstant.getUuid());
|
|
UserAuthenticationDto userAuthenticationDto = new UserAuthenticationDto(String.valueOf(sysUser.getId()), SystemConstant.getUuid());
|
|
- WhuUserAuthCacheUtil.setAuthCode(userAuthenticationDto);
|
|
|
|
|
|
+ ThirdUserAuthCacheUtil.setAuthCode(userAuthenticationDto);
|
|
String pattern = "{0}{1}{2}";
|
|
String pattern = "{0}{1}{2}";
|
|
String code = URLEncoder.encode(MessageFormat.format(pattern, userAuthenticationDto.getUid(), SignatureEntityTest.FIELD_JOINER, SignatureEntityTest.encrypt(userAuthenticationDto.getCode())), SystemConstant.CHARSET_NAME);
|
|
String code = URLEncoder.encode(MessageFormat.format(pattern, userAuthenticationDto.getUid(), SignatureEntityTest.FIELD_JOINER, SignatureEntityTest.encrypt(userAuthenticationDto.getCode())), SystemConstant.CHARSET_NAME);
|
|
|
|
|
|
|
|
+ ThirdUserAuthCacheUtil.setAuthReturnUrl(String.valueOf(sysUser.getId()), loginThirdParam.getReturnUrl());
|
|
response.setHeader("Access-Control-Allow-Origin", "*");
|
|
response.setHeader("Access-Control-Allow-Origin", "*");
|
|
response.sendRedirect(dictionaryConfig.sysDomain().getLoginAuthenUrl() + code);
|
|
response.sendRedirect(dictionaryConfig.sysDomain().getLoginAuthenUrl() + code);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @ApiOperation(value = "用户临时授权登录接口")
|
|
|
|
+ @RequestMapping(value = "/user/authentication/login", method = RequestMethod.POST)
|
|
|
|
+ @ApiResponses({@ApiResponse(code = 200, message = "返回消息", response = LoginResult.class)})
|
|
|
|
+ @Aac(auth = BOOL.FALSE)
|
|
|
|
+ public Result login(@ApiParam(value = "授权码信息", required = true) @RequestParam String code) throws NoSuchAlgorithmException, UnsupportedEncodingException {
|
|
|
|
+ String[] values = StringUtils.split(URLDecoder.decode(code, SystemConstant.CHARSET_NAME), SignatureEntityTest.FIELD_JOINER);
|
|
|
|
+ if (Objects.isNull(values) || values.length != 2) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("临时授权码出错,请重新获取");
|
|
|
|
+ }
|
|
|
|
+ log.info("values:{}", JacksonUtil.parseJson(values));
|
|
|
|
+ SysUser sysUser = sysUserService.getById(Long.parseLong(values[0]));
|
|
|
|
+ //用户不存在
|
|
|
|
+ if (Objects.isNull(sysUser)) {
|
|
|
|
+ throw ExceptionResultEnum.USER_NO_DATA.exception();
|
|
|
|
+ }
|
|
|
|
+ //停用
|
|
|
|
+ if (!sysUser.getEnable()) {
|
|
|
|
+ throw ExceptionResultEnum.USER_ENABLE.exception();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String userAuthCode = ThirdUserAuthCacheUtil.getAuthCode(values[0]);
|
|
|
|
+ if (Objects.isNull(userAuthCode) || !Objects.equals(values[1], URLDecoder.decode(SignatureEntityTest.encrypt(userAuthCode), SystemConstant.CHARSET_NAME))) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("临时授权码已过期");
|
|
|
|
+ }
|
|
|
|
+ LoginResult loginResult = teachcloudCommonService.login(sysUser.getPassword(), sysUser, AppSourceEnum.PRINT_THIRD);
|
|
|
|
+ ThirdUserAuthCacheUtil.deleteAuthCode(values[0]);
|
|
|
|
+ return ResultUtil.ok(loginResult);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @ApiOperation(value = "用户鉴权退出接口")
|
|
|
|
+ @RequestMapping(value = "/user/authentication/logout", method = RequestMethod.GET)
|
|
|
|
+ @ApiResponses({@ApiResponse(code = 200, message = "返回消息", response = Result.class)})
|
|
|
|
+ @Aac(auth = BOOL.FALSE)
|
|
|
|
+ public void authenticationLogout() throws IOException {
|
|
|
|
+ reportCommonService.thirdLogout();
|
|
|
|
+ }
|
|
}
|
|
}
|