2012-01-25 2 views
3

고객 테이블을 기반으로 HTTP 인증을 사용하고 있습니다. 사용자가 인증 된 후 편안한 웹 서비스가 호출됩니다. 하지만 웹 서비스에서 HTTP 인증 (HttpRequest의 헤더 데이터)에 어떻게 액세스 할 수 있습니까? 내 코드는 다음과 같습니다Glassfish 3.1.1 : RESTful 웹 서비스에서 HTTP 인증 검색

@GET 
@Path("{id}") 
@Produces({"application/xml"}) 
public ObjectList read(@PathParam("id") Integer id) { 
... //how to get here the HTTP-Username and Password? 
} 

답변

3

Principal 및 역할을 얻기 위해, 클래스 본문 또는에 @Context SecurityContext를 주입 메소드의 입력 파라미터

import javax.ws.rs.core; 
// 
public ObjectList read(
    @PathParam("id") Integer id, 
    @Context SecurityContext sc) { 
    String principalUserName = sc.getUserPrincipal().getName(); 
    if (sc.isUserInRole("MyRole")) { 
     return new MyRoleResource(); 
    } else { 
     return new MyDefaultRoleResource(); 
    } 
} 
0

이 같이 당신의 방법에 더 많은 매개 변수를 추가

import javax.ws.rs.HeaderParam; 

// ... 

public ObjectList read(
    @PathParam("id") Integer id, 
    @HeaderParam("user-agent") String userAgent, 
    @HeaderParam("X-auth-token") String authToken) ... 
관련 문제