【问题标题】:JerseyTest with shiroJerseyTest 与 shiro
【发布时间】:2017-09-19 09:24:44
【问题描述】:

用这个类实例化 JerseyTest

public class NMAAbstractRestTest extends JerseyTest {

  @Override
  protected Application configure() {
    NMAdaptorApplication application = new NMAdaptorApplication();
    return application;
  }

}

NMAdaptorApplication 构造函数。

public NMAdaptorApplication() {
    log.info("NM-Adaptor-Application is being initilized....");
    register(NMRestExceptionMapper.class);
    register(ShiroFeature.class);
    register(NMMarshallingFeature.class);
    register(new LoggingFeature(java.util.logging.Logger.getLogger("nma"), LoggingFeature.Verbosity.PAYLOAD_ANY));
    packages(true, "com.retailwave.rwos.nma.rest.resource");
    loadProperties();

}

在执行下面的测试方法时

@Test
public void makeOrder()
{  
   ...
   Entity entity = Entity.entity(orderContent, MediaType.APPLICATION_JSON);
   WebTarget target = target("customerorders");
   target.path("");
   target.queryParam("cmd", "create");
   target.queryParam("branchCode", "610");
   arget.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_USERNAME, "admin");
   target.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_PASSWORD, "admin");
   Response response = target.request().post(entity);
}

得到以下异常

16:34:23.893 错误 [grizzly-http-server-0] crrneNMRestExceptionMapper - 在 Mapper 捕获的异常:调用代码无法访问 SecurityManager,绑定到 org.apache.shiro.util.ThreadContext 或作为vm 静态单例。这是无效的应用程序配置。 org.apache.shiro.UnavailableSecurityManagerException:调用代码无法访问 SecurityManager,绑定到 org.apache.shiro.util.ThreadContext 或作为 vm 静态单例。这是无效的应用程序配置。 在 org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:123) 在 org.apache.shiro.subject.Subject$Builder.(Subject.java:627)

我猜是因为我在 web.xml 中配置的 Shiro 过滤器

 <filter>
    <filter-name>ShiroFilter</filter-name>
    <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
 </filter>

<filter-mapping>
    <filter-name>ShiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>

当 JUnit 测试失败时。是否有任何选项可以在 JerseyTest 中包含 web.xml

【问题讨论】:

    标签: java jersey-2.0 shiro jersey-test-framework


    【解决方案1】:

    我不知道这是否能解决问题,因为我不使用 Shiro,但是如果你想配置一些原本会在 web.xml 中配置的东西,你需要做更多的工作。

    首先需要在 servlet 容器中运行测试。假设,您使用的是 Grizzly(注意,这不可能使用内存提供程序),您需要使用 GrizzlyWebTestContainerFactory

    @Override
    protected TestContainerFactory getTestContainerFactory() {
        return new GrizzlyWebTestContainerFactory();
    }
    

    然后你需要配置DeploymentContext。这是您可以进行 servlet 容器配置的地方

    @Override
    protected DeploymentContext configureDeployment() {
        // just configure the ResourceConfig here instead of the `configure`.
        final ResourceConfig config = new ResourceConfig();
        return ServletDeploymentContext
                .forServlet(new ServletContainer(config))
                .addFilter(ShiroFilter.class, "ShiroFilter")
                .build();
    }
    

    另请参阅:

    【讨论】:

    • 完美,尚未解决,但现在需要额外的 shiro 配置。希望我能解决它。完成后我会更新。
    • 您找到解决方案了吗?我有同样的问题,我认为 ShiroFeature 的全部目的是初始化上下文侦听器和过滤器,而不是使用 web.xml