Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 测试期间的Spring MockMVC、Spring安全性和全局方法安全性_Java_Spring_Spring Boot_Spring Security_Jhipster - Fatal编程技术网

Java 测试期间的Spring MockMVC、Spring安全性和全局方法安全性

Java 测试期间的Spring MockMVC、Spring安全性和全局方法安全性,java,spring,spring-boot,spring-security,jhipster,Java,Spring,Spring Boot,Spring Security,Jhipster,我有以下用户资源,方法createUser被保护为管理员角色 我预计测试会失败,因为当mock使用用户角色时,api只允许管理员角色使用,但测试正在通过。 任何帮助都将不胜感激。尝试删除@WithMockUser注释,并更改测试方法,如下所示 ManagedUserVM managedUserVM = new ManagedUserVM(); managedUserVM.setLogin(DEFAULT_LOGIN); managedUserVM.setPassword(DEFA

我有以下用户资源,方法createUser被保护为管理员角色

我预计测试会失败,因为当mock使用用户角色时,api只允许管理员角色使用,但测试正在通过。
任何帮助都将不胜感激。

尝试删除@WithMockUser注释,并更改测试方法,如下所示

ManagedUserVM managedUserVM = new ManagedUserVM();
    managedUserVM.setLogin(DEFAULT_LOGIN);
    managedUserVM.setPassword(DEFAULT_PASSWORD);
   managedUserVM.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));
进行完整的测试。你可以参考这个


注意:我使用的JHipster版本是5.2.0。不保证这将适用于所有版本

如果您使用的是您应该使用的服务,则可以对服务方法进行注释。在集成测试中使用@WithMockUser应该可以正常工作,而无需进行任何其他更改。这里有一个例子。注意,我还在JDL中使用一个服务接口传递serviceImpl标志,但它也可以在服务实现中使用

/**
* Service Interface for managing Profile.
*/
public interface ProfileService {

/**
 * Delete the "id" profile.
 *
 * @param id the id of the entity
 */
@Secured(AuthoritiesConstants.ADMIN)
void delete(Long id);
JHipster自动生成的相应rest控制器方法:

/**
 * DELETE  /profiles/:id : delete the "id" profile.
 *
 * @param id the id of the profileDTO to delete
 * @return the ResponseEntity with status 200 (OK)
 */
@DeleteMapping("/profiles/{id}")
@Timed
public ResponseEntity<Void> deleteProfile(@PathVariable Long id) {
    log.debug("REST request to delete Profile : {}", id);
    profileService.delete(id);
    return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
}
JHipster使用MockMvcBuilders.standaloneSetup,该程序通过手动实例化的控制器传递,而不是通过Spring,因此也不是通过AOP。 因此,不会拦截预授权,并跳过安全检查。 因此,您可以@Autowire您的控制器并将其传递给MockMvcBuilders.standaloneSetup,这种设置违背了使用独立设置的目的,或者简单地使用WebApplicationContext:MockMvcBuilders.webAppContextSetup with and autowired WepAppContext。

删除@WithMockUser有什么意义?我想用角色\用户运行该方法。此外,managedUserVM只是一个对象,我们将在post中使用它来创建用户。
/**
* Service Interface for managing Profile.
*/
public interface ProfileService {

/**
 * Delete the "id" profile.
 *
 * @param id the id of the entity
 */
@Secured(AuthoritiesConstants.ADMIN)
void delete(Long id);
/**
 * DELETE  /profiles/:id : delete the "id" profile.
 *
 * @param id the id of the profileDTO to delete
 * @return the ResponseEntity with status 200 (OK)
 */
@DeleteMapping("/profiles/{id}")
@Timed
public ResponseEntity<Void> deleteProfile(@PathVariable Long id) {
    log.debug("REST request to delete Profile : {}", id);
    profileService.delete(id);
    return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
}