spring-boot - 在带有 Spring Boot 的 Swagger 安全方案上使用两个 API 密钥
问题描述
是否可以在 Swagger 上拥有两个 API 密钥并在我的 API 中赋予它们不同的权限?
例如:
API_KEY_1 : Has access to one Post method
API_KEY_2 : Has access to all of my API
非常感谢
解决方案
就 Spring Security 而言,这完全取决于您如何验证应用程序中的 API 密钥。一旦您设置了应用程序以验证 API 密钥并创建一个SecurityContext
,您最好的选择是映射到两个不同角色之一,一个用于受限访问,一个用于所有访问。例如:
@Configuration
public static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// ...
.authorizeRequests()
.mvcMatchers(HttpMethod.POST, "/some/api").hasAnyRole("BASIC", "ADMIN")
.anyRequest().hasRole("ADMIN");
}
}
更多信息和授权示例可以在文档中找到。
推荐阅读
- c++ - 初始化数据成员的列表的正确术语是什么?
- html - 如果单击Angular 5中行上的任意位置,请选择单选按钮
- php - 在年份之间移动时如何正确处理 PHP DateTime 对象问题
- jenkins - 如何在 jenkins 中为 github webhook 设置 Secret
- android - Oppo 上显示 Qwerty 键盘,但它应该是数字键盘
- json - JSONField 在 JSON 中引用像一对一这样的模型
- angular - 量角器测试挂起等待 browser.wait 并禁用控制流
- python - 用最后一个不为零的值替换零
- selenium - UnreachableBrowserException:无法启动新会话。可能的原因是远程服务器地址无效或浏览器启动失败
- c# - Microsoft 服务总线 - 使用 OnMessage() 方法从总线接收消息