在 Spring 引导中重定向到不同的主机(非 www 到 www URL)
Redirect to a different host in Spring Boot (non-www to www URL)
我已使用自签名证书配置我的项目,并将不安全的 http 重定向到 https。我还想将请求重定向到没有“www.
”前缀的主机到有前缀的主机,就像我们向 https://google.com its automatically redirected to https://www.google.com.
发出请求一样
现在为了这样做,我找到了一个名为 UrlRewriteFilter but this library has configuration available in XML. I tried to convert the XML configuration to java equivalent one but i had no luck as I couldn't find the java equivalent methods. I tried to convert the configuration by taking reference from this Baeldung resource 的库。下面是基于 XML 的配置。我正在使用带有嵌入式 undertow 服务器的 Spring Boot 1.5.19。请帮忙。
Maven 依赖:
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>4.0.4</version>
</dependency>
web.xml
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
urlrewrite.xml
<urlrewrite>
<rule>
<name>seo redirect</name>
<condition name="host" operator="notequal">^www.csetutorials.com</condition>
<from>^/(.*)</from>
<to type="permanent-redirect" last="true">http://www.csetutorials.com/</to>
</rule>
</urlrewrite>
我必须在这里回答我自己的问题,因为我发现这个库没有 Java 等效配置,因为它最后一次维护是在 2012 年。解决方案是 Java 和 XML 配置。如果您使用反向代理服务器,则可以避免此配置。但是,我想避免这种情况,让单个应用程序服务器来做各种事情。所以这里是:
配置文件:
@Configuration
public class UrlRewriteConfig extends UrlRewriteFilter {
private UrlRewriter urlRewriter;
@Bean
public FilterRegistrationBean tuckeyRegistrationBean() {
final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new UrlRewriteConfig());
return registrationBean;
}
@Override
public void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
try {
ClassPathResource classPathResource = new ClassPathResource("urlrewrite.xml");
InputStream inputStream = classPathResource.getInputStream();
Conf conf1 = new Conf(filterConfig.getServletContext(), inputStream, "urlrewrite.xml", "");
urlRewriter = new UrlRewriter(conf1);
} catch (Exception e) {
throw new ServletException(e);
}
}
@Override
public UrlRewriter getUrlRewriter(ServletRequest request, ServletResponse response, FilterChain chain) {
return urlRewriter;
}
@Override
public void destroyUrlRewriter() {
if (urlRewriter != null)
urlRewriter.destroy();
}
}
项目结构:
和 urlrewrite.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite
PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
"http://www.tuckey.org/res/dtds/urlrewrite3.0.dtd">
<urlrewrite>
<rule>
<name>SEO Redirect and Secure Channel</name>
<condition name="host" operator="equal">^example.com</condition>
<from>^(.*)$</from>
<to type="permanent-redirect">https://www.example.com</to>
</rule>
</urlrewrite>
需要注意的一个非常重要的点,是我必须在 Undertow 服务器配置中删除不安全的 http 到 https 重定向,因为它抛出了一个错误 - "TOO MANY REDIRECTS".所以我所做的是我保持打开两个端口 - 80 和 443 用于不安全和安全连接,并且 tuckey 配置进行各种重定向,从 http 到 https 以及从非 www 到 www。希望对你有帮助。
我已使用自签名证书配置我的项目,并将不安全的 http 重定向到 https。我还想将请求重定向到没有“www.
”前缀的主机到有前缀的主机,就像我们向 https://google.com its automatically redirected to https://www.google.com.
现在为了这样做,我找到了一个名为 UrlRewriteFilter but this library has configuration available in XML. I tried to convert the XML configuration to java equivalent one but i had no luck as I couldn't find the java equivalent methods. I tried to convert the configuration by taking reference from this Baeldung resource 的库。下面是基于 XML 的配置。我正在使用带有嵌入式 undertow 服务器的 Spring Boot 1.5.19。请帮忙。
Maven 依赖:
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>4.0.4</version>
</dependency>
web.xml
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
urlrewrite.xml
<urlrewrite>
<rule>
<name>seo redirect</name>
<condition name="host" operator="notequal">^www.csetutorials.com</condition>
<from>^/(.*)</from>
<to type="permanent-redirect" last="true">http://www.csetutorials.com/</to>
</rule>
</urlrewrite>
我必须在这里回答我自己的问题,因为我发现这个库没有 Java 等效配置,因为它最后一次维护是在 2012 年。解决方案是 Java 和 XML 配置。如果您使用反向代理服务器,则可以避免此配置。但是,我想避免这种情况,让单个应用程序服务器来做各种事情。所以这里是:
配置文件:
@Configuration
public class UrlRewriteConfig extends UrlRewriteFilter {
private UrlRewriter urlRewriter;
@Bean
public FilterRegistrationBean tuckeyRegistrationBean() {
final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new UrlRewriteConfig());
return registrationBean;
}
@Override
public void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
try {
ClassPathResource classPathResource = new ClassPathResource("urlrewrite.xml");
InputStream inputStream = classPathResource.getInputStream();
Conf conf1 = new Conf(filterConfig.getServletContext(), inputStream, "urlrewrite.xml", "");
urlRewriter = new UrlRewriter(conf1);
} catch (Exception e) {
throw new ServletException(e);
}
}
@Override
public UrlRewriter getUrlRewriter(ServletRequest request, ServletResponse response, FilterChain chain) {
return urlRewriter;
}
@Override
public void destroyUrlRewriter() {
if (urlRewriter != null)
urlRewriter.destroy();
}
}
项目结构:
和 urlrewrite.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite
PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
"http://www.tuckey.org/res/dtds/urlrewrite3.0.dtd">
<urlrewrite>
<rule>
<name>SEO Redirect and Secure Channel</name>
<condition name="host" operator="equal">^example.com</condition>
<from>^(.*)$</from>
<to type="permanent-redirect">https://www.example.com</to>
</rule>
</urlrewrite>
需要注意的一个非常重要的点,是我必须在 Undertow 服务器配置中删除不安全的 http 到 https 重定向,因为它抛出了一个错误 - "TOO MANY REDIRECTS".所以我所做的是我保持打开两个端口 - 80 和 443 用于不安全和安全连接,并且 tuckey 配置进行各种重定向,从 http 到 https 以及从非 www 到 www。希望对你有帮助。