|
@@ -4,6 +4,7 @@ import com.qmth.teachcloud.cas.util.supwisdom.utils.Base64Utils;
|
|
|
import com.qmth.teachcloud.cas.util.supwisdom.utils.BooleanUtils;
|
|
|
import com.qmth.teachcloud.cas.util.supwisdom.utils.HttpRequestUtils;
|
|
|
import com.qmth.teachcloud.cas.util.supwisdom.utils.StringUtils;
|
|
|
+import com.qmth.teachcloud.exchange.common.util.ServletUtil;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpSession;
|
|
@@ -20,7 +21,8 @@ public class CasUtils {
|
|
|
return BooleanUtils.toBoolean(String.valueOf(isLogin));
|
|
|
}
|
|
|
|
|
|
- public static String getBasePath(HttpServletRequest request) {
|
|
|
+ public static String getBasePath() {
|
|
|
+ HttpServletRequest request = ServletUtil.getRequest();
|
|
|
String scheme = request.getScheme();
|
|
|
String serverName = request.getServerName();
|
|
|
int serverPort = request.getServerPort();
|
|
@@ -29,41 +31,33 @@ public class CasUtils {
|
|
|
// 判断是否配置了显式端口
|
|
|
boolean explicit_port = Constants.CLIENT_SYSTEM_EXPLICIT_PORT != null
|
|
|
&& !"".equals(Constants.CLIENT_SYSTEM_EXPLICIT_PORT);
|
|
|
-
|
|
|
if (explicit_port) {
|
|
|
try {
|
|
|
serverPort = Integer
|
|
|
.parseInt(Constants.CLIENT_SYSTEM_EXPLICIT_PORT);
|
|
|
} catch (Exception e) {
|
|
|
-
|
|
|
// 异常时赋值,方便双方排查问题
|
|
|
serverPort = 19000;
|
|
|
}
|
|
|
-
|
|
|
- String url = scheme + "://" + serverName + ":" + serverPort
|
|
|
- + contextPath + "/";
|
|
|
+ String url = scheme + "://" + serverName + ":" + serverPort + contextPath + "/";
|
|
|
return url;
|
|
|
-
|
|
|
} else {
|
|
|
if ((serverPort == 80) || (serverPort == 443)) {
|
|
|
-
|
|
|
- String url = scheme + "://" + serverName + contextPath
|
|
|
- + "/";
|
|
|
+ String url = scheme + "://" + serverName + contextPath + "/";
|
|
|
return url;
|
|
|
} else {
|
|
|
- String url = scheme + "://" + serverName + ":" + serverPort
|
|
|
- + contextPath + "/";
|
|
|
+ String url = scheme + "://" + serverName + ":" + serverPort + contextPath + "/";
|
|
|
return url;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取TargetUrl
|
|
|
*/
|
|
|
- public static String getTargetUrl(HttpServletRequest request) {
|
|
|
- String basePath = getBasePath(request);
|
|
|
+ public static String getTargetUrl() {
|
|
|
+ HttpServletRequest request = ServletUtil.getRequest();
|
|
|
+ String basePath = getBasePath();
|
|
|
|
|
|
// 获取请求中的targetUrl
|
|
|
String targetUrl = request.getParameter(Constants.TARGET_URL_KEY);
|
|
@@ -84,58 +78,46 @@ public class CasUtils {
|
|
|
/**
|
|
|
* 判断票据是否存在
|
|
|
*/
|
|
|
- public static boolean hasTicket(HttpServletRequest request) {
|
|
|
+ public static boolean hasTicket() {
|
|
|
+ HttpServletRequest request = ServletUtil.getRequest();
|
|
|
Object ticket = request.getParameter(Constants.TICKET_KEY);
|
|
|
|
|
|
// System.out.println("ticket = " + ticket);
|
|
|
- return ticket != null
|
|
|
- && !StringUtils.isEmpty(String.valueOf(ticket));
|
|
|
+ return ticket != null && !StringUtils.isEmpty(String.valueOf(ticket));
|
|
|
}
|
|
|
|
|
|
- public static String getURLEncodeServiceUrl(HttpServletRequest request)
|
|
|
+ public static String getURLEncodeServiceUrl()
|
|
|
throws UnsupportedEncodingException {
|
|
|
-
|
|
|
// 编码成系统可识别的加密串
|
|
|
- String targetUrl = getTargetUrl(request);
|
|
|
+ String targetUrl = getTargetUrl();
|
|
|
String base64TargetUrl = Base64Utils.encodeBase64Str(targetUrl);
|
|
|
-
|
|
|
- String serviceUrlRoot = getBasePath(request)
|
|
|
- + Constants.SSO_LOGIN_URI;
|
|
|
-
|
|
|
- String serviceUrl = serviceUrlRoot + "?" + Constants.TARGET_URL_KEY
|
|
|
- + "=" + Constants.BASE64_PREFIX + base64TargetUrl;
|
|
|
+ String serviceUrlRoot = getBasePath() + Constants.SSO_LOGIN_URI;
|
|
|
+ String serviceUrl = serviceUrlRoot + "?" + Constants.TARGET_URL_KEY + "=" + Constants.BASE64_PREFIX + base64TargetUrl;
|
|
|
return URLEncoder.encode(serviceUrl, Constants.UTF_8_STR);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取Cas登录Url 登录成功后返回票据
|
|
|
*/
|
|
|
- public static String getLoginUrl(HttpServletRequest request)
|
|
|
- throws UnsupportedEncodingException {
|
|
|
- String encodeServiceUrl = getURLEncodeServiceUrl(request);
|
|
|
-
|
|
|
- return Constants.CAS_LOGIN_URL + "?" + Constants.SERVICE_KEY + "="
|
|
|
- + encodeServiceUrl;
|
|
|
+ public static String getLoginUrl() throws UnsupportedEncodingException {
|
|
|
+ String encodeServiceUrl = getURLEncodeServiceUrl();
|
|
|
+ return Constants.CAS_LOGIN_URL + "?" + Constants.SERVICE_KEY + "=" + encodeServiceUrl;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取校验票据Url
|
|
|
*/
|
|
|
- public static String getServiceValidateUrl(HttpServletRequest request)
|
|
|
- throws UnsupportedEncodingException {
|
|
|
-
|
|
|
- String encodeServiceUrl = getURLEncodeServiceUrl(request);
|
|
|
+ public static String getServiceValidateUrl() throws UnsupportedEncodingException {
|
|
|
+ HttpServletRequest request = ServletUtil.getRequest();
|
|
|
+ String encodeServiceUrl = getURLEncodeServiceUrl();
|
|
|
Object ticket = request.getParameter(Constants.TICKET_KEY);
|
|
|
-
|
|
|
return Constants.CAS_VALIDATE_URL + "?" + Constants.TICKET_KEY
|
|
|
+ "=" + ticket + "&" + Constants.SERVICE_KEY + "="
|
|
|
+ encodeServiceUrl;
|
|
|
}
|
|
|
|
|
|
- public static LoginUser getLoginUser(HttpServletRequest request)
|
|
|
- throws IOException {
|
|
|
- String serviceValidateUrl = getServiceValidateUrl(request);
|
|
|
-
|
|
|
+ public static LoginUser getLoginUser() throws IOException {
|
|
|
+ String serviceValidateUrl = getServiceValidateUrl();
|
|
|
String casUserInfoXml = HttpRequestUtils.doGet(serviceValidateUrl);
|
|
|
casUserInfoXml = "<?xml version=\"1.0\" encoding=\"UTF8\"?>" + casUserInfoXml;
|
|
|
return new LoginUser(casUserInfoXml);
|
|
@@ -144,23 +126,17 @@ public class CasUtils {
|
|
|
/**
|
|
|
* 获取登出地址
|
|
|
*/
|
|
|
- public static String getLogoutUrl(HttpServletRequest request)
|
|
|
- throws UnsupportedEncodingException {
|
|
|
+ public static String getLogoutUrl() throws UnsupportedEncodingException {
|
|
|
// 获取本次请求的根Path
|
|
|
- String loginUrlRoot = getBasePath(request)
|
|
|
- + Constants.SSO_LOGIN_URI;
|
|
|
- String encodeLoginUrlRoot = URLEncoder.encode(loginUrlRoot,
|
|
|
- Constants.UTF_8_STR);
|
|
|
- return Constants.CAS_LOGOUT_URL + "?" + Constants.SERVICE_KEY + "="
|
|
|
- + encodeLoginUrlRoot;
|
|
|
+ String loginUrlRoot = getBasePath() + Constants.SSO_LOGIN_URI;
|
|
|
+ String encodeLoginUrlRoot = URLEncoder.encode(loginUrlRoot, Constants.UTF_8_STR);
|
|
|
+ return Constants.CAS_LOGOUT_URL + "?" + Constants.SERVICE_KEY + "=" + encodeLoginUrlRoot;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 集成失败页
|
|
|
*/
|
|
|
- public static String getErrorUrl(HttpServletRequest request)
|
|
|
- throws UnsupportedEncodingException {
|
|
|
-
|
|
|
+ public static String getErrorUrl() throws UnsupportedEncodingException {
|
|
|
return Constants.SSO_ERROR_URI;
|
|
|
}
|
|
|
|
|
@@ -179,5 +155,4 @@ public class CasUtils {
|
|
|
session.removeAttribute(Constants.LOGIN_KEY);
|
|
|
session.removeAttribute(Constants.LOGIN_USER_KEY);
|
|
|
}
|
|
|
-
|
|
|
}
|