springboot 2.6.13WebMvcConfigurer和WebSecurityConfigurerAdapter都生效
时间: 2024-03-25 08:32:54 浏览: 12
根据提供的引用内容,可以看出在Spring Boot 2.6.13中,可以通过配置`spring.mvc.pathmatch.matching-strategy`来指定路径匹配策略。而在第一个引用中,配置的是`ant_path_matcher`策略,而在第二个引用中,也是配置的相同的策略。
因此,可以得出结论,在Spring Boot 2.6.13中,无论是WebMvcConfigurer还是WebSecurityConfigurerAdapter都会生效,并且都会使用`ant_path_matcher`作为路径匹配策略。
相关问题
springboot 2.6.13WebMvcConfigurer和WebSecurityConfigurerAdapter 实现方法都生效
根据提供的引用内容,可以使用Spring Boot 2.6.13中的WebMvcConfigurer和WebSecurityConfigurerAdapter来实现方法的生效。
首先,我们需要创建一个类,实现WebMvcConfigurer接口,并重写addInterceptors方法。在该方法中,我们可以添加自定义的拦截器。
```java
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 添加自定义的拦截器
registry.addInterceptor(new MyInterceptor());
}
}
```
接下来,我们需要创建一个类,继承WebSecurityConfigurerAdapter,并重写configure方法。在该方法中,我们可以配置安全相关的设置,例如身份验证和授权。
```java
@Configuration
@EnableWebSecurity
public class MyWebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 配置安全相关的设置
http.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/user/**").hasRole("USER")
.anyRequest().authenticated()
.and()
.formLogin();
}
}
```
最后,我们需要在应用程序的入口类上添加@EnableWebMvc和@EnableWebSecurity注解,以启用WebMvcConfigurer和WebSecurityConfigurerAdapter。
```java
@SpringBootApplication
@EnableWebMvc
@EnableWebSecurity
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
```
通过以上步骤,我们可以同时实现WebMvcConfigurer和WebSecurityConfigurerAdapter中的方法生效。
springboot2.6.13如何 WebMvcConfigurer和WebSecurityConfigurerAdapter实现方法同时生效
在Spring Boot 2.6.13中,可以通过实现WebMvcConfigurer和WebSecurityConfigurerAdapter接口来同时生效WebMvcConfigurer和WebSecurityConfigurerAdapter的配置方法。
下面是一个示例代码,展示了如何实现这两个接口并使其同时生效:
```java
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
// 实现WebMvcConfigurer接口的方法
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 添加拦截器
registry.addInterceptor(new MyInterceptor());
}
}
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// 实现WebSecurityConfigurerAdapter接口的方法
@Override
protected void configure(HttpSecurity http) throws Exception {
// 配置安全规则
http.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/user/**").hasRole("USER")
.anyRequest().authenticated()
.and()
.formLogin().permitAll()
.and()
.logout().permitAll();
}
}
```
在上面的示例中,WebConfig类实现了WebMvcConfigurer接口的addInterceptors方法,用于添加拦截器。SecurityConfig类继承了WebSecurityConfigurerAdapter类,并重写了configure方法,用于配置安全规则。
为了使这两个配置同时生效,需要在启动类上添加@EnableWebMvc和@EnableWebSecurity注解,示例如下:
```java
@SpringBootApplication
@EnableWebMvc
@EnableWebSecurity
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
这样,WebMvcConfigurer和WebSecurityConfigurerAdapter的配置方法就会同时生效。