【问题标题】:Spring Secured Rest Controller is not found in WebMVCTest在 WebMVCTest 中找不到 Spring Secured Rest Controller
【发布时间】:2022-11-25 02:08:15
【问题描述】:

在 REST-Controller 上使用 @Secured 实现接口时,在 @WebMvcTest 中找不到控制器。删除 @Secured 注释或删除类上的实现将使其在测试中运行。

@Controller
@RequestMapping(path="/failing")
public class FailingTestController implements MyPasswordApi {

    @RequestMapping(method = GET, produces = MediaType.APPLICATION_JSON_VALUE, path = "/test")
    @Secured("ROLE_USER")
    public ResponseEntity<GetEntity> getMethod()

@Controller
@RequestMapping(path = "/running")
public class RunningTestController  {

    @RequestMapping(method = GET, produces = MediaType.APPLICATION_JSON_VALUE, path = "/test")
    @Secured("ROLE_USER")
    public ResponseEntity<GetEntity> getMethod() {

都用于不同的 jUnit-5 测试。 “RunningTest”将成功(即 GET-Request 的状态为 200),而“FailingTest”将以状态 404 结束。使用注入的 RequestMapppingHanderMapping 可以看到,具有继承的控制器未绑定.

实际上,在应用程序中,找到了两个控制器。

我的问题是,如何测试实现安全性的控制器一个界面。

在github上找到一个测试用例:https://github.com/sanddorn/Spring-Boot-Security-Rest-Showcase

【问题讨论】:

    标签: spring-boot spring-security


    【解决方案1】:

    您使用了错误的注释,将 Controller 替换为 RestController

    @RestController
    @RequestMapping(path = "/running")
    public class RunningTestController  {
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-16
      • 2019-03-11
      • 2019-06-17
      • 1970-01-01
      • 2017-11-05
      • 2022-01-02
      • 2019-05-10
      • 2019-07-16
      • 1970-01-01
      相关资源
      最近更新 更多