2012-11-07 6 views
2

일부 사용자 지정 헤더 매개 변수를 HTTP 303 (리디렉션) 응답에 추가하려고합니다. 그러나 새로운 헤더가 응답에서 제거되는 것 같습니다.JAX-RS 사용자 지정 헤더가 응답 리디렉션에 추가되지 않음

이 코드는 요청을 수신하는 것을 의미하고, HTTP 303 응답을 반환합니다 :

@POST 
@Path("/authorize") 
@Produces("application/x-www-form-urlencoded") 
public Response getOAuthGrant(@HeaderParam(OAuth2.AUTHORIZATION) @DefaultValue("") String authorization, 
           @HeaderParam(OAuth2.CLIENT_ID)  @DefaultValue("") String clientId, 
           @HeaderParam(OAuth2.CLIENT_SECRET) @DefaultValue("") String clientSecret, 
           @HeaderParam(OAuth2.GRANT_TYPE)  @DefaultValue("") String grantType) throws InternalServerException, UnauthorizedException { 

     OAuth2.validateGrantRequest(clientId, clientSecret, authorization, grantType); 

     ApiTokenV2 apiTokenV2 = new ApiTokenV2(); 

     try { 
      apiTokenV2 = TokenManager.getApiToken(clientId); 

      if (apiTokenV2 != null) { 
       apiTokenV2.generateAccessGrant(clientId); 
      } else { 
       logger.error("Error in TokenEndpoint. Retrieved token is null."); 
       throw new InternalServerException("A server error occurred while trying to authorize the requester. Could not find 'generateAccessGrant' method"); 
      } 
     } catch (NamingException e) { 
      throw new InternalServerException("A server error occurred while trying to authorize grant request. Could not find 'generateAccessGrant' method.", e); 
     } 

     return Response.status(Response.Status.SEE_OTHER) 
         .type(MediaType.APPLICATION_FORM_URLENCODED_TYPE) 
         .header("Location", "/api/token") 
         .header("Authorization", "OAuth") 
         .header("Access-Grant", apiTokenV2.getAccessGrant()) 
         .build(); 
} 

내가 잘못 여기서 뭐하는 거지를? 대신 @Context을 사용해야합니까?

+0

사용 '@ Produces' 어노테이션. 귀하의 방법은 어떤 응답 엔터티를 생성합니까? –

답변

7

당신이하려는 것은 효과가 있습니다.

% curl -v -X POST http://localhost:8080/WebApplication1/rest/console/authorize 
* About to connect() to localhost port 8080 (#0) 
* Trying ::1... connected 
> POST /WebApplication1/rest/console/authorize HTTP/1.1 
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3 
> Host: localhost:8080 
> Accept: */* 
> 
< HTTP/1.1 303 See Other 
< X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2.2 Java/Oracle Corporation/1.7) 
< Server: GlassFish Server Open Source Edition 3.1.2.2 
< Location: /api/token 
< X-Foo: bar 
< Content-Length: 0 
< Date: Wed, 07 Nov 2012 08:20:24 GMT 
< 
* Connection #0 to host localhost left intact 
* Closing connection #0 

당신이 볼 수 있듯이, 응답 코드가

303 See Other 

하고 interesing 헤더가 설정됩니다 :

@POST 
@Path("/authorize") 
public Response authorize() { 
    return Response.status(Response.Status.SEE_OTHER) 
      .header(HttpHeaders.LOCATION, "/api/token") 
      .header("X-Foo", "bar") 
      .build(); 
} 

curl로 실행

Location: /api/token 
X-Foo: bar 
+4

+1,'HttpHeaders.LOCATION' 상수를 사용하는 것이 좋습니다. – yegor256

+0

@ yegor256 감사합니다. –

관련 문제