首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JerseyTest与shiro

JerseyTest与shiro
EN

Stack Overflow用户
提问于 2017-04-22 11:14:13
回答 1查看 491关注 0票数 1

用这个类实例化JerseyTest

代码语言:javascript
复制
public class NMAAbstractRestTest extends JerseyTest {

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

}

NMAdaptorApplication构造函数

代码语言:javascript
复制
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();

}

在执行下列测试方法时

代码语言:javascript
复制
@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 ERROR grizzly-http-server-0 c.r.r.n.e.NMRestExceptionMapper -在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) at org.apache.shiro.subject.Subject$Builder.(Subject.java:627)

我想这要归功于我在web.xml中配置的Shiro过滤器

代码语言:javascript
复制
 <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测试失败时,这是失败的。是否有任何选项可以将web.xml包括在JerseyTest中

EN

回答 1

Stack Overflow用户

发布于 2017-04-22 12:13:36

我不知道这是否能解决问题,因为我不使用Shiro,但是如果您想配置web.xml中本来可以配置的东西,那么您需要做更多的工作。

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

代码语言:javascript
复制
@Override
protected TestContainerFactory getTestContainerFactory() {
    return new GrizzlyWebTestContainerFactory();
}

然后需要配置DeploymentContext。在这里,您可以进行servlet容器配置。

代码语言:javascript
复制
@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();
}

还请参见:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43558564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档