getContextPath

This commit is contained in:
MaxKey 2023-02-14 15:57:45 +08:00
parent fe82cb8c72
commit 22340037eb
5 changed files with 19 additions and 47 deletions

View File

@ -102,7 +102,7 @@ public class SavedRequestAwareAuthenticationSuccessHandler
logger.trace("CAS " + request.getParameter(WebConstants.CAS_SERVICE_PARAMETER));
if (request.getParameter(WebConstants.CAS_SERVICE_PARAMETER) != null
&& request.getParameter(WebConstants.CAS_SERVICE_PARAMETER).startsWith("http")) {
targetUrl = WebContext.getHttpContextPath(true) + "/authz/cas/login?service="
targetUrl = WebContext.getContextPath(true) + "/authz/cas/login?service="
+ request.getParameter(WebConstants.CAS_SERVICE_PARAMETER);
}
targetUrl = targetUrl == null ? "/forwardindex" : targetUrl;

View File

@ -60,7 +60,7 @@ public class SocialSignOnEndpoint extends AbstractSocialSignOnEndpoint{
) {
_logger.trace("SocialSignOn provider : " + provider);
String instId = WebContext.getInst().getId();
String originURL =WebContext.getHttpContextPath(request,false);
String originURL =WebContext.getContextPath(request,false);
String authorizationUrl =
buildAuthRequest(
instId,
@ -77,7 +77,7 @@ public class SocialSignOnEndpoint extends AbstractSocialSignOnEndpoint{
public ResponseEntity<?> scanQRCode(HttpServletRequest request,
@PathVariable("provider") String provider) {
String instId = WebContext.getInst().getId();
String originURL =WebContext.getHttpContextPath(request,false);
String originURL =WebContext.getContextPath(request,false);
AuthRequest authRequest =
buildAuthRequest(
instId,
@ -112,7 +112,7 @@ public class SocialSignOnEndpoint extends AbstractSocialSignOnEndpoint{
HttpServletRequest request) {
//auth call back may exception
try {
String originURL = WebContext.getHttpContextPath(request,false);
String originURL = WebContext.getContextPath(request,false);
SocialsAssociate socialsAssociate =
this.authCallback(userInfo.getInstId(),provider,originURL + applicationConfig.getFrontendUri());
socialsAssociate.setSocialUserInfo(accountJsonString);
@ -139,7 +139,7 @@ public class SocialSignOnEndpoint extends AbstractSocialSignOnEndpoint{
HttpServletRequest request) {
//auth call back may exception
try {
String originURL =WebContext.getHttpContextPath(request,false);
String originURL =WebContext.getContextPath(request,false);
String instId = WebContext.getInst().getId();
SocialsAssociate socialsAssociate =
this.authCallback(instId,provider,originURL + applicationConfig.getFrontendUri());

View File

@ -39,6 +39,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.security.web.util.UrlUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;
@ -158,9 +159,9 @@ public final class WebContext {
*
* @return String HttpContextPath
*/
public static String getHttpContextPath(boolean isContextPath) {
public static String getContextPath(boolean isContextPath) {
HttpServletRequest httpServletRequest = WebContext.getRequest();
return getHttpContextPath(httpServletRequest,isContextPath);
return getContextPath(httpServletRequest,isContextPath);
}
/**
@ -169,45 +170,16 @@ public final class WebContext {
* @return String eg:http://192.168.1.20:9080/webcontext or
* http://www.website.com/webcontext
*/
public static String getHttpContextPath(HttpServletRequest request,boolean isContextPath) {
ApplicationConfig applicationConfig =
WebContext.getBean("applicationConfig",ApplicationConfig.class);
_logger.trace("Config ServerPrefix " + applicationConfig.getServerPrefix());
_logger.trace("Config DomainName " + applicationConfig.getDomainName());
_logger.trace("ServerName " + request.getServerName());
StringBuilder url = new StringBuilder();
if (request.getServerName().matches(ipAddressRegex)
||request.getServerName().equalsIgnoreCase("localhost")) {
url.append(request.getScheme().toLowerCase())
.append("://").append(request.getServerName())
.append(request.getServerPort());
}else {
String scheme = request.getScheme().toLowerCase();
String serverName = request.getServerName();
int serverPort = request.getServerPort();
url.append(scheme).append("://").append(serverName);
// Only add port if not default
if ("http".equals(scheme)) {
if (serverPort != 80) {
url.append(":").append(serverPort);
}
}
else if ("https".equals(scheme)) {
if (serverPort != 443) {
url.append(":").append(serverPort);
}
}
}
public static String getContextPath(HttpServletRequest request,boolean isContextPath) {
String fullRequestUrl = UrlUtils.buildFullRequestUrl(request);
StringBuilder url = new StringBuilder(fullRequestUrl.substring(0, fullRequestUrl.indexOf(request.getContextPath())));
if(isContextPath) {
url.append(request.getContextPath());
}
_logger.trace("httpContextPath {}" , url);
return url.toString();
_logger.trace("http ContextPath {}" , url);
return url.toString();
}
/**

View File

@ -49,7 +49,7 @@ public class BasePathTagDirective implements TemplateDirectiveModel {
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
throws TemplateException, IOException {
env.getOut().append(WebContext.getHttpContextPath(request,true));
env.getOut().append(WebContext.getContextPath(request,true));
}

View File

@ -112,19 +112,19 @@ public class SamlMetadataEndpoint {
IDPSSODescriptor descriptor = metadataGenerator.buildIDPSSODescriptor();
descriptor.getSingleSignOnServices().add(
metadataGenerator.getSingleSignOnService(WebContext.getHttpContextPath(true) + "/authz/saml20/" + appId,null));
metadataGenerator.getSingleSignOnService(WebContext.getContextPath(true) + "/authz/saml20/" + appId,null));
descriptor.getSingleSignOnServices().add(
metadataGenerator.getSingleSignOnService(WebContext.getHttpContextPath(true) + "/authz/saml20/" + appId,SAMLConstants.SAML2_REDIRECT_BINDING_URI));
metadataGenerator.getSingleSignOnService(WebContext.getContextPath(true) + "/authz/saml20/" + appId,SAMLConstants.SAML2_REDIRECT_BINDING_URI));
descriptor.getSingleSignOnServices().add(
metadataGenerator.getSingleSignOnService(WebContext.getHttpContextPath(true) + "/authz/saml20/" + appId,SAMLConstants.SAML2_POST_SIMPLE_SIGN_BINDING_URI));
metadataGenerator.getSingleSignOnService(WebContext.getContextPath(true) + "/authz/saml20/" + appId,SAMLConstants.SAML2_POST_SIMPLE_SIGN_BINDING_URI));
descriptor.getSingleLogoutServices().add(
metadataGenerator.getSingleLogoutService(WebContext.getHttpContextPath(true) + "/force/logout" , null));
metadataGenerator.getSingleLogoutService(WebContext.getContextPath(true) + "/force/logout" , null));
descriptor.getManageNameIDServices().add(
metadataGenerator.getManageNameIDService(WebContext.getHttpContextPath(true) + "/metadata/saml20/" + WebConstants.MXK_METADATA_PREFIX + appId + ".xml"));
metadataGenerator.getManageNameIDService(WebContext.getContextPath(true) + "/metadata/saml20/" + WebConstants.MXK_METADATA_PREFIX + appId + ".xml"));
descriptor.getKeyDescriptors().add(metadataGenerator.generateEncryptionKeyDescriptor(signingCredential));