From 47d3d5bd78bdb82af1f2fcb4eb555e43b3108894 Mon Sep 17 00:00:00 2001 From: thinkgem Date: Tue, 21 Jan 2020 10:34:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20scheme=20=E5=BC=BA?= =?UTF-8?q?=E5=88=B6=E6=9B=BF=E6=8D=A2=E4=B8=BA=20https=20=E7=9A=84?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E5=99=A8=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=BD=93?= =?UTF-8?q?=20nginx=20=E9=85=8D=E7=BD=AE=E4=BA=86=20ssl=EF=BC=8C=E8=80=8C?= =?UTF-8?q?=E8=A2=AB=E4=BB=A3=E7=90=86=E7=9A=84=E7=B3=BB=E7=BB=9F=E4=B8=8D?= =?UTF-8?q?=E9=87=87=E7=94=A8=20ssl=20=E7=9A=84=E6=97=B6=E5=80=99=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/config/web/SchemeHttpsConfig.java | 70 +++++++++++++++++++ web/src/main/resources/config/application.yml | 3 + 2 files changed, 73 insertions(+) create mode 100644 modules/core/src/main/java/com/jeesite/modules/config/web/SchemeHttpsConfig.java diff --git a/modules/core/src/main/java/com/jeesite/modules/config/web/SchemeHttpsConfig.java b/modules/core/src/main/java/com/jeesite/modules/config/web/SchemeHttpsConfig.java new file mode 100644 index 00000000..9345108f --- /dev/null +++ b/modules/core/src/main/java/com/jeesite/modules/config/web/SchemeHttpsConfig.java @@ -0,0 +1,70 @@ +/** + * Copyright (c) 2013-Now http://jeesite.com All rights reserved. + */ +package com.jeesite.modules.config.web; + +import java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; + +/** + * 将请求协议转换为 https + * @author ThinkGem + * @version 2020年1月21日 + */ +@Configuration +@ConditionalOnProperty(name="server.schemeHttps", havingValue="true", matchIfMissing=false) +public class SchemeHttpsConfig { + + @Bean + public FilterRegistrationBean schemeFilterRegistrationBean() { + FilterRegistrationBean bean = new FilterRegistrationBean<>(); + bean.setOrder(Ordered.HIGHEST_PRECEDENCE); + bean.setFilter(new Filter() { + + @Override + public void init(FilterConfig filterConfig) throws ServletException {} + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + chain.doFilter(new HttpServletRequestWrapper((HttpServletRequest) request) { + + @Override + public String getScheme() { + return "https"; + } + + @Override + public StringBuffer getRequestURL() { + StringBuffer sb = super.getRequestURL(); + if ("http:".equals(sb.substring(0, 5))){ + return sb.replace(0, 5, "https:"); + }else{ + return sb; + } + } + + }, response); + } + + @Override + public void destroy() {} + }); + bean.addUrlPatterns("/*"); + return bean; + } + +} diff --git a/web/src/main/resources/config/application.yml b/web/src/main/resources/config/application.yml index 60488f01..9a2f3af0 100644 --- a/web/src/main/resources/config/application.yml +++ b/web/src/main/resources/config/application.yml @@ -26,6 +26,9 @@ server: tomcat: uri-encoding: UTF-8 + # 将请求协议转换为 https + schemeHttps: false + #======================================# #========== Database sttings ==========# #======================================#