spring-security-test

Spring Security + Spring-Boot Testing Controller

谁说胖子不能爱 提交于 2019-12-24 02:13:07
问题 I'm trying to test the home controller @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } I'm using spring security using as username "user" and test as password by default but @PreAuthorize is not working @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @PreAuthorize("hasRole('ADMIN')") public class HomeControllerTest { @Autowired private TestRestTemplate restTemplate; @Test @WithMockUser(username = "user",

Spring Security Test and MockMvc supply null custom UserDetails parameter to REST Controller

点点圈 提交于 2019-12-06 10:51:41
问题 I'm trying to write an integration test that hits a REST endpoint and gets data for a specific authenticated user (the one I'm setting up in the test). I initially tried my setup with mockMvc = webAppContextSetup(wac).apply(springSecurity()).build() , but that was consistently failing with a BeanInstantiationException . With the help of Testing Spring Security and MvcMock using a custom UserDetails implementation, I was able to overcome that issue by switching up my setUp. Now that I've

Spring MVC Test with RequestPostProcessor vs. Annotations

[亡魂溺海] 提交于 2019-12-05 19:54:40
I have an application I've created with JHipster. I generated a Blog entity, then modified the BlogResource class so its getAll() method only returns the blog for the current user. /** * GET /blogs -> get all the blogs. */ @RequestMapping(value = "/blogs", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @Timed public List<Blog> getAll() { log.debug("REST request to get all Blogs"); return blogRepository.findAllForCurrentUser(); } BlogRepository has the following for its findAllForCurrentUser() method. @Query("select blog from Blog blog where blog.user.login = ?#

Spring Security Test and MockMvc supply null custom UserDetails parameter to REST Controller

三世轮回 提交于 2019-12-04 17:51:31
I'm trying to write an integration test that hits a REST endpoint and gets data for a specific authenticated user (the one I'm setting up in the test). I initially tried my setup with mockMvc = webAppContextSetup(wac).apply(springSecurity()).build() , but that was consistently failing with a BeanInstantiationException . With the help of Testing Spring Security and MvcMock using a custom UserDetails implementation , I was able to overcome that issue by switching up my setUp. Now that I've switched my setup to use standaloneSetup , I'm able to call into my controller. But, no matter what I do, I