Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

java - Redirect to a different host in Spring Boot (non-www to www URL)

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.

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:

<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/$1</to>
  </rule>
</urlrewrite>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

The configuration file:

@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();
    }
}

The project structure:

project structure

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>

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...