【问题标题】:How to turn off Spring Security in Spring Boot Application如何在 Spring Boot 应用程序中关闭 Spring Security
【发布时间】: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,所以我也有 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 网址时,我仍然得到 401。

  1. 注释掉与安全相关的类中的所有注解,包括@Configuration@EnableWebSecurity。在Spring boot Security Disable security 中,建议在底部添加@EnableWebSecurity 将禁用身份验证,我认为这没有任何意义。试过了,还是不行。

  2. 通过删除所有安全内容来修改 websecurityconfig,并且只做 http .authorizeRequests() .anyRequest().permitAll();

Disable Basic Authentication while using Spring Security Java configuration。也无济于事。

  1. 删除安全自动配置

    @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


【解决方案1】:

正如@Maciej Walkowiak 提到的,你应该为你的主课这样做:

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

【讨论】:

  • 在我的主应用程序类中添加它并在我的 securityconfig 类中注释掉 @EnableWebSecurity 后,它可以工作,就像@MaciejWalkowiak 所说的那样。
  • 现在是org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
【解决方案2】:

试试这个

1->在您的安全配置中注释注释@EnableWebSecurity

//@EnableWebSecurity

2->在您的安全配置中添加这些行

spring.security.enabled=false

management.security.enabled=false

security.basic.enabled=false

【讨论】:

    【解决方案3】:

    对我有用的是这个。创建 WebFilter 和 PermitAll 请求交换并禁用 CSRF。

        @Bean
        public SecurityWebFilterChain chain(ServerHttpSecurity http, AuthenticationWebFilter webFilter) {
            return http.authorizeExchange().anyExchange().permitAll().and()
                    .csrf().disable()
                    .build();
        }
    

    只需将此代码放在@SpringBootApplication 类中,就像这样,就会像魅力一样工作

    @SpringBootApplication
    public class ConverterApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConverterApplication.class, args);
        }
    
    
        @Bean
        public SecurityWebFilterChain chain(ServerHttpSecurity http, AuthenticationWebFilter webFilter) {
            return http.authorizeExchange().anyExchange().permitAll().and()
                    .csrf().disable()
                    .build();
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-08
      • 2016-03-28
      • 2016-08-26
      • 2020-03-20
      • 2016-07-16
      • 2018-08-02
      相关资源
      最近更新 更多