spring-boot - 使用方法安全springboot
问题描述
我使用这个示例(https://www.djamware.com/post/5c819d0180aca754f7a9d1ee/securing-restful-api-with-spring-boot-security-and-data-mongodb#ch5)来创建一个安全的rest Api。但是可以使用@EnableGlobalMethodSecurity 在方法级别进行保护吗?
怎么样.. tks
解决方案
当然。只需@EnableGlobalMethodSecurity
在任何@Configuration
bean 上进行注释。一个好的方法是在配置安全性的配置 bean 上进行注释:
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
}
然后您可以使用@PreAuthorize
、@PreFilter
和@PostAuthorize
来@PostFilter
配置 SpEL 以保护 bean 方法:
@PreAuthorize("hasRole('ADMIN')")
public void create(Contact contact){
}
有关可用于保护方法的内置 SpEL 的可用列表,请参阅此内容。
推荐阅读
- django - 如何修复 TypeError:需要一个类似字节的对象,而不是 travis 上的 'str' 导致测试在我的本地机器上运行良好
- elasticsearch - ElasticSearch 中的路径层次标记器无法正常工作
- xcode - macOS:删除文件时的断点
- javascript - 如何在Angular中获取具有所需下拉元素的有效表单
- swashbuckle - Microsoft.OpenAPI 示例或文档?
- c# - 如何在xamarin android中更改铃声模式
- c++ - ECDSA 使用 SecByteBlock 密钥对
- javascript - 如何在不同时区的特定日期显示数据库中的项目?
- android - 自定义字体垂直对齐的偏移量
- c - 我查找素数的代码不起作用。这里有什么问题?