|
@@ -13,9 +13,11 @@ 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.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
@@ -23,6 +25,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.Map;
|
|
@@ -46,6 +49,8 @@ public class OpenApiController {
|
|
|
private static final String ACCESS_TOKEN_URL = "https://org.xjtu.edu.cn/openplatform/oauth/getAccessToken";
|
|
|
private static final String USER_INFO_URL = "https://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 RETURN_URL = "https://org.xjtu.edu.cn/openplatform/login.html";
|
|
|
+ private static final String schoolCode = "xjtu";//测试学校code,正式改成xjtu
|
|
|
|
|
|
@Resource
|
|
|
CommonService commonService;
|
|
@@ -53,6 +58,9 @@ public class OpenApiController {
|
|
|
@Resource
|
|
|
AuthInfoService authInfoService;
|
|
|
|
|
|
+ @Value("${cas.config.logoutUrl}")
|
|
|
+ String logoutUrl;
|
|
|
+
|
|
|
@ApiOperation(value = "西安交通大学cas鉴权接口")
|
|
|
@ApiResponses({@ApiResponse(code = 200, message = "返回消息", response = Result.class)})
|
|
|
@RequestMapping(value = "/authentication", method = RequestMethod.GET)
|
|
@@ -66,7 +74,6 @@ public class OpenApiController {
|
|
|
|| (Objects.isNull(employeeNo) || Objects.equals(employeeNo, ""))) {
|
|
|
throw ExceptionResultEnum.ERROR.exception("请先通过学校地址登录");
|
|
|
}
|
|
|
- String schoolCode = "xjtu";//测试学校code,正式改成xjtu
|
|
|
authInfoService.appHasExpired(schoolCode);
|
|
|
|
|
|
Map<String, Object> accessTokenParams = new LinkedHashMap<>();
|
|
@@ -80,7 +87,7 @@ public class OpenApiController {
|
|
|
JSONObject jsonObject = JSONObject.parseObject(accessTokenResult);
|
|
|
JSONObject object = jsonObject.getJSONObject("data");
|
|
|
String message = jsonObject.getString("message");
|
|
|
- if (Objects.nonNull(object) && Objects.equals(message, "成功")) {
|
|
|
+ if (Objects.nonNull(object) && Objects.nonNull(message) && Objects.equals(message, "成功")) {
|
|
|
accessToken = object.getString("accessToken");
|
|
|
gsessionId = object.getString("gsessionId");
|
|
|
} else {
|
|
@@ -101,7 +108,7 @@ public class OpenApiController {
|
|
|
String message = jsonObject.getString("message");
|
|
|
JSONArray userTypeJsonArray = object.getJSONArray("userTypes");
|
|
|
JSONArray deptInfoJsonArray = object.getJSONArray("deptInfos");
|
|
|
- if (Objects.nonNull(object) && Objects.equals(message, "成功")) {
|
|
|
+ if (Objects.nonNull(object) && Objects.nonNull(message) && Objects.equals(message, "成功")) {
|
|
|
openParams.setOrgName(object.getString("orgName"));
|
|
|
if (Objects.nonNull(userTypeJsonArray) && userTypeJsonArray.size() > 0) {
|
|
|
JSONObject userTypeJsonObject = userTypeJsonArray.getJSONObject(0);
|
|
@@ -125,18 +132,40 @@ public class OpenApiController {
|
|
|
|
|
|
//登出
|
|
|
if (Objects.nonNull(gsessionId)) {
|
|
|
-// Map<String, Object> logoutParams = new LinkedHashMap<>();
|
|
|
-// logoutParams.put("gSessionId", gsessionId);
|
|
|
-// String logoutResult = HttpUtil.post(LOGOUT_URL, logoutParams, null);
|
|
|
-// if (Objects.nonNull(logoutResult)) {
|
|
|
-// log.info("logoutResult:{}", JacksonUtil.parseJson(logoutResult));
|
|
|
-// }
|
|
|
StringJoiner stringJoiner = new StringJoiner("");
|
|
|
- stringJoiner.add(LOGOUT_URL).add(SystemConstant.GET_UNKNOWN).add("gSessionId")
|
|
|
+ stringJoiner.add(logoutUrl).add(SystemConstant.GET_UNKNOWN).add("gSessionId")
|
|
|
.add(SystemConstant.GET_EQUAL).add(gsessionId);
|
|
|
returnUrl = stringJoiner.toString();
|
|
|
+ } else {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("gSessionId为空");
|
|
|
}
|
|
|
|
|
|
commonService.redirectLogic(employeeNo, schoolCode, returnUrl, Objects.nonNull(openParams) ? JacksonUtil.parseJson(openParams) : null);
|
|
|
}
|
|
|
+
|
|
|
+ @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 {
|
|
|
+ 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为空");
|
|
|
+ }
|
|
|
+ Map<String, Object> logoutParams = new LinkedHashMap<>();
|
|
|
+ logoutParams.put("gSessionId", gSessionId);
|
|
|
+ String logoutResult = HttpUtil.post(LOGOUT_URL, logoutParams, null);
|
|
|
+ if (Objects.nonNull(logoutResult)) {
|
|
|
+ log.info("logoutResult:{}", JacksonUtil.parseJson(logoutResult));
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(logoutResult);
|
|
|
+ String message = jsonObject.getString("message");
|
|
|
+ if (Objects.nonNull(message) && Objects.equals(message, "成功")) {
|
|
|
+ HttpServletResponse response = ServletUtil.getResponse();
|
|
|
+ response.setHeader("Access-Control-Allow-Origin", SystemConstant.PATH_MATCH);
|
|
|
+ response.sendRedirect(RETURN_URL);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|