Browse Source

jfinal 4.9

James 5 years ago
parent
commit
3fc8cb41c9

+ 0 - 12
src/main/java/com/jfinal/config/Constants.java

@@ -105,18 +105,6 @@ final public class Constants {
 	}
 	}
 	
 	
 	/**
 	/**
-	 * 配置重定向时使用的协议,只允许配置为 http 与 https
-	 * 
-	 * 该配置将协议添加到未指定协议的 url 之中,主要用于解决 nginx 代理做 https 时无法重定向到 https 的问题
-	 * 
-	 * 注意:当 url 中已经包含协议时,该配置无效,因为要支持跨域名重定向
-	 *      例如: redirect("https://jfinal.com");
-	 */
-	public void setRedirectProtocol(String protocol) {
-		RenderManager.me().setRedirectProtocol(protocol);
-	}
-	
-	/**
 	 * 设置 Json 转换工厂实现类,目前支持:JFinalJsonFactory(默认)、JacksonFactory、FastJsonFactory
 	 * 设置 Json 转换工厂实现类,目前支持:JFinalJsonFactory(默认)、JacksonFactory、FastJsonFactory
 	 * 分别支持 JFinalJson、Jackson、FastJson
 	 * 分别支持 JFinalJson、Jackson、FastJson
 	 */
 	 */

+ 5 - 0
src/main/java/com/jfinal/kit/HttpKit.java

@@ -305,6 +305,11 @@ public class HttpKit {
 	
 	
 	/**
 	/**
 	 * 检测是否为 https 请求
 	 * 检测是否为 https 请求
+	 * 
+	 * nginx 代理实现 https 的场景,需要对 nginx 进行如下配置:
+	 *     proxy_set_header X-Forwarded-Proto https;
+	 * 或者配置:
+	 *     proxy_set_header X-Forwarded-Proto $scheme;
 	 */
 	 */
 	public static boolean isHttps(HttpServletRequest request) {
 	public static boolean isHttps(HttpServletRequest request) {
 		return  "https".equalsIgnoreCase(request.getHeader("X-Forwarded-Proto"))
 		return  "https".equalsIgnoreCase(request.getHeader("X-Forwarded-Proto"))

+ 19 - 25
src/main/java/com/jfinal/render/RedirectRender.java

@@ -18,6 +18,7 @@ package com.jfinal.render;
 
 
 import java.io.IOException;
 import java.io.IOException;
 import com.jfinal.core.JFinal;
 import com.jfinal.core.JFinal;
+import com.jfinal.kit.HttpKit;
 
 
 /**
 /**
  * RedirectRender with status: 302 Found.
  * RedirectRender with status: 302 Found.
@@ -28,24 +29,6 @@ public class RedirectRender extends Render {
 	protected boolean withQueryString;
 	protected boolean withQueryString;
 	protected static final String contextPath = getContxtPath();
 	protected static final String contextPath = getContxtPath();
 	
 	
-	protected static String protocol = null;
-	
-	/**
-	 * 配置重定向时使用的协议,只允许配置为 http 与 https
-	 * 
-	 * 该配置将协议添加到未指定协议的 url 之中,主要用于解决 nginx 代理做 https 时无法重定向到 https 的问题
-	 * 
-	 * 
-	 * 注意:当 url 中已经包含协议时,该配置无效,因为要支持跨域名重定向
-	 *      例如: redirect("https://jfinal.com");
-	 */
-	public static void setProtocol(String protocol) {
-		if (!"http".equalsIgnoreCase(protocol) && !"https".equalsIgnoreCase(protocol)) {
-			throw new IllegalArgumentException("protocol must be \"http\" or \"https\"");
-		}
-		RedirectRender.protocol = protocol.toLowerCase() + "://";
-	}
-	
 	static String getContxtPath() {
 	static String getContxtPath() {
 		String cp = JFinal.me().getContextPath();
 		String cp = JFinal.me().getContextPath();
 		return ("".equals(cp) || "/".equals(cp)) ? null : cp;
 		return ("".equals(cp) || "/".equals(cp)) ? null : cp;
@@ -83,28 +66,39 @@ public class RedirectRender extends Render {
 		}
 		}
 		
 		
 		// 跳过 http/https 已指定过协议类型的 url,用于支持跨域名重定向
 		// 跳过 http/https 已指定过协议类型的 url,用于支持跨域名重定向
-		if (protocol == null || ret.toLowerCase().startsWith("http")) {
+		if (ret.toLowerCase().startsWith("http")) {
+			return ret;
+		}
+		
+		// http 跳过无需处理
+		if ( ! HttpKit.isHttps(request) ) {
 			return ret;
 			return ret;
 		}
 		}
 		
 		
 		/**
 		/**
 		 * 支持 https 协议下的重定向
 		 * 支持 https 协议下的重定向
-		 *     https://jfinal.com/feedback/6939
 		 * 
 		 * 
-		 * PS:nginx 层面配置 http 重定向到 https 的方法为:
-		 *     proxy_redirect http:// https://;
+		 * nginx 代理实现 https 的场景,需要对 nginx 进行如下配置:
+		 *     proxy_set_header X-Forwarded-Proto https;
+		 * 或者配置:
+		 *     proxy_set_header X-Forwarded-Proto $scheme;
+		 * 
+		 * 
+		 * PS:nginx 将 http 重定向到 https 的配置为:
+		 *    proxy_redirect http:// https://;
+		 *    注意: 同时支持 http 与 https 的场景需要去除该配置
 		 */
 		 */
 		String serverName = request.getServerName();
 		String serverName = request.getServerName();
 		
 		
 		int port = request.getServerPort();
 		int port = request.getServerPort();
-		if (port != 80 && port != 443) {
+		if (port != 443) {
 			serverName = serverName + ":" + port;
 			serverName = serverName + ":" + port;
 		}
 		}
 		
 		
 		if (ret.charAt(0) != '/') {
 		if (ret.charAt(0) != '/') {
-			return protocol + serverName + "/" + ret;
+			return "https://" + serverName + "/" + ret;
 		} else {
 		} else {
-			return protocol + serverName + ret;
+			return "https://" + serverName + ret;
 		}
 		}
 	}
 	}
 	
 	

+ 0 - 12
src/main/java/com/jfinal/render/RenderManager.java

@@ -52,18 +52,6 @@ public class RenderManager {
 		this.renderFactory = renderFactory;
 		this.renderFactory = renderFactory;
 	}
 	}
 	
 	
-	/**
-	 * 配置重定向时使用的协议,只允许配置为 http 与 https
-	 * 
-	 * 该配置将协议添加到未指定协议的 url 之中,主要用于解决 nginx 代理做 https 时无法重定向到 https 的问题
-	 * 
-	 * 注意:当 url 中已经包含协议时,该配置无效,因为要支持跨域名重定向
-	 *      例如: redirect("https://jfinal.com");
-	 */
-	public void setRedirectProtocol(String protocol) {
-		RedirectRender.setProtocol(protocol);
-	}
-	
 	public void init(Engine engine, Constants constants, ServletContext servletContext) {
 	public void init(Engine engine, Constants constants, ServletContext servletContext) {
 		this.engine = engine;
 		this.engine = engine;
 		this.constants = constants;
 		this.constants = constants;