2017-04-20 3 views
1

私はSpring SecurityのSpring Bootアプリケーションを持っています。新しいエンドポイント/healthは、基本的なHTTP認証でアクセスできるように設定する必要があります。次のように現在のHttpSecurity構成は次のとおりです。春のセキュリティを備えた特定のエンドポイントにHTTP基本認証を追加するにはどうすればよいですか?

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

http.requestMatchers() 
    .antMatchers(HttpMethod.OPTIONS, "/**") 
    .and() 
    .csrf() 
    .disable() 
    .authorizeRequests() 
    .anyRequest() 
    .permitAll() 
    .and() 
    .sessionManagement() 
    .sessionCreationPolicy(SessionCreationPolicy.STATELESS); 

}

私は/healthのベース認証を追加するにはどうすればよいですか?私はこのような何かを必要と理解し、私はこれは完全に正しいとは思わない、と正確にそれを追加する場所を私は本当に理解していない:

.authorizeRequests() 
    .antMatchers(
     // Health status 
     "/health", 
     "/health/" 
    ) 
    .hasRole(HEALTH_CHECK_ROLE) 
    .and() 
    .httpBasic() 
    .realmName(REALM_NAME) 
    .authenticationEntryPoint(getBasicAuthEntryPoint()) 
    .and() 
    .sessionManagement() 
    .sessionCreationPolicy(SessionCreationPolicy.STATELESS) 

私はこれらのリソースが役立つことが判明したが、十分ではありません:

+0

'/ health/**'で試しましたか? –

答えて

0
@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .csrf().disable() 
      .authorizeRequests() 
      .antMatchers("/health/**").hasRole("SOME_ROLE") 
      .anyRequest().authenticated() 
      .and() 
      .httpBasic() 
      .and() 
      .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 
    } 


    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 


     auth 
      .inMemoryAuthentication() 
      .withUser("yourusername").password("yourpassword").roles("SOME_ROLE") 

     ; 
    } 

} 
+0

いただきありがとうございます。しかし、問題は既存の機能を保持しなければならないということです。私が今までに得た最も近いものは、ここから参照されているコードに従っています:http://stackoverflow.com/questions/18815015/creating-multiple-http-sections-in-spring-security-java-config ie異なるアダプタクラスを複数作成するエンドポイント。事態をさらに複雑にするために、継承したコードにはOAuthを使用する別の認証メカニズムが含まれています。これは引き続き機能する必要があります。私は引き続きドキュメントを読む必要があると思います。 – dave

関連する問題