WebSecurityConfigurerAdapter和webmvcconfigurer 执行顺序
时间: 2023-10-26 19:40:53 浏览: 38
`WebSecurityConfigurerAdapter` 和 `WebMvcConfigurer` 是两个不同的接口,分别用于 Spring Security 和 Spring MVC 的配置。
它们的执行顺序是不同的。`WebSecurityConfigurerAdapter` 的配置会优先于`WebMvcConfigurer` 执行。这是因为 Spring Security 的过滤器链需要在 Spring MVC 的拦截器之前执行,以确保安全性。因此,在 Spring Boot 应用程序中,`WebMvcConfigurer` 的配置不会影响 Spring Security 的配置。
如果您需要在 Spring Security 和 Spring MVC 之间共享配置,可以使用`AbstractConfiguredSecurityBuilder`中的方法,或者使用`@Order`注解为配置类指定顺序来实现。
相关问题
WebSecurityConfigurerAdapter WebMvcConfigurer
WebSecurityConfigurerAdapter是Spring Security提供的一个适配器类,用于配置Spring Security的安全性。它提供了一些方法,可以用来配置Spring Security的行为,例如配置用户认证、授权、跨域等。在使用Spring Security时,我们可以继承WebSecurityConfigurerAdapter类,并重写其中的方法来实现自定义的安全配置。
WebMvcConfigurer是Spring MVC提供的一个接口,用于配置Spring MVC的行为。它提供了一些方法,可以用来配置Spring MVC的行为,例如配置视图解析器、拦截器、静态资源等。在使用Spring MVC时,我们可以实现WebMvcConfigurer接口,并重写其中的方法来实现自定义的MVC配置。同时,WebMvcConfigurerAdapter是WebMvcConfigurer的实现类,它提供了一些默认的空方法,方便我们只需要重写需要的方法即可。但是需要注意的是,WebMvcConfigurerAdapter已经被废弃,建议直接实现WebMvcConfigurer接口。
WebMvcConfigurer和WebSecurityConfigurerAdapter都生效
WebMvcConfigurer和WebSecurityConfigurerAdapter是Spring框架中用于配置Web应用程序的两个重要接口。它们分别用于配置Web MVC和Web安全。
WebMvcConfigurer是一个接口,用于配置Spring MVC的行为。它提供了一些方法,可以用来自定义MVC配置,例如添加拦截器、配置视图解析器、配置静态资源等。通过实现WebMvcConfigurer接口并重写其中的方法,可以对Spring MVC进行个性化的配置。
WebSecurityConfigurerAdapter是一个抽象类,用于配置Spring Security的行为。它提供了一些方法,可以用来自定义安全配置,例如配置身份验证、授权规则、登录页面等。通过继承WebSecurityConfigurerAdapter并重写其中的方法,可以对Spring Security进行个性化的配置。
如果想同时生效WebMvcConfigurer和WebSecurityConfigurerAdapter,可以创建一个配置类,同时实现这两个接口,并重写它们的方法。这样就可以在同一个配置类中对Spring MVC和Spring Security进行配置。
以下是一个示例代码:
```java
@Configuration
@EnableWebMvcSecurity
public class WebAppConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer {
// 配置Spring Security
@Override
protected void configure(HttpSecurity http) throws Exception {
// 添加安全配置
}
// 配置Spring MVC
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 添加拦截器
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
// 配置视图解析器
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 配置静态资源
}
// 其他重写的方法...
}
```
这样,WebMvcConfigurer和WebSecurityConfigurerAdapter的配置都会生效。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)