您现在的位置是: 首页 > 后端开发 SpringBoot 2.x Security security.basic.enabled=false 失效问题解决

SpringBoot 2.x Security security.basic.enabled=false 失效问题解决

2020-06-10 后端开发 3761人已围观 6353次浏览

简介SpringBoot 2.x Security security.basic.enabled=false 失效问题解决

在将一个 SpringBoot 1.x 的项目升级到 2.x 的过程中,测试时发现之前在 1.x 项目中,关闭 Security 验证的配置不可用了

security:
  basic:
    enabled: false

查看文档发现,这样的配置在 2.0+ 之后就无法使用了,在 SpringBoot 2.x 的项目中,需要通过代码去配置

也就是继承 WebSecurityConfigurerAdapter 类,并重写其中的 configure(HttpSecurity http) 方法

我们可以新建一个类叫做 SecurityConfig,并加入以下代码

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().permitAll().and().logout().permitAll();//配置不需要登录验证
    }
}

之后重新运行 SpringBoot 项目,访问任意接口,就都不需要使用 Security 自动生成的账号密码登录了

很赞哦! (0)

站长推荐

站点信息

  • 网站地图