[JAVA] Be aware that the default for Spring boot redirect is http

When redirecting with spring boot, the protocol defaults to http.

When using it, it is necessary to be aware of the protocol. Be especially careful if the protocol is different for each environment (development, staging, production).

TestContoroller.java


public class TestController {

    @GetMapping("/show")
    public ModelAndView test() {
        return new ModelAndView("redirect:" + "/sample");
    }
}

In the above case It becomes http: // localhost / sample. If you want to read this from application.yml and set it

TestContoroller.java


@RequiredArgsConstructor
@EnableConfigurationProperties({RedirectUrlProperties.class})
public class TestController {
    private final RedirectUrlProperties redirectUrlProperties;

    @GetMapping("/show")
    public ModelAndView test() {
        return new ModelAndView("redirect:" + buildAdminRedirectUrl("/sample"));
    }

    /*
     *spring redirect is http by default
     *Method to create redirectURL for each environment
     */
    public String buildAdminRedirectUrl(String url) {
        return UriComponentsBuilder.fromUriString(redirectUrlProperties.getUrl() + url).toUriString();
    }
}

application.yml


spring:
  redirect:
    url: https://localhost

RedirectUrlProperties.java


@Getter
@Setter
@ConfigurationProperties(prefix= "spring.redirect")
public class RedirectUrlProperties {
    private String url;
}

So far we've described redirect, but if you just want to hit an internal method, consider using forward. However, since the URL of forward does not change, it is necessary to verify whether it is suitable including that side. (redirect throws the request again, but forward processes it internally, so the request is only the first time)

I referred to the following. https://qiita.com/rubytomato@github/items/8d132dec042f695e50f6

Recommended Posts

Be aware that the default for Spring boot redirect is http
Spring Boot for the first time
Spring.messages.fallback-to-system-locale: false is required to default message.properties for i18n support in Spring boot
[Spring Boot] List of validation rules that can be used in the property file for error messages
Please note that Spring Boot + Tomcat 8.5.8 cannot be used!
The story that the port can no longer be used in the Spring boot sample program
Create a web app that is just right for learning [Spring Boot + Thymeleaf + PostgreSQL]
Change the injection target for each environment with Spring Boot 2
What is the constructor for?
Spring Boot for annotation learning
Is it easy for the user to use when implementing general-purpose functions? Let's be aware of
Sample code that uses the Mustache template engine in Spring Boot
[Spring Boot] Mock the contents that are autowired outside the scope [JUnit 5]
See the relative redirect behavior with the server.tomcat.use-relative-redirects setting in Spring Boot
[Swift] The story that switch is often used for enum judgment
I want to control the default error message of Spring Boot
[Spring Boot] Until @Autowired is run in the test class [JUnit5]
Frequent annotations for Spring Boot tests
What is @Autowired in Spring boot?
Use DBUnit for Spring Boot test
A memo that touched Spring Boot
Spring AOP for the first time
I used Docker to solidify the template to be developed with spring boot.
[For internal use] For those assigned to the Spring Boot project (under construction)