2017-10-06 1 views
5

내가이 자원이 말 : 특정 (테스트) 사용자가 엔드 포인트로부터 응답을 얻을Shiro : @RequiresRoles로 보호되는 엔드 포인트에 대한 테스트를 작성하는 방법은 무엇입니까?

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Response; 

import org.apache.shiro.authz.annotation.RequiresAuthentication; 
import org.apache.shiro.authz.annotation.RequiresRoles; 

import io.swagger.annotations.Api; 
import io.swagger.annotations.ApiOperation; 

@Path("/authhello") 
@Api(value = "hello", description = "Simple endpoints for testing api authentification", 
    hidden = true) 
@Produces(MediaType.APPLICATION_JSON) 
@RequiresAuthentication 
public class AuthenticatedHelloWorldResource { 

    private static final String READ = "READ"; 
    private static final String WRITE = "WRITE"; 

    @GET 
    @ApiOperation(value = "helloworld", 
     notes = "Simple hello world.", 
     response = String.class) 
    @RequiresRoles(READ) 
    public Response helloWorld() { 
    String hello = "Hello world!"; 
    return Response.status(Response.Status.OK).entity(hello).build(); 
    } 

    @GET 
    @Path("/{param}") 
    @ApiOperation(value = "helloReply", 
     notes = "Returns Hello you! and {param}", 
     response = String.class) 
    @RequiresRoles(WRITE) 
    public Response getMsg(@PathParam("param") String msg) { 
    String output = "Hello you! " + msg; 
    return Response.status(Response.Status.OK).entity(output).build(); 
    } 
} 

내가 확인 테스트를 작성해야, 그리고 특정 사용자가하지? 그리고 만약 그렇다면 어떻게 테스트를 작성할 수 있습니까? 나는 이런 식으로 뭔가를 시도했다 :

import javax.ws.rs.core.Application; 

import org.glassfish.jersey.server.ResourceConfig; 
import org.junit.Test; 

import com.cognite.api.shiro.AbstractShiroTest; 

import static org.junit.Assert.assertEquals; 

public class AuthenticatedHelloWorldTest extends AbstractShiroTest { 

    @Override 
    protected Application configure() { 
    return new ResourceConfig(AuthenticatedHelloWorldResource.class); 
    } 

    @Test 
    public void testAuthenticatedReadHelloWorld() { 
    final String hello = target("/authhello").request().get(String.class); 
    assertEquals("Hello world!", hello); 
    } 

    @Test 
    public void testAuthenticatedWriteHelloWorld() { 
    final String hello = target("/authhello/test").request().get(String.class); 
    assertEquals("Hello you! test", hello); 
    } 

} 

하지만 실제로 @RequiresRoles -annotation의 기능을 테스트하는 방법을 모르겠어요. Shiro's page on testing을 읽었으나 실패한 테스트를 작성하지 못했습니다 (예 : /authhello/test에 액세스하려는 역할이없는 과목의 테스트). 어떤 조언을 부탁드립니다.

답변

6

테스트 해 볼까요?

예. 특정 역할에 리소스가 있거나 리소스에 액세스하지 못하도록하려는 경우에 한합니다. 이것은 보안 통합 테스트가 될 것입니다.

전체 응용 프로그램을 설정하는 방법 + 실제로 테스트하려면 http 요청과 함께 실제로 호출해야합니까? 아니면 더 간단한 방법이 있습니까?

@RequiresAuthentication@RequiresRoles 자체는 클래스 및 메소드 메타 정보 일뿐입니다. 주석 자체는 보안 검사 기능을 제공하지 않습니다.

사용중인 컨테이너의 유형이 분명하지 않지만 저지 JAX-RS 서비스 (맞습니까?)라고 생각됩니다. Shiro가 보안 검사를 수행하려면 엔드 포인트 주변에 일부 JAX-RS 필터 (다른 방법으로는)를 추가해야합니다. 보안을 테스트하려면 테스트에서이 설정을 복제해야합니다. 그렇지 않으면 엔진 주석 처리가 발생하며 그 결과로 보안 검사가 수행되지 않습니다.

+0

답변 해 주셔서 감사합니다. 진지한 파기와 독서를 한 후에 나는 당신이 말한 것처럼 설치 코드를 놓쳤다는 것을 깨달았습니다. 내 테스트에서 앱에서 완료된 설정을 복제 할 수있었습니다 (하지만 불행히도 여기에서는 예제를 공유 할 수 없습니다). – L42

관련 문제