Swagger + Spring安全性 - 隐藏基于角色的方法

Esp*_*tad 15 java spring-security swagger spring-rest springfox

我有一个拥有不同消费者的api.我希望他们根据他们在春季安全中的角色获取相关文档.

例如

Api操作A受限于角色A和角色B.

Api操作B受限于角色B.

Api操作C对所有人开放

我正在使用springfox,spring 4,spring rest,security

我知道有一个名为@ApiIgnore的注释,也许可以使用它.

这是可能吗?

The*_*oul -4

您可能已经看到了这一点,但是 SpringFox 本身提供了配置安全性的机制。请参阅SpringFox 官方文档中的本节以及本节的示例(注意点 #14 和 #15)。

如果您愿意允许不同的使用者查看 API,但仍然无法执行 API,则可以考虑在具有适当角色的 API 上添加@Secured注释。

例如:

@Secured ({"ROLE_A", "ROLE_B")
@RequestMapping ("/open/to/both")
public String operationA() {
    // do something
}

@Secured ("ROLE_B")
@RequestMapping ("/open/to/b/only")
public String operationB() {
    // do something
}

// No @Secured annotation here
@RequestMapping ("/open/to/all")
public String operationC() {
    // do something
}
Run Code Online (Sandbox Code Playgroud)

确保您已@EnableGlobalMethodSecurity (securedEnabled = true)在您的SecurityConfig班级(或您拥有的任何班级)中添加@Secured 以便工作。