在Spring Boot中重定向到其他主机(非www到www URL) [英] Redirect to a different host in Spring Boot (non-www to www URL)
问题描述
我已为我的项目配置了自签名证书,并已配置为将不安全的http重定向到https.我还想将请求重定向到没有前缀"www.
"的主机,例如当我们向 https://google.com 其自动重定向到 https://www.google.com .
I have configured my project with a self signed certificate and have configured to redirect insecure http to https. I also want to redirect a request to a host without a "www.
" prefix to a host that does, like when we make a request to https://google.com its automatically redirected to https://www.google.com.
现在,为了这样做,我找到了一个名为 UrlRewriteFilter 的库,但是该库具有配置以XML提供.我试图将XML配置转换为Java等效方法,但是由于找不到Java等效方法而没有运气.我尝试通过参考此Baeldung资源进行转换.以下是基于XML的配置.我正在将Spring Boot 1.5.19与嵌入式underwow服务器一起使用.请帮忙.
Now in order to do so, I have found a library called 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. Below is the XML based configuration. I am using Spring Boot 1.5.19 with embedded undertow server. Please help.
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.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/$1</to>
</rule>
</urlrewrite>
推荐答案
我不得不在这里回答我自己的问题,因为我发现该库没有Java等效配置,因为它上一次维护于2012年.是Java和XML配置的混合.如果使用反向代理服务器,则可以避免此配置.但是,我想避免这种情况,而是让单个应用程序服务器来做各种事情.这样就可以了:
I have to answer my own question here as I found out that there is no Java equivalent configuration for this library, as it was last maintained in 2012. The solution is a mix of Java and XML configuration. This configuration can be avoided if you use a reverse proxy server. However, I wanted to avoid that and have the single application server to do all sorts of things. So here it goes:
配置文件:
@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文件:
And the urlrewrite.xml file:
<?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$1</to>
</rule>
</urlrewrite>
一个非常重要的要点,是我不得不删除Undertow Server配置中不安全的http到https重定向,因为它引发了错误-"TOO MANY REDIRECTS".所以我要做的是我保持了两个端口的打开状态-80和443用于不安全的连接,并且tuckey配置进行了各种重定向,从http到https和从非www到www.希望对您有所帮助.
A very important point to be noted, is that I had to remove my insecure http to https redirection in the Undertow Server configuration as it threw an error - "TOO MANY REDIRECTS". So what i did is I kept two ports opened - 80 and 443 for insecure and secure connections and the tuckey configuration does the all sorts of redirection, from http to https and from non-www to www. I hope it helps.
这篇关于在Spring Boot中重定向到其他主机(非www到www URL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!