2011-02-25 2 views
6

JAX-RS 엔드 포인트의 보안을 설정하려고하며 현재 인증 및 권한 부여 작동 방식을 파악하려고합니다. 대부분의 예제는 web.xml을 통해 Java EE App-Server 역할에 피기 백 (piggyback)하기 때문에 매우 간단합니다.JAX-RS 및 사용자 정의 인증

Java EE AS ​​역할 이외의 다른 용도로 사용하는 방법이 궁금합니다. 예 : 세션 또는 일종의 토큰 (또는 일종의 식별자)을 사용하고 싶습니다.

답변

7

모든 것은 사용중인 JAX-RS 구현에 따라 다릅니다. 내장 된에 Jersey을 사용하고 있습니다. Jetty 당신이 HttpServletRequestPrincipal이 있으면

SecurityHandler sh = new SecurityHandler(); 

// the UserRealm is the collection of users, and a mechanism to determine if 
// provided credentials are valid 
sh.setUserRealm(new MyUserRealm()); 

// the Authenticator is a strategy for extracting authentication credentials 
// from the request. BasicAuthenticator uses HTTP Basic Auth 
sh.setAuthenticator(new BasicAuthenticator()); 

당신이 JAX-RS 요청의 컨텍스트에이를 주입 할 수

How to Configure Security with Embedded Jetty를 참조하십시오.

public abstract class AbstractResource { 
    private Principal principal; 
    @Context 
    public void setSecurityContext(SecurityContext context) { 
     principal = context.getUserPrincipal(); 
    } 
    protected Principal getPrincipal() { 
     return principal; 
    } 
} 

@Path("/some/path") 
public class MyResource extends AbstractResource { 
    @GET 
    public Object get() { 
     Principal user = this.getPrincipal(); 
     // etc 
    } 
} 
2

면책 조항 : 정말로, 정말로 필요한 경우가 아니면 자신의 보안 프레임 워크를 사용하지 마십시오.

저지의 OAuth filter은 무엇을 참조하십시오. 정상적으로 이해되는 형식 (HTTP Basic)과 다른 형식으로 자격 증명을 보유하는 Authorization 헤더를 읽습니다. 이러한 자격 증명을 역할로 변환하여 실제 적용을 수행하는 Roles Allowed Filter을 추가하면 보안을 구현하는 데 사용할 수 있습니다 (@RolesAllowed). 필터가 어떻게 작동하는지 살펴보십시오.

관련 문제