2013-09-30 2 views
2

HTTP 기본 인증으로 보안 된 리소스가 필요한 사소한 REST 앱이 있습니다. 나는이 앱의 클라이언트가 할 수있는 것에 대한 응답으로 컨테이너가 HTML을 생성하는 것을보고 싶지 않습니다.스프링 보안에 대한 컨테이너 응답 무시 BadCredentialsException

<http pattern="/api/**" auto-config="true" create-session="stateless" entry-point-ref="entryPoint"> 
    <intercept-url pattern="/**" method="PUT" access="ROLE_ADMIN" /> 
    <intercept-url pattern="/**" method="POST" access="ROLE_ADMIN" /> 
    <intercept-url pattern="/**" method="DELETE" access="ROLE_ADMIN" /> 
    <http-basic /> 
</http> 

<http security="none" /> 

<authentication-manager> 
    <authentication-provider> 
     <user-service id="userDetailsService" 
      properties="classpath:META-INF/spring/users.properties"/> 
    </authentication-provider> 
</authentication-manager> 

내에서는 EntryPoint 클래스는 다음과 같습니다 : 그래서 이런 내 SpringSecurity 설정을 가지고 내가 보호 된 자원/방법의 조합을 명중 할 때

public class BasicJsonEntryPoint extends BasicAuthenticationEntryPoint { 

    @Override 
    public void commence(
     HttpServletRequest request, 
     HttpServletResponse response, 
     AuthenticationException authException 
    ) throws IOException, ServletException { 

     response.addHeader("WWW-Authenticate", "Basic realm=\"" + getRealmName() + "\""); 
     response.setStatus(SC_UNAUTHORIZED); 
     PrintWriter writer = response.getWriter(); 
     writer.println("{\"status\":" + SC_UNAUTHORIZED + ", \"message\":\"" + authException.getMessage() + "\"}"); 

    } 
} 

이 작동하고 내가 좋아하는 뭔가를 얻을 다음과 같은;

HTTP/1.1 401 Unauthorized 
Server: Apache-Coyote/1.1 
WWW-Authenticate: Basic realm="Admin" 
Content-Length: 84 
Date: Sun, 29 Sep 2013 11:50:48 GMT 

{"status":401, "message":"Full authentication is required to access this resource"} 

.. 정확히 원하는 것입니다. 나는 권한 부여에 잘못된 자격 증명을 제공하는 경우에는 ...

HTTP/1.1 401 Unauthorized 
Server: Apache-Coyote/1.1 
WWW-Authenticate: Basic realm="Admin" 
Content-Type: text/html;charset=utf-8 
Content-Length: 1019 
Date: Sun, 29 Sep 2013 11:51:14 GMT 

<html><head><title>Apache Tomcat/7.0.33 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 401 - Invalid basic authentication token</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Invalid basic authentication token</u></p><p><b>description</b> <u>This request requires HTTP authentication.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.33</h3></body></html> 

다른 내가/우선 여기에 같은 JSON 응답을 얻기 위해 구현해야 할 (이상적으로 사용하여 HTTP 헤더, 나는 Tomcat의 HTML 응답을 얻을 내가 이미 만든 동일한 수업)?

많은 감사,

답변

2

문제는 대신 구현 BasicAuthenticationEntryPoint의 그것의 자신의 인스턴스를 사용하는 BasicAuthenticationFilter에 있습니다. 따라서 조금 비틀 수 있습니다.

<http auto-config="true" create-session="stateless"> 
    <intercept-url .../> 
    <http-basic /> 
    <custom-filter ref="basicAuthenticationFilter" before="BASIC_AUTH_FILTER" /> 
</http> 

<beans:bean id="basicAuthenticationFilter" 
     class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter"> 
    <beans:property name="authenticationManager" ref="authenticationManager" /> 
    <beans:property name="authenticationEntryPoint" ref="entryPoint" /> 
</beans:bean> 

<beans:bean id="entryPoint" class="mypackage.BasicJsonEntryPoint"> 
    <beans:property name="realmName" value="realm"/> 
</beans:bean> 

<authentication-manager alias="authenticationManager"> 
    <authentication-provider> 
     ... 
    </authentication-provider> 
</authentication-manager> 
+0

감사합니다. 나는 신임장이없는 원래의 요청이 여전히 작동하기 위해서 엘리먼트에 [entry-point-ref = "entryPoint"]를 유지해야 할 필요가 있음을 발견했다. – darrend

관련 문제