From 2691e499574c5fa27db569b6099fa2b1755aad27 Mon Sep 17 00:00:00 2001 From: MaxKey Date: Thu, 16 Jul 2020 09:44:50 +0800 Subject: [PATCH] =?UTF-8?q?http=20cookie=E5=86=B2=E7=AA=81=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/org/maxkey/web/WebContext.java | 52 +++++++++++-------- .../maxkey/web/tag/BasePathTagDirective.java | 23 ++------ .../org/maxkey/web/tag/BaseTagDirective.java | 8 ++- 3 files changed, 38 insertions(+), 45 deletions(-) diff --git a/maxkey-core/src/main/java/org/maxkey/web/WebContext.java b/maxkey-core/src/main/java/org/maxkey/web/WebContext.java index 79c9fdc18..ca85739ac 100644 --- a/maxkey-core/src/main/java/org/maxkey/web/WebContext.java +++ b/maxkey-core/src/main/java/org/maxkey/web/WebContext.java @@ -35,6 +35,8 @@ import org.maxkey.domain.UserInfo; import org.maxkey.util.DateUtils; import org.maxkey.util.StringGenerator; import org.maxkey.web.message.Message; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; @@ -52,7 +54,9 @@ import org.springframework.web.servlet.support.RequestContextUtils; * @since 1.5 */ public final class WebContext { - + + final static Logger _logger = LoggerFactory.getLogger(WebContext.class); + public static Properties properties; /** @@ -192,33 +196,39 @@ public final class WebContext { } /** - * get Http Context full Path,if port equals 80 is omitted. + * get Http Context full Path. + * + * @return String HttpContextPath + */ + public static String getHttpContextPath() { + HttpServletRequest httpServletRequest = WebContext.getRequest(); + return getHttpContextPath(httpServletRequest); + } + + /** + * get Http Context full Path,if port equals 80 or 443 is omitted. * * @return String eg:http://192.168.1.20:9080/webcontext or * http://www.website.com/webcontext */ - public static String getHttpContextPath() { - HttpServletRequest httpServletRequest = WebContext.getRequest(); + public static String getHttpContextPath(HttpServletRequest httpServletRequest) { ApplicationConfig applicationConfig = ( ApplicationConfig) WebContext.getBean("applicationConfig"); - - if (applicationConfig.getServerPrefix() != null - && !applicationConfig.getServerPrefix().equals("")) { - return applicationConfig.getServerPrefix(); - } else { - String httpContextPath = - httpServletRequest.getScheme() + "://" + applicationConfig.getDomainName(); - int port = httpServletRequest.getServerPort(); - if (port == 443 && httpServletRequest.getScheme().equalsIgnoreCase("https")) { - // - } else if (port == 80 && httpServletRequest.getScheme().equalsIgnoreCase("http")) { - // - } else { - httpContextPath += ":" + port; - } - httpContextPath += httpServletRequest.getContextPath() + ""; - return httpContextPath; + + _logger.trace("Config ServerPrefix " + applicationConfig.getServerPrefix()); + _logger.trace("Config DomainName " + applicationConfig.getDomainName()); + _logger.trace("ServerName " + httpServletRequest.getServerName()); + + String scheme = httpServletRequest.getScheme().toLowerCase(); + String httpContextPath = scheme + "://"+httpServletRequest.getServerName(); + int port = httpServletRequest.getServerPort(); + if(!(port==80 || port==443)){ + httpContextPath += ":"+port; } + httpContextPath += httpServletRequest.getContextPath() + ""; + + _logger.trace("httpContextPath " + httpContextPath); + return httpContextPath; } diff --git a/maxkey-core/src/main/java/org/maxkey/web/tag/BasePathTagDirective.java b/maxkey-core/src/main/java/org/maxkey/web/tag/BasePathTagDirective.java index fd6ffd8da..217b92670 100644 --- a/maxkey-core/src/main/java/org/maxkey/web/tag/BasePathTagDirective.java +++ b/maxkey-core/src/main/java/org/maxkey/web/tag/BasePathTagDirective.java @@ -22,6 +22,7 @@ import java.util.Map; import javax.servlet.http.HttpServletRequest; +import org.maxkey.web.WebContext; import org.springframework.beans.factory.annotation.Autowired; import freemarker.core.Environment; @@ -33,38 +34,22 @@ import freemarker.template.TemplateModel; /** * <@basePath/> * 获取请求地址及应用上下文标签 - * get Http Context full Path,if port equals 80 443 is omitted - * @return String - * eg:http://192.168.1.20:9080/webcontext or http://www.website.com/webcontext * @author Crystal.Sea * */ @FreemarkerTag("basePath") public class BasePathTagDirective implements TemplateDirectiveModel { + @Autowired private HttpServletRequest request; - private String basePath = null; - @Override public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { - if(basePath==null) { - basePath = request.getScheme()+"://"+request.getServerName(); - int port=request.getServerPort(); - if(port==443 && request.getScheme().equalsIgnoreCase("https")){ - - }else if(port==80 && request.getScheme().equalsIgnoreCase("http")){ - - }else{ - basePath += ":"+port; - } - basePath += request.getContextPath()+""; - } - env.getOut().append(basePath); + + env.getOut().append(WebContext.getHttpContextPath(request)); - } } diff --git a/maxkey-core/src/main/java/org/maxkey/web/tag/BaseTagDirective.java b/maxkey-core/src/main/java/org/maxkey/web/tag/BaseTagDirective.java index ccc9e3e81..7c19226a8 100644 --- a/maxkey-core/src/main/java/org/maxkey/web/tag/BaseTagDirective.java +++ b/maxkey-core/src/main/java/org/maxkey/web/tag/BaseTagDirective.java @@ -40,16 +40,14 @@ import freemarker.template.TemplateModel; public class BaseTagDirective implements TemplateDirectiveModel { @Autowired private HttpServletRequest request; - - private static String base = null; @Override public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { //String url = params.get(URL).toString(); - if(base==null) { - base=request.getContextPath(); - } + + String base=request.getContextPath(); + env.getOut().append(base);