我有一個工作spring-security
配置,它包裝了我的基於球衣的REST端點並允許方法級別的訪問。如何使用spring-security設置JerseyTest?
與迅速重現該問題所需的一切示例項目:https://github.com/pulkitsinghal/dummy-rest-api
// Server-Side jersey resource
@GET
@Path("/protected/myendpoint/")
@PreAuthorize("hasRole('DUMMY')")
public String getMyEndpoint(){
return "blah";
}
但對於保護端點寫理智測試時,我遇到了以下異常這只發生在單元測試 :
Caused by:
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException:
An Authentication object was not found in the SecurityContext
下面是基於JerseyTest的單元測試類的樣子:
@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);
}
當彈簧安全保護端點設置爲JerseyTest
時,是否有其他人遇到此問題?可以做些什麼呢?
我遠程看到這裏的一些連接:Spring Test & Security: How to mock authentication?但我沒有達到我可以在該線程中發生的事情的頭或尾的水平。思考?
您可以發佈異常的完整堆棧跟蹤以及您的Spring Security配置嗎?另外,您使用的是Spring,Spring Security和Jersey的哪些版本? –
添加了一個工作示例項目,演示單元測試運行時的問題,在這裏:https://github.com/pulkitsinghal/dummy-rest-api – pulkitsinghal
是這種單元測試,這是不會工作的'GrizzlyWebTestContainerFactory',不管是什麼? – pulkitsinghal