【问题标题】:Cannot disable security in spring boot [duplicate]无法在 Spring Boot 中禁用安全性 [重复]
【发布时间】:2016-07-25 04:27:57
【问题描述】:

我想在我的应用程序中禁用 Spring Security,并在 application.yml 文件中设置属性 security.basic.enable=false。

security:
  basic:
    enabled: false

我使用 spring-boot-actuator 检查了 /env 并发现它已正确加载:(在第 2 行)

[classpath:/application.yml]":{"spring.datasource.url":"jdbc:mysql://localhost:3306/toe?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true","spring.datasource.username":"root","spring.datasource.password":"******",
"security.basic.enabled":false,
"server.port":7777,"flyway.enabled":false}}

但是,安全配置仍然有效,我无法访问需要身份验证的那些,但我可以访问那些是 permitAll。

这是应用程序类:

@SpringBootApplication
@MapperScan("team.xuli.toe.dao")
public class ToeServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ToeServerApplication.class, args);}
}

这是安全配置:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter{

  @Override
  protected void configure(HttpSecurity http) throws Exception {
      http.csrf().disable();
      http.httpBasic();
      http.
             authorizeRequests()
             .antMatchers("/hello").permitAll()
             .anyRequest().authenticated();
  }
  @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      System.out.println("user added in mem!");
      auth
          .inMemoryAuthentication()
              .withUser("xqf").password("123").roles("ADMIN");
  }
}

【问题讨论】:

标签: java spring spring-mvc spring-security spring-boot


【解决方案1】:

如果您在应用程序的任何位置使用@EnableWebSecurity 定义@Configuration,它将关闭 Spring Boot 中的默认 webapp 安全设置。

【讨论】:

【解决方案2】:

如果您需要安全性作为依赖项但不希望 Spring Boot 为您配置它,您可以使用此排除项:

@EnableAutoConfiguration(exclude = { 
        org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class

    })

【讨论】:

    猜你喜欢
    • 2019-03-30
    • 2014-07-16
    • 2020-06-11
    • 2019-01-03
    • 2016-05-03
    • 1970-01-01
    • 2016-01-02
    • 2019-12-28
    相关资源
    最近更新 更多