JerseyTest与shiro [英] JerseyTest with shiro

查看:128
本文介绍了JerseyTest与shiro的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用该类实例化JerseyTest

Instantiating JerseyTest with this class

public class NMAAbstractRestTest extends JerseyTest {

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

}

NMAdaptorApplication构造函数.

NMAdaptorApplication constructor.

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-映射器捕获到异常:调用代码无法访问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)

16:34:23.893 ERROR [grizzly-http-server-0] c.r.r.n.e.NMRestExceptionMapper - Exception caught at Mapper : No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton. This is an invalid application configuration. org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton. This is an invalid application configuration. at org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:123) at org.apache.shiro.subject.Subject$Builder.(Subject.java:627)

我猜这是由于我在web.xml中配置的Shiro过滤器

I guess its due to Shiro filter which I configured in web.xml

 <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

while JUnit test this fails. Is there any option to include web.xml in JerseyTest

推荐答案

我不知道这是否可以解决问题,因为我不使用Shiro,但是如果您要配置本应在其中配置的内容web.xml,您需要做更多的工作.

I don't know if this will solve the problem, as I don't use Shiro, but if you want to configure things that would otherwise be configured in the web.xml, you need to do a little more work.

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

First need to run the test in a servlet container. Assuming, you're using Grizzly (note, this is not possible using the in-memory provider), you need to use the GrizzlyWebTestContainerFactory

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

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

Then you need to configure the DeploymentContext. This is where you can make your servlet container configurations

@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();
}

另请参见:

  • Javadoc for ServletDeploymentContext.
  • Source code test examples

这篇关于JerseyTest与shiro的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆