복붙노트

[SPRING] Spring MVC의 "redirect :"접두사는 항상 http로 리다이렉트됩니다 - https에 머물게하려면 어떻게해야합니까?

SPRING

Spring MVC의 "redirect :"접두사는 항상 http로 리다이렉트됩니다 - https에 머물게하려면 어떻게해야합니까?

나는 이것을 나 자신으로 해결했다. 그러나 나는 그토록 단순한 해결책을 발견하는 데 오랜 시간을 보냈다. 나는 여기에 문서화 될 가치가 있다고 생각했다.

InternalResourceViewResolver와 함께 전형적인 Spring 3 MVC 셋업을 가지고있다 :

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/" />
  <property name="suffix" value=".jsp" />
</bean>

내 컨트롤러에 꽤 간단한 핸들러 메서드가 있지만이 예제에서는 더 단순화했습니다.

@RequestMapping("/groups")
public String selectGroup() {
    return "redirect:/";
}

문제는 https://my.domain.com/groups로 이동하면 리디렉션 후에 http://my.domain.com/에서 끝납니다. 실제로로드 밸런서는 모든 HTTP 요청을 https로 리디렉션하지만 이러한 경고가 켜져있는 사람들을 위해 "안전한 연결을 떠날 / 입력하고 있습니다"유형의 여러 브라우저 경고를 발생시킵니다.

그래서 질문은 : 원래 요청이 사용했을 때 스프링이 https로 리디렉션되는 방법은 무엇입니까?

해결법

  1. ==============================

    1.간단한 대답은 InternalResourceViewResolver의 redirectHttp10Compatible 속성을 false로 설정하는 것입니다.

    간단한 대답은 InternalResourceViewResolver의 redirectHttp10Compatible 속성을 false로 설정하는 것입니다.

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
      <property name="prefix" value="/" />
      <property name="suffix" value=".jsp" />
      <property name="redirectHttp10Compatible" value="false" />
    </bean>
    

    핸들러 메소드가 String 대신 View를 반환하도록하고, RedirectView를 직접 생성하고 setHttp10Compatible (false)를 호출하여 요청별로 처리 할 수 ​​있습니다.

    (RedirectView가 HTTP 1.0 호환 리디렉션에 사용하는 HttpServletResponse.sendRedirect는 범인이지만, 서블릿 컨테이너의 구현 (?)에 따라 다르다는 것을 알 수 있습니다. Tomcat과 Jetty에서 모두 문제가 발생했습니다. )

  2. ==============================

    2.Spring 부트는 프록시 서버를 사용하고 있다면이 두 가지 속성을 application.properties 파일에 추가하기 만하면이 멋진 구성 기반 솔루션을 제공합니다.

    Spring 부트는 프록시 서버를 사용하고 있다면이 두 가지 속성을 application.properties 파일에 추가하기 만하면이 멋진 구성 기반 솔루션을 제공합니다.

    server.tomcat.remote_ip_header=x-forwarded-for
    server.tomcat.protocol_header=x-forwarded-proto
    

    이것은 나에게 스프링 바운드 샘플 -web-ui를 https로드 밸런서 뒤에있는 Elastic Beanstalk에 배포합니다. 이러한 프로퍼티가 없으면 MessageController 라인 68의 리다이렉션은 기본적으로 http와 hangs입니다.

    희망이 도움이됩니다.

  3. ==============================

    3.확실해?

    확실해?

    코드를 보면 차이가없는 것으로 보입니다. 두 변형 모두 response.encodeRedirectURL (targetUrl)을 호출합니다 (RedirectView의 388 줄 참조).

    if (http10Compatible) {
        // Always send status code 302.
        response.sendRedirect(response.encodeRedirectURL(targetUrl));
    }
    else {
        HttpStatus statusCode = getHttp11StatusCode(request, response, targetUrl);
        response.setStatus(statusCode.value());
        response.setHeader("Location", response.encodeRedirectURL(targetUrl));
    }
    

    나는 같은 문제가 있었지만로드 밸런서 뒤에 바람둥이를 설치함으로써 촉발되었다. 로드 밸런서는 SSL 핸드 셰이크를 수행하고 일반 HTTP 연결을 Tomcat에 전달합니다.

    해결책은 Loadbalancer에 특별한 Http Header를 보내서 Tomcat이이 연결을 "신뢰"할 수있게하는 것입니다. 서블릿 필터를 사용하면 response.isSecure 플래그를 설정해야합니다. 그런 다음 redirectView를 덮어 써서 response.isSecure가 올바른지 확인하십시오.

    나는 그것이 문제를 일으키는 지 확실하지 않기 때문에이 해결책을 짧게 유지했다.

  4. ==============================

    4.나를 위해 일한 것은 이것을 application.properties에 추가하는 것입니다. server.tomcat.use-relative-redirects = true

    나를 위해 일한 것은 이것을 application.properties에 추가하는 것입니다. server.tomcat.use-relative-redirects = true

    그래서 당신은 :

    public function redirect() {
      return "redirect:/"
    }
    

    server.tomcat.use-relative-redirects가 없으면 http://my-host.com/와 같은 Location 헤더가 추가됩니다. server.tomcat.use-relative-redirects를 사용하면 다음과 같이 표시됩니다. 따라서 브라우저 관점에서 현재 페이지와 관련됩니다.

  5. ==============================

    5.springmvc를 사용하는 경우 다음을 시도 할 수 있습니다.

    springmvc를 사용하는 경우 다음을 시도 할 수 있습니다.

    modelAndView.setView(new RedirectView("redirect url path", true, false));
    
  6. from https://stackoverflow.com/questions/3401113/spring-mvc-redirect-prefix-always-redirects-to-http-how-do-i-make-it-stay by cc-by-sa and MIT license