gpt4 book ai didi

java - JerseyTest 与 shiro

转载 作者:行者123 更新时间:2023-11-30 02:34:03 26 4
gpt4 key购买 nike

使用此类实例化 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 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 过滤器

 <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

最佳答案

我不知道这是否能解决问题,因为我不使用 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();
}

另请参阅:

关于java - JerseyTest 与 shiro,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43558564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com