spring - MockMvc 和用户主体

标签 spring security principal mockmvc

我尝试测试注入(inject) UserPrincipal 的 mvcControllers 的访问:

restPockMockMvc .perform(get("/pocs").principal(authToken)) .andReturn();

这是我尝试测试的 Controller 方法:

@PreAuthorize(value = "hasRole('AK_ADMIN')") @RequestMapping(method = RequestMethod.GET, produces = "application/json; charset=utf-8") 公共(public) PagedResources 集合列表(HttpServletRequest httpServletRequest){ ........................

httpServletRequest.getUserPrincipal() 始终为空,而我注入(inject)了 userPrincipal,但我不明白为什么...

欢迎您的帮助。 谢谢

最佳答案

尝试

MockHttpServletRequest request = new MockHttpServletRequest();
request.getUserPrincipal();

关于spring - MockMvc 和用户主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29340971/

相关文章:

java - 在 Spring Batch 中访问 HeaderCallBack 中的 ExecutionContext 值

Java System.setSecurityManager() 不能很好地处理策略文件

阿拉伯语的 Java 日期时间

java - Spring Cloud 合约 - Feign Client

java - 模拟restTemplate getForObject

web-services - 安全网络服务的负载测试

security - 通过移动应用程序安全授权支付的协议(protocol)(算法)

Javascript 安全问题/使用 eval()

c# - 我应该如何检查用户是否在 MVC5 中经过身份验证?

simulation - *像box2d/bullet3d这样的*连续物理模拟*的基本思想是什么?