pul*_*hal 5 jersey spring-security
我有工作 spring-security配置,它包装我的基于jersey的REST端点,并允许方法级访问.
示例项目包含快速重现问题所需的一切: https //github.com/pulkitsinghal/dummy-rest-api
// Server-Side jersey resource
@GET
@Path("/protected/myendpoint/")
@PreAuthorize("hasRole('DUMMY')")
public String getMyEndpoint(){
return "blah";
}
Run Code Online (Sandbox Code Playgroud)
但是当为受保护的端点编写健全性测试时,我遇到了以下异常,这种情况只发生在单元测试中:
Caused by:
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException:
An Authentication object was not found in the SecurityContext
Run Code Online (Sandbox Code Playgroud)
以下是基于JerseyTest的Unit Test类:
@Test
public void testProtectedEndpoint() {
WebResource webResource = resource();
webResource.addFilter(new HTTPBasicAuthFilter("username", "password"));
String responseMsg = webResource
.path("protected/myendpoint/")
.get(String.class);
System.out.println(responseMsg);
}
Run Code Online (Sandbox Code Playgroud)
设置JerseyTest弹簧安全保护端点时是否有其他人遇到此问题?可以做些什么呢?
我在这里远程看到一些连接:Spring Test&Security:如何模拟身份验证?但是我并没有能够在那个线程中做出正面或反面的事情.思考?
理想情况下,我想要一个单独的容器,例如GrizzlyWebTestContainerFactory通过默认 Maven 生命周期运行测试时的测试 Web 服务器:mvn clean test
但我想,如果没有正确的答案,那么作为使用ExternalTestContainerFactory作为测试网络服务器的解决方法,必须这样做:
$ mvn clean package -DskipTests && (nohup foreman start &)
$ mvn -Djersey.test.containerFactory=com.sun.jersey.test.framework.spi.container.external.ExternalTestContainerFactory \
-Djersey.test.port=5000 \
-Djersey.test.host=localhost \
test
Run Code Online (Sandbox Code Playgroud)
注意:对于那些不熟悉的人,foreman start只需将其视为tomcat start此用例中的所有密集目的即可。
如果测试被破坏并且 spring-security 不会在外部 tomcat 上呕吐,那么这仍然会阻止签入来掌握,所以我想它是有效的。总是去查找pidtomcat/java后台进程并杀死它以进行清理,这有点烦人。
| 归档时间: |
|
| 查看次数: |
1244 次 |
| 最近记录: |