2017-06-24 9 views
1

.AND()requiresChannelを()リダイレクトする必要がありanyRequest()requiresSecure()春のMVC:。。。セキュリティ設定requireSecure()恒久的に301

私は上記の構成でHTTPSを強制していますが、問題は一時的にリダイレクトユーザーです(302)。私はここで

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private MyAuthenticationProvider _auth; 

    @Override 
    public void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(_auth); 
    } 

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


     http.headers().disable().authorizeRequests() 
       .someCodehere. 
       .and().requiresChannel().antMatchers("/ping").requiresInsecure() 
       .and().requiresChannel().anyRequest().requiresSecure() 
       .and().csrf().disable(); 

    } 
} 

答えて

1

が私のためにどのような作品であるHTTPステータスコードが301(永久的なリダイレクト)する必要がありますしたいです。

最初にステータス301を使用し、周囲にSecureChannelProcessorを使用しているRedirectStrategyを定義します。そして、このSecureChannelProcessorで設定

final RedirectStrategy redirectStrategy = new RedirectStrategy() { 
    @Override 
    public void sendRedirect(HttpServletRequest request, HttpServletResponse response, 
      String url) throws IOException { 
     response.addHeader("Location", response.encodeRedirectURL(url)); 
     response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); 
     response.flushBuffer(); 
    } 
}; 

final RetryWithHttpsEntryPoint entryPoint = new RetryWithHttpsEntryPoint(); 
entryPoint.setRedirectStrategy(redirectStrategy); 

final SecureChannelProcessor secureChannelProcessor = new SecureChannelProcessor(); 
secureChannelProcessor.setEntryPoint(entryPoint); 

:これは

.requiresChannel().anyRequest().requiresSecure()仕組み

http.requiresChannel().anyRequest().requiresSecure().channelProcessors(Arrays.asList(secureChannelProcessor)).and() 
... other configuration 

は、順番にRedirectStrategyを有し、RetryWithHttpsEntryPointを持っているSecureChannelProcessorインスタンスを追加します。リダイレクション戦略を変更するには、エントリポイントとプロセッサオブジェクトを構築する必要があります。

応答がコミットされない限り、Springがリクエストの処理を続けるため、リダイレクトを行うときにはresponse.flushBuffer()が必要です。 flushBuffer()が正しいことを確認します。

リダイレクト時には、PortMapperを使用してポートを変更します。定義していない場合は、80から443と8080から8443に変更されるデフォルトのものが使用されます。ポートが異なる場合は、PortMapperも設定してRetryWithHttpsEntryPointインスタンスに注入する必要があります。

InsecureChannelProcessorについてはすべて似ています。

+0

これに必要なmavenの依存関係はありますか? 'org.springframework.beans.factory.BeanCreationException:名前が 'org.spri[email protected]41921212'のBeanを作成中にエラーが発生しました:initメソッドの呼び出しに失敗しました。ネストされた例外はjava.lang.IllegalArgumentExceptionです:サポートされていない設定属性:[REQUIRES_INSECURE_CHANNEL] ' –

+0

質問に完全なスタックトレースを追加してください –

+0

' InsecureChannelProcessor'を 'requiresSecure()'に偶然追加しませんか? –

関連する問題