新增 scheme 强制替换为 https 的过滤器,用于当 nginx 配置了 ssl,而被代理的系统不采用 ssl 的时候使用。

This commit is contained in:
thinkgem
2020-01-21 10:34:36 +08:00
parent 62df63900d
commit 47d3d5bd78
2 changed files with 73 additions and 0 deletions

View File

@@ -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<Filter> schemeFilterRegistrationBean() {
FilterRegistrationBean<Filter> 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;
}
}

View File

@@ -26,6 +26,9 @@ server:
tomcat:
uri-encoding: UTF-8
# 将请求协议转换为 https
schemeHttps: false
#======================================#
#========== Database sttings ==========#
#======================================#