2016-06-15 2 views
1

Spring Boot 1.3.3 응용 프로그램에서 Spring Security 4.0.3 사용. "API" 지내는 API 및 "UI" 웹 기반 사용되는 인터페이스 (Thymeleaf + 스프링 웹 MVC) :Spring Security는 보안되지 않은 URL에 대해 401을 반환합니다.

응용 프로그램은 HTTP 내용의 두 가지 유형이 있습니다.

응용 프로그램의 REST API의 대부분의 끝점은 Basic을 사용하여 보안 설정되지만 일부는 이 아니며 반드시을 사용할 수 있어야합니다. 예상대로 보안 엔드 포인트 작업에

 // In one method, the security config for the "API" 
     httpSecurity 
       .antMatcher("/api/**") 
       .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER) 
       .and() 
       .authorizeRequests() 
       .antMatchers("/api/ping").permitAll() 
       .antMatchers("/api/**").hasRole("USER") 
       .and() 
       .httpBasic(); 

     // In another method, the security config for the "UI" 
     httpSecurity 
       .authorizeRequests() 
       .antMatchers("/ui/", "/ui/index.html", "/ui/css/*", "/ui/js/*", "/ui/img/*").permitAll() 
       .antMatchers("/ui/user/**").hasRole("USER") 
       .antMatchers("/ui/**").denyAll() 
       .and() 
       .formLogin().loginPage("/ui/login.html").permitAll().failureUrl("/ui/login.html").defaultSuccessUrl("/ui/user/main.html") 
       .and() 
       .logout().logoutUrl("/ui/logout").permitAll().logoutSuccessUrl("/ui/login.html") 
       .and() 
       .httpBasic(); 

가에 액세스 : 같은

단순화 된 구성을 보인다.

".../api/ping"과 같은 공용 끝점에 대한 액세스는 사용자가 유효하지 않은 기본 인증을 제공 한 경우 401으로 실패합니다. 물론 이러한 엔드 포인트는 유효하지 않거나 유효한 기본 인증이 제공 될 때 잘 작동합니다.

스프링 보안의 401은 놀랍습니다. 선택된 엔드 포인트에 대해 401이나 403을 반환하지 않는 스프링 보안 구성을 어떻게 구현할 수 있습니까?

감사합니다.

업데이트 1은 :

답변

0

주문이 중요하다 "UI"존재와 보안 설정에 대한 정보를 추가했습니다. 첫째 대부분의 특정 규칙 (경로) : 경기 antMatcher("/api/**")에 같이있는 경우

httpSecurity 
    .antMatchers("/api/ping").permitAll() 
    // and then the rest 

이, 봄 보안 나중에 규칙을 평가하지 않기 때문.

+0

먼저 "UI"에 대한 세부 정보가 추가되었습니다. 그런데 안타깝게도 초기의 "andMatcher ('/ api/**')가 스프링 보안 설정의 두 부분이 분리되어 있어야하기 때문에 제안서는 여기서 작동하지 않습니다. –

관련 문제