【发布时间】:2017-03-06 19:09:51
【问题描述】:
我已经使用 Spring Security 在我的 Spring Boot 应用程序中实现了身份验证。
控制身份验证的主类应该是 websecurityconfig:
@Configuration
@EnableWebSecurity
@PropertySource(value = { "classpath:/config/application.properties" })
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private RestAuthenticationSuccessHandler authenticationSuccessHandler;
@Autowired
private RestAuthenticationEntryPoint restAuthenticationEntryPoint;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.httpBasic()
.and()
.csrf().disable()
.sessionManagement().sessionCreationPolicy(
SessionCreationPolicy.STATELESS)
.and()
.exceptionHandling()
.authenticationEntryPoint(restAuthenticationEntryPoint)
.and()
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/logout").permitAll()
.antMatchers("/ristore/**").authenticated()
.anyRequest().authenticated()
.and()
.formLogin()
.successHandler(authenticationSuccessHandler)
.failureHandler(new SimpleUrlAuthenticationFailureHandler());
}
因为我在做 OAuth,所以我也有 AuthServerConfig 和 ResourceServerConfig。我的主要应用程序类如下所示:
@SpringBootApplication
@EnableSpringDataWebSupport
@EntityScan({"org.mdacc.ristore.fm.models"})
public class RistoreWebApplication extends SpringBootServletInitializer
{
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*");
}
};
}
public static void main( String[] args )
{
SpringApplication.run(RistoreWebApplication.class, args);
}
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(RistoreWebApplication.class);
}
}
由于我们正在进行代码整合,我们需要暂时关闭身份验证。但是,我尝试了以下方法,但似乎没有任何效果。当我点击这些休息 api 网址时,我仍然得到 401。
注释掉与安全相关的类中的所有注解,包括
@Configuration、@EnableWebSecurity。在Spring boot Security Disable security 中,建议在底部添加@EnableWebSecurity将禁用身份验证,我认为这没有任何意义。试过了,还是不行。通过删除所有安全内容来修改 websecurityconfig,并且只做
http .authorizeRequests() .anyRequest().permitAll();
Disable Basic Authentication while using Spring Security Java configuration。也无济于事。
-
删除安全自动配置
@EnableAutoConfiguration(排除 = { org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class, org.springframework.boot.actuate.autoconfigure.ManagementSecurityAutoConfiguration.class})
就像他们在disabling spring security in spring boot app 所做的一样。但是我认为这个功能只适用于我没有的spring-boot-actuator。所以没试过。
禁用弹簧安全的正确方法是什么?
【问题讨论】:
-
添加
@EnableWebSecurity会禁用 Spring Boot 安全自动配置。在您的情况下,您已经拥有此注释,因此无论如何您都不会利用自动配置。我建议尝试从您的课程中注释掉@EnableWebSecurity,并从自动配置中排除SecurityAutoConfiguration.class。 -
@MaciejWalkowiak 我应该在应用程序类中排除 SecurityAutoConfiguration.class 吗?
-
看看this
-
这能回答你的问题吗? Spring boot Security Disable security
标签: spring rest authentication spring-security spring-boot