2015-07-20 69 views
0

我移動了一些我的應用程序並創建了單獨的模塊,但現在由於某種原因,當我嘗試啓用HTTPS時,它將請求發送到無限重定向循環...當在彈簧安全中配置安全通道時循環重定向

有人可能會建議爲什麼這個請求http://myhost/login會發出重定向。這是我認爲是相關的配置。請注意,如果我拿出requiresChannel部分,它工作正常。

@Override 
protected void configure(HttpSecurity http) throws Exception { 

    http 
      //.addFilterBefore(systemAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class) 
      .addFilter(systemAuthenticationFilter()) 
      .addFilter(new RememberMeAuthenticationFilter(authenticationManager(), rememberMeService())) 
      .authorizeRequests() 
      .antMatchers("/login","/welcome", "/login/new**", "/register", "/logout", "/**", "/session/timeout", "/admin/assets/**").permitAll() 
      .antMatchers("/my_account").hasRole("REGISTERED_CUSTOMER") 
      .anyRequest().permitAll() 
      .and() 
      .formLogin() 
      .failureHandler(exceptionMappingAuthenticationFailureHandler()) 
      .loginPage("/login") 
      .loginProcessingUrl("/log_in") 
      .defaultSuccessUrl("/welcome") 
      .usernameParameter("username") 
      .passwordParameter("password") 

      .and() 
      .logout() 
      .logoutUrl("/logout") 
      .logoutSuccessUrl("/login") 
      .deleteCookies("SPRING_SECURITY_REMEMBER_ME_COOKIE") 


      .and() 
      .requiresChannel() 
      .antMatchers("/my_account", "/login").requiresSecure() 
      .and() 
      .rememberMe() 
      .tokenValiditySeconds(1209600) 
      .key(env.getProperty("rememberme.key")) 

    ; 

} 

哦也,此模塊,從web.xml中servlet上下文的相關部分:

<servlet-mapping> 
     <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <filter> 
     <filter-name>encoding-filter</filter-name> 
     <filter-class> 
      org.springframework.web.filter.CharacterEncodingFilter 
     </filter-class> 
     <init-param> 
      <param-name>encoding</param-name> 
      <param-value>UTF-8</param-value> 
     </init-param> 
     <init-param> 
      <param-name>forceEncoding</param-name> 
      <param-value>true</param-value> 
     </init-param> 
    </filter> 
    <filter-mapping> 
     <filter-name>encoding-filter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 


    <filter> 
     <display-name>springMultipartFilter</display-name> 
     <filter-name>springMultipartFilter</filter-name> 
     <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>springMultipartFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <filter> 
     <display-name>springSecurityFilterChain</display-name> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
     <dispatcher>ERROR</dispatcher> 
     <dispatcher>FORWARD</dispatcher> 
     <dispatcher>REQUEST</dispatcher> 
    </filter-mapping> 

回答

0

我不知道你的情況是一樣的,但我已經遇到了類似的在Pivotal Cloud Foundry部署我的Spring Boot應用程序時出現問題。看來,PaaS代理服務器將https重定向到http。添加一對夫婦在我application.properties行的固定問題:

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

我已經在博客它here,如果有幫助。

0

我今天再次在這個問題上花時間。事實證明,當我重新部署應用程序時,我更改了HTTP端口,以便可以運行舊版本。

在Tomcat配置有一個在連接器本節:

<Connector executor="tomcatThreadPool" 
      port="8080" protocol="HTTP/1.1" 
      connectionTimeout="20000" 
      redirectPort="8443" URIEncoding="UTF-8" /> 

我切換回原來的端口8080和它的正常工作。我不確定爲什麼Tomcat設置很重要,但是如果我使用的是不同於此處配置的HTTP端口的Spring端口,則Spring Security將重定向到原始HTTP端口。