WebSecurityConfigurerAdapter的实现类 一般放在springboot那个位置?
时间: 2023-10-12 22:18:02 浏览: 34
WebSecurityConfigurerAdapter的实现类应该放在Spring Security的配置类中。通常情况下,这个配置类应该放在Spring Boot应用程序的主要包(即包含@SpringBootApplication注释的类所在的包)中。如果你使用的是多模块项目,那么该配置类应该放在与Spring Boot主要类相同的模块中。
例如:
```
com
|- example
|- myproject
|- MyProjectApplication.java
|- security
|- SecurityConfig.java
```
其中,MyProjectApplication是Spring Boot应用程序的主要类,SecurityConfig是Spring Security的配置类,应该放在与MyProjectApplication.java相同的包中。
相关问题
springboot 类可以同时继承和实现WebMvcConfigurer和WebSecurityConfigurerAdapter?
根据提供的引用内容,可以看出在Spring Boot中,一个类是可以同时继承和实现`WebMvcConfigurer`和`WebSecurityConfigurerAdapter`的。下面是一个示例代码:
```java
@Configuration
public class WebMvcAndSecurityConfig extends WebMvcConfigurationSupport implements WebSecurityConfigurerAdapter {
// TODO: 实现WebMvcConfigurer和WebSecurityConfigurerAdapter的方法
}
```
在这个示例中,`WebMvcAndSecurityConfig`类同时继承了`WebMvcConfigurationSupport`类和实现了`WebSecurityConfigurerAdapter`接口。这样可以在同一个类中配置Web MVC和Web安全相关的设置。
springboot 类可以同时继承和实现WebMvcConfigurer和WebSecurityConfigurerAdapter
根据提供的引用内容,可以看出Spring Boot中可以同时继承和实现WebMvcConfigurer和WebSecurityConfigurerAdapter类。下面是一个示例代码:
```java
@Configuration
@EnableWebMvc
@EnableWebSecurity
public class MyConfig extends WebMvcConfigurerAdapter implements WebSecurityConfigurerAdapter {
// WebMvcConfigurerAdapter的方法实现
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(3600);
}
// WebSecurityConfigurerAdapter的方法实现
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/api/**").authenticated()
.anyRequest().permitAll()
.and()
.httpBasic();
}
}
```
这个示例中,我们同时继承了WebMvcConfigurerAdapter和WebSecurityConfigurerAdapter类,并实现了它们的方法。在addCorsMappings方法中,我们配置了跨域访问的规则;在configure方法中,我们配置了安全认证的规则。