|
@@ -1,37 +1,29 @@
|
|
|
-package com.qmth.xjtu.api;
|
|
|
+package com.qmth.teachcloud.cas.util;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
-import com.qmth.boot.api.annotation.Aac;
|
|
|
-import com.qmth.boot.api.annotation.BOOL;
|
|
|
-import com.qmth.boot.api.constant.ApiConstant;
|
|
|
+import com.qmth.boot.core.cache.service.CacheService;
|
|
|
import com.qmth.boot.tools.signature.SignatureEntity;
|
|
|
import com.qmth.boot.tools.signature.SignatureType;
|
|
|
import com.qmth.teachcloud.exchange.common.bean.params.OpenParams;
|
|
|
+import com.qmth.teachcloud.exchange.common.bean.params.RedirectParams;
|
|
|
import com.qmth.teachcloud.exchange.common.contant.SystemConstant;
|
|
|
import com.qmth.teachcloud.exchange.common.entity.BasicSchool;
|
|
|
import com.qmth.teachcloud.exchange.common.enums.ExceptionResultEnum;
|
|
|
import com.qmth.teachcloud.exchange.common.service.AuthInfoService;
|
|
|
import com.qmth.teachcloud.exchange.common.service.BasicSchoolService;
|
|
|
-import com.qmth.teachcloud.exchange.common.service.CommonService;
|
|
|
import com.qmth.teachcloud.exchange.common.util.HttpUtil;
|
|
|
import com.qmth.teachcloud.exchange.common.util.JacksonUtil;
|
|
|
import com.qmth.teachcloud.exchange.common.util.Result;
|
|
|
import com.qmth.teachcloud.exchange.common.util.ServletUtil;
|
|
|
-import io.swagger.annotations.*;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
-import org.springframework.validation.annotation.Validated;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
-import org.springframework.web.bind.annotation.RequestParam;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
@@ -39,59 +31,53 @@ import java.io.IOException;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
- * <p>
|
|
|
- * 西安交通大学开放接口前端控制器
|
|
|
- * </p>
|
|
|
- *
|
|
|
- * @author wangliang
|
|
|
- * @since 2022-04-26
|
|
|
+ * @Description: 西交大cas工具类
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: wangliang
|
|
|
+ * @Date: 2023/6/12
|
|
|
*/
|
|
|
-@Api(tags = "西安交通大学开放接口Controller")
|
|
|
-@RestController
|
|
|
-@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_OPEN)
|
|
|
-@Validated
|
|
|
-public class OpenApiController {
|
|
|
- private static final Logger log = LoggerFactory.getLogger(OpenApiController.class);
|
|
|
+@Component
|
|
|
+public class XjtuCasUtil {
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(XjtuCasUtil.class);
|
|
|
+ private String teachcloudLoginUrl = null, logoutUrl = null, returnUrl = null;
|
|
|
+ private static final String LOGIN_BEFORE_XJU_LOGIC_API = "/api/admin/print/open/login_before_xju_logic";//西交大登录之前逻辑
|
|
|
private static final String ACCESS_TOKEN_URL = "http://org.xjtu.edu.cn/openplatform/oauth/getAccessToken";
|
|
|
private static final String USER_INFO_URL = "http://org.xjtu.edu.cn/openplatform/oauth/open/getUserInfo";
|
|
|
private static final String LOGOUT_URL = "http://org.xjtu.edu.cn/openplatform/oauth/logout";
|
|
|
- private static final String SCHOOL_CODE = "xjtu";//测试学校code,正式改成xjtu
|
|
|
- private static final String LOGIN_BEFORE_XJU_LOGIC_API = "/api/admin/print/open/login_before_xju_logic";//西交大登录之前逻辑
|
|
|
- private static final String VERSION = "1.0.1.1";
|
|
|
-
|
|
|
- @Resource
|
|
|
- CommonService commonService;
|
|
|
|
|
|
@Resource
|
|
|
AuthInfoService authInfoService;
|
|
|
|
|
|
- @Value("${cas.config.logoutUrl}")
|
|
|
- String logoutUrl;
|
|
|
-
|
|
|
- @Value("${cas.config.returnUrl}")
|
|
|
- String returnUrl;
|
|
|
-
|
|
|
- @Value("${cas.config.teachcloudPrintLoginUrl}")
|
|
|
- String teachcloudLoginUrl;
|
|
|
+ @Resource
|
|
|
+ CacheService cacheService;
|
|
|
|
|
|
@Resource
|
|
|
BasicSchoolService basicSchoolService;
|
|
|
|
|
|
- @ApiOperation(value = "西安交通大学cas鉴权接口")
|
|
|
- @ApiResponses({@ApiResponse(code = 200, message = "返回消息", response = Result.class)})
|
|
|
- @RequestMapping(value = "/authentication", method = RequestMethod.GET)
|
|
|
- @Aac(auth = BOOL.FALSE)
|
|
|
- public void authentication(@ApiParam(value = "工号") @RequestParam(required = false) String code,
|
|
|
- @ApiParam(value = "系统参数") @RequestParam(required = false) String state,
|
|
|
- @ApiParam(value = "用户类型") @RequestParam(required = false) String userType,
|
|
|
- @ApiParam(value = "员工工号") @RequestParam(required = false) String employeeNo,
|
|
|
- @ApiParam(value = "返回url") @RequestParam(required = false) String returnUrl) throws IOException {
|
|
|
- log.info("version:{}", VERSION);
|
|
|
+ /**
|
|
|
+ * 西交大登录逻辑
|
|
|
+ *
|
|
|
+ * @param schoolCode
|
|
|
+ * @param code
|
|
|
+ * @param state
|
|
|
+ * @param userType
|
|
|
+ * @param employeeNo
|
|
|
+ * @param returnUrl
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public RedirectParams authentication(String schoolCode,
|
|
|
+ String code,
|
|
|
+ String state,
|
|
|
+ String userType,
|
|
|
+ String employeeNo,
|
|
|
+ String returnUrl) throws IOException {
|
|
|
if ((Objects.isNull(code) || Objects.equals(code, ""))
|
|
|
|| (Objects.isNull(employeeNo) || Objects.equals(employeeNo, ""))) {
|
|
|
throw ExceptionResultEnum.ERROR.exception("请先通过学校地址登录");
|
|
|
}
|
|
|
- authInfoService.appHasExpired(SCHOOL_CODE);
|
|
|
+ authInfoService.appHasExpired(schoolCode);
|
|
|
|
|
|
//2022-12-06加入登录之前逻辑
|
|
|
if (Objects.isNull(teachcloudLoginUrl) || Objects.equals(teachcloudLoginUrl, "")) {
|
|
@@ -99,13 +85,17 @@ public class OpenApiController {
|
|
|
}
|
|
|
String[] strs = teachcloudLoginUrl.split(SystemConstant.PATH_SUBSTR);
|
|
|
if (strs[0].contains(SystemConstant.PATH_MATCH)) {
|
|
|
- strs[0] = strs[0].replace(SystemConstant.PATH_MATCH, SCHOOL_CODE);
|
|
|
+ strs[0] = strs[0].replace(SystemConstant.PATH_MATCH, schoolCode);
|
|
|
}
|
|
|
|
|
|
- QueryWrapper<BasicSchool> basicSchoolQueryWrapper = new QueryWrapper<>();
|
|
|
- basicSchoolQueryWrapper.lambda().eq(BasicSchool::getCode, SCHOOL_CODE);
|
|
|
- BasicSchool basicSchool = basicSchoolService.getOne(basicSchoolQueryWrapper);
|
|
|
+ BasicSchool basicSchool = (BasicSchool) cacheService.get(SystemConstant.SCHOOL_CODE_CACHE, schoolCode);
|
|
|
+ if (Objects.isNull(basicSchool)) {
|
|
|
+ QueryWrapper<BasicSchool> basicSchoolQueryWrapper = new QueryWrapper<>();
|
|
|
+ basicSchoolQueryWrapper.lambda().eq(BasicSchool::getCode, schoolCode);
|
|
|
+ basicSchool = basicSchoolService.getOne(basicSchoolQueryWrapper);
|
|
|
+ }
|
|
|
Optional.ofNullable(basicSchool).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("学校信息不存在"));
|
|
|
+ cacheService.put(SystemConstant.SCHOOL_CODE_CACHE, schoolCode, basicSchool);
|
|
|
|
|
|
Long timestamp = System.currentTimeMillis();
|
|
|
String signature = SignatureEntity.build(SignatureType.SECRET, SystemConstant.METHOD, LOGIN_BEFORE_XJU_LOGIC_API, timestamp, basicSchool.getAccessKey(), basicSchool.getAccessSecret());
|
|
@@ -197,22 +187,25 @@ public class OpenApiController {
|
|
|
} else {
|
|
|
throw ExceptionResultEnum.ERROR.exception("gSessionId为空");
|
|
|
}
|
|
|
-
|
|
|
- commonService.redirectLogic(employeeNo, SCHOOL_CODE, returnUrl, Objects.nonNull(openParams) ? JacksonUtil.parseJson(openParams) : null);
|
|
|
+ return new RedirectParams(employeeNo, schoolCode, returnUrl, openParams);
|
|
|
}
|
|
|
|
|
|
- @ApiOperation(value = "西安交通大学cas鉴权退出接口")
|
|
|
- @RequestMapping(value = "/authentication/logout", method = RequestMethod.GET)
|
|
|
- @ApiResponses({@ApiResponse(code = 200, message = "返回消息", response = Result.class)})
|
|
|
- @Aac(auth = BOOL.FALSE)
|
|
|
- public void logout(@ApiParam(value = "sessionId", required = true) @RequestParam String gSessionId) throws IOException {
|
|
|
+ /**
|
|
|
+ * 西交大注销逻辑
|
|
|
+ *
|
|
|
+ * @param schoolCode
|
|
|
+ * @param gSessionId
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public void logout(String schoolCode,
|
|
|
+ String gSessionId) throws IOException {
|
|
|
if (Objects.isNull(logoutUrl) || Objects.equals(logoutUrl, "")) {
|
|
|
throw ExceptionResultEnum.PARAMS_ERROR.exception("鉴权退出地址不存在");
|
|
|
}
|
|
|
if (Objects.isNull(gSessionId) || Objects.equals(gSessionId, "")) {
|
|
|
throw ExceptionResultEnum.PARAMS_ERROR.exception("sessionId为空");
|
|
|
}
|
|
|
- authInfoService.appHasExpired(SCHOOL_CODE);
|
|
|
+ authInfoService.appHasExpired(schoolCode);
|
|
|
Map<String, Object> logoutParams = new LinkedHashMap<>();
|
|
|
logoutParams.put("gSessionId", gSessionId);
|
|
|
String logoutResult = HttpUtil.post(LOGOUT_URL, logoutParams, null);
|