2013-12-08 4 views
2

CXF 기반의 나머지 API를 보호하려고합니다. 내 구성이 기술적으로 작동하는 동안 401에서 HTML 메시지가 아닌 JSON으로 응답하는 API를 얻을 수없는 것 같습니다. 몇 가지 다른 게시물을 기반으로 봄 보안 구성을 위해 groovy를 사용하여 다음 Java 구성을 함께 작성했습니다.Java 보안 설정을 사용하여 Rest API에 대한 스프링 보안 HTTP 기본 인증

@Configuration 
@EnableWebSecurity 
@Slf4j 
class SecurityConfig extends WebSecurityConfigurerAdapter { 

    protected void configure(HttpSecurity http) { 
     http.antMatcher('/api/**') 
       .authorizeRequests() 
       .antMatchers('/api/admin/**').hasRole('ADMIN') 
       .antMatchers('/api/**').hasRole('USER') 
       .and() 
       .httpBasic() 
       .and() 
       .addFilterBefore(
        new BasicAuthenticationFilter(authenticationManager: authenticationManager(), authenticationEntryPoint: new BasicJsonEntryPoint(realmName: 'Local')), 
        BasicAuthenticationFilter.class 
       ) 
    } 

    static class BasicJsonEntryPoint extends BasicAuthenticationEntryPoint { 

     @Override 
     public void commence(HttpServletRequest req, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException { 
      log.debug 'Handling response' 
      response.addHeader HttpHeaders.WWW_AUTHENTICATE, /Basic realm="${getRealmName()}"/ 
      response.setStatus HttpStatus.UNAUTHORIZED.value() 
      response.getWriter().println([status: HttpStatus.UNAUTHORIZED.value(), message: e.getMessage()].toJson()) 
     } 
    } 
} 

나는이 일반적인 접근 방식에 대해 여러 가지 변형을 시도했지만, API에서 HTML을 다시 얻었습니다. 다음과 같은 필수/RESP 참조 :

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/> 
     <title>Error 401 Full authentication is required to access this resource</title> 
    </head> 
    <body> 
     <h2>HTTP ERROR 401</h2> 
     <p>Problem accessing /api/test. Reason: 

      <pre> Full authentication is required to access this resource</pre> 
     </p> 
     <hr /> 
     <i> 
      <small>Powered by Jetty://</small> 
     </i> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
    </body> 
</html> 

답변

0

난 그냥 여기 추측하고있어,하지만 응용 프로그램이 200S 이외의 HTTP 응답 코드를 방출 때때로 때 부두는 너무 도움이됩니다. 제 추천은 부두의 유용성을 줄이기 위해 web.xml에 약간의 로직을 추가하는 것입니다. 유사한 문제에서 내 응용 프로그램을 얻은 전체 기술은 How do I suppress Jetty 8's default ErrorHandler?

행운을 빕니다.

+0

이 문제는 해결되지 않습니다. 나는 방파제, 톰캣, 언더 토우 (Undertow)와 함께 노력했다. 같은 결과. – Todd

관련 문제