首页 > 解决方案 > 使用方法安全springboot

问题描述

我使用这个示例(https://www.djamware.com/post/5c819d0180aca754f7a9d1ee/securing-restful-api-with-spring-boot-security-and-data-mongodb#ch5)来创建一个安全的rest Api。但是可以使用@EnableGlobalMethodSecurity 在方法级别进行保护吗?

怎么样.. tks

标签: spring-bootjwtjjwt

解决方案


当然。只需@EnableGlobalMethodSecurity在任何@Configurationbean 上进行注释。一个好的方法是在配置安全性的配置 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 的可用列表,请参阅此内容。


推荐阅读