|
@@ -1,20 +1,19 @@
|
|
|
package com.qmth.cdut.api;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
import com.qmth.boot.api.annotation.Aac;
|
|
|
import com.qmth.boot.api.annotation.BOOL;
|
|
|
import com.qmth.boot.api.constant.ApiConstant;
|
|
|
-import com.qmth.teachcloud.exchange.common.bean.params.OpenParams;
|
|
|
+import com.qmth.cdut.supwisdom.CasUtils;
|
|
|
+import com.qmth.cdut.supwisdom.Constants;
|
|
|
+import com.qmth.cdut.supwisdom.LoginUser;
|
|
|
import com.qmth.teachcloud.exchange.common.contant.SystemConstant;
|
|
|
import com.qmth.teachcloud.exchange.common.enums.ExceptionResultEnum;
|
|
|
import com.qmth.teachcloud.exchange.common.service.AuthInfoService;
|
|
|
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;
|
|
@@ -25,32 +24,26 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.servlet.ServletContext;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import javax.servlet.http.HttpSession;
|
|
|
import java.io.IOException;
|
|
|
-import java.util.LinkedHashMap;
|
|
|
-import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.StringJoiner;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
|
- * 西安交通大学开放接口前端控制器
|
|
|
+ * 成都理工大学开放接口前端控制器
|
|
|
* </p>
|
|
|
- *
|
|
|
- * @author wangliang
|
|
|
- * @since 2022-04-26
|
|
|
*/
|
|
|
-@Api(tags = "西安交通大学开放接口Controller")
|
|
|
+@Api(tags = "成都理工大学开放接口Controller")
|
|
|
@RestController
|
|
|
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.open}")
|
|
|
@Validated
|
|
|
public class OpenApiController {
|
|
|
private static final Logger log = LoggerFactory.getLogger(OpenApiController.class);
|
|
|
- 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 RETURN_URL = "https://org.xjtu.edu.cn/openplatform/login.html";
|
|
|
- private static final String schoolCode = "xjtu";//测试学校code,正式改成xjtu
|
|
|
+ private static final String schoolCode = "cdut";//测试学校code,成都理工大学
|
|
|
|
|
|
@Resource
|
|
|
CommonService commonService;
|
|
@@ -64,112 +57,89 @@ public class OpenApiController {
|
|
|
@Value("${cas.config.returnUrl}")
|
|
|
String returnUrl;
|
|
|
|
|
|
- @ApiOperation(value = "西安交通大学cas鉴权接口")
|
|
|
+ @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 {
|
|
|
- if ((Objects.isNull(code) || Objects.equals(code, ""))
|
|
|
- || (Objects.isNull(employeeNo) || Objects.equals(employeeNo, ""))) {
|
|
|
- throw ExceptionResultEnum.ERROR.exception("请先通过学校地址登录");
|
|
|
- }
|
|
|
+ public void sso(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
+ HttpSession session = request.getSession();
|
|
|
+ String targetUrl = CasUtils.getTargetUrl(request);
|
|
|
authInfoService.appHasExpired(schoolCode);
|
|
|
+ if (CasUtils.isLogin(session)) {
|
|
|
+ LoginUser loginUser = (LoginUser) session.getAttribute(Constants.LOGIN_USER_KEY);
|
|
|
+ String account = loginUser.getAccount();
|
|
|
|
|
|
- Map<String, Object> accessTokenParams = new LinkedHashMap<>();
|
|
|
- accessTokenParams.put("code", code);
|
|
|
- String accessTokenResult = HttpUtil.post(ACCESS_TOKEN_URL, accessTokenParams, null);
|
|
|
- String accessToken = null, gsessionId = null;
|
|
|
-
|
|
|
- //获取accessToken
|
|
|
- if (Objects.nonNull(accessTokenResult)) {
|
|
|
- log.info("accessTokenResult:{}", JacksonUtil.parseJson(accessTokenResult));
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(accessTokenResult);
|
|
|
- JSONObject object = jsonObject.getJSONObject("data");
|
|
|
- String message = jsonObject.getString("message");
|
|
|
- if (Objects.nonNull(object) && Objects.nonNull(message) && Objects.equals(message, "成功")) {
|
|
|
- accessToken = object.getString("accessToken");
|
|
|
- gsessionId = object.getString("gsessionId");
|
|
|
+ String sessionId = session.getId();
|
|
|
+ if (StringUtils.isNotBlank(sessionId)) {
|
|
|
+ StringJoiner stringJoiner = new StringJoiner("");
|
|
|
+ stringJoiner.add(logoutUrl).add(SystemConstant.GET_UNKNOWN).add("sessionId")
|
|
|
+ .add(SystemConstant.GET_EQUAL).add(sessionId);
|
|
|
+ returnUrl = stringJoiner.toString();
|
|
|
} else {
|
|
|
- throw ExceptionResultEnum.ERROR.exception(message);
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("sessionId为空");
|
|
|
}
|
|
|
- }
|
|
|
+ commonService.redirectLogic(account, schoolCode, returnUrl, null);
|
|
|
+ } else {
|
|
|
+ if (CasUtils.hasTicket(request)) {
|
|
|
+ LoginUser loginUser = CasUtils.getLoginUser(request);
|
|
|
+ if (loginUser.isLogin() && doLogin(loginUser, request)) {
|
|
|
+ CasUtils.login(loginUser, session);
|
|
|
|
|
|
- OpenParams openParams = null;
|
|
|
- //获取用户信息
|
|
|
- if (Objects.nonNull(accessToken)) {
|
|
|
- String userInfoResult = HttpUtil.post(USER_INFO_URL, null, accessToken);
|
|
|
- if (Objects.nonNull(userInfoResult)) {
|
|
|
- log.info("userInfoResult:{}", JacksonUtil.parseJson(userInfoResult));
|
|
|
- openParams = new OpenParams();
|
|
|
-// openParams.setResult(JacksonUtil.parseJson(userInfoResult));
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(userInfoResult);
|
|
|
- JSONObject object = jsonObject.getJSONObject("data");
|
|
|
- String message = jsonObject.getString("message");
|
|
|
- JSONArray userTypeJsonArray = object.getJSONArray("userTypes");
|
|
|
- JSONArray deptInfoJsonArray = object.getJSONArray("deptInfos");
|
|
|
- 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);
|
|
|
- openParams.setName(userTypeJsonObject.getString("memberName"));
|
|
|
- Integer userTypeRole = userTypeJsonObject.getInteger("userType");
|
|
|
- if (Objects.nonNull(userTypeRole) && userTypeRole.intValue() == 1) {
|
|
|
- openParams.setRoleName("学生");
|
|
|
- } else if (Objects.nonNull(userTypeRole) && userTypeRole.intValue() == 2) {
|
|
|
- openParams.setRoleName("教职工");
|
|
|
- }
|
|
|
- }
|
|
|
- if (Objects.nonNull(deptInfoJsonArray) && deptInfoJsonArray.size() > 0) {
|
|
|
- JSONObject deptInfoJsonArrayJsonObject = deptInfoJsonArray.getJSONObject(0);
|
|
|
- openParams.setDeptName(deptInfoJsonArrayJsonObject.getString("deptName"));
|
|
|
- }
|
|
|
+ String account = loginUser.getAccount();
|
|
|
+ commonService.redirectLogic(account, schoolCode, returnUrl, null);
|
|
|
} else {
|
|
|
- throw ExceptionResultEnum.ERROR.exception(message);
|
|
|
+ String loginUrl = CasUtils.getLoginUrl(request);
|
|
|
+ response.sendRedirect(loginUrl);
|
|
|
+ // TODO 可选:业务系统可根据实际情况进行处理
|
|
|
+// response.sendRedirect(CasUtils.getErrorUrl(request));
|
|
|
}
|
|
|
+ } else {
|
|
|
+ String loginUrl = CasUtils.getLoginUrl(request);
|
|
|
+ response.sendRedirect(loginUrl);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- //登出
|
|
|
- if (Objects.nonNull(gsessionId)) {
|
|
|
- StringJoiner stringJoiner = new StringJoiner("");
|
|
|
- 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 {
|
|
|
+ public void logout(@ApiParam(value = "sessionId", required = true) @RequestParam String sessionId) throws IOException {
|
|
|
if (Objects.isNull(logoutUrl) || Objects.equals(logoutUrl, "")) {
|
|
|
throw ExceptionResultEnum.PARAMS_ERROR.exception("鉴权退出地址不存在");
|
|
|
}
|
|
|
- if (Objects.isNull(gSessionId) || Objects.equals(gSessionId, "")) {
|
|
|
+ if (Objects.isNull(sessionId) || Objects.equals(sessionId, "")) {
|
|
|
throw ExceptionResultEnum.PARAMS_ERROR.exception("sessionId为空");
|
|
|
}
|
|
|
authInfoService.appHasExpired(schoolCode);
|
|
|
- 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(returnUrl);
|
|
|
- }
|
|
|
+
|
|
|
+ HttpServletRequest request = ServletUtil.getRequest();
|
|
|
+ HttpServletResponse response = ServletUtil.getResponse();
|
|
|
+ if (doLogout(request)) {
|
|
|
+ HttpSession session = request.getSession();
|
|
|
+ CasUtils.logout(session);
|
|
|
+ response.sendRedirect(CasUtils.getLogoutUrl(request));
|
|
|
+ } else {
|
|
|
+ response.sendRedirect(CasUtils.getLoginUrl(request));
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public boolean doLogin(LoginUser loginUser, HttpServletRequest request) {
|
|
|
+ HttpSession session = request.getSession();
|
|
|
+ ServletContext application = session.getServletContext();
|
|
|
+ // 如果使用了Spring可以用下面的方法获取spring的context对象
|
|
|
+ // WebApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(application);
|
|
|
+ // 如果需要使用SpringMVC上下文、可以用下面的方法获取springMVC的context对象
|
|
|
+ // WebApplicationContext mvcContext = RequestContextUtils.getWebApplicationContext(request);
|
|
|
+
|
|
|
+ // TODO 需要业务系统重写
|
|
|
+
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean doLogout(HttpServletRequest request) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
}
|