小编典典

如何在Spring Boot应用程序中关闭Spring Security

spring-boot

我已经在具有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,我有AuthServerConfigResourceServerConfig也。我的主要应用程序类如下所示:

@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 URL时,我仍然得到401。

  1. 注释掉与安全相关的类中的所有注释,包括@Configuration@EnableWebSecurity。在Spring boot Security Disable security中,建议在底部添加@EnableWebSecurity将DISABLE auth,我认为这没有任何意义。无论如何尝试过,没有用。

  2. 通过删除所有安全性内容来修改websecurityconfig,仅这样做 http .authorizeRequests() .anyRequest().permitAll();

使用Spring SecurityJava配置时禁用基本身份验证。也无济于事。

  1. 删除安全性自动配置

@EnableAutoConfiguration(排除=
{org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class,org.springframework.boot.actuate.autoconfigure.ManagementSecurityAutoConfiguration.class})

就像他们在spring boot app中禁用springsecurity一样。但是我认为此功能仅适用于spring-boot-actuator我没有的功能。所以不要尝试这个。

禁用Spring Security的正确方法是什么?


阅读 5148

收藏
2020-05-30

共1个答案

小编典典

正如@Maciej Walkowiak所提到的,您应该在您的主类中这样做:

@SpringBootApplication(exclude = org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class)
public class MainClass {
2020-05-30