springboot 2.6.13WebMvcConfigurer和WebSecurityConfigurerAdapter 实现方法都生效
时间: 2024-03-25 18:32:54 浏览: 26
根据提供的引用内容,可以使用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中的方法生效。