@EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true)含义
时间: 2023-12-09 21:46:47 浏览: 24
@EnableWebSecurity 是一个注解,用于启用 Spring Security 的 web 安全功能。当我们使用 Spring Security 时,我们可以使用这个注解来告诉 Spring,我们已经准备好使用 Spring Security 进行安全控制。
@EnableGlobalMethodSecurity(prePostEnabled = true) 是另一个注解,它允许我们开启方法级别的安全控制。它使用了 Spring Security 的 PreAuthorize 和 PostAuthorize 注解,让我们可以在方法级别上控制访问权限。这个注解的参数 prePostEnabled = true 表示开启 PreAuthorize 和 PostAuthorize 注解。
相关问题
@EnableGlobalMethodSecurity(prePostEnabled = true)和@EnableWebSecurity区别
`@EnableGlobalMethodSecurity(prePostEnabled = true)`和`@EnableWebSecurity`都是Spring Security提供的注解,但它们的作用略有不同。
`@EnableWebSecurity`注解是用于开启Web安全性的,它会创建一个`springSecurityFilterChain`过滤器链,用于拦截并处理Web请求。在`@EnableWebSecurity`注解下,可以定义一些Web安全的配置,如`WebSecurityConfigurerAdapter`中的`configure(HttpSecurity http)`方法,用于配置HTTP请求的安全性。
`@EnableGlobalMethodSecurity(prePostEnabled = true)`注解则是用于开启Spring方法级别的安全性。它允许在方法上使用`@PreAuthorize`和`@PostAuthorize`等注解,来实现对方法的访问控制。
因此,`@EnableWebSecurity`注解和`@EnableGlobalMethodSecurity(prePostEnabled = true)`注解是不同层面的安全性配置,前者关注Web请求的安全性,后者关注方法的安全性。在实际使用中,这两个注解可以同时使用,以实现对整个系统的全面安全保护。
需要注意的是,如果同时使用了这两个注解,`@EnableGlobalMethodSecurity(prePostEnabled = true)`注解需要放在`@EnableWebSecurity`注解之后,例如:
```java
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// ...
}
```
在配置类上写了@EnableWebSecurity,是不是就可以不用写@EnableGlobalMethodSecurity(prePostEnabled = true)?
不是的,尽管`@EnableWebSecurity`注解是开启Web安全配置的关键注解,但是它并不会自动开启Spring Security的方法级别的安全控制。因此,如果您想要在Spring Security中使用方法级别的安全控制(如@PreAuthorize, @PostAuthorize等注解),则仍然需要在配置类上使用`@EnableGlobalMethodSecurity(prePostEnabled = true)`注解来开启方法级别的安全控制。
需要注意的是,`@EnableGlobalMethodSecurity(prePostEnabled = true)`注解只有在使用方法级别的安全控制时才需要使用。如果您只是使用基于URL的安全控制(如使用`antMatchers()`进行URL授权控制),则不需要使用该注解。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)