springboot2.0 security关闭验证(security.basic.enabled=false失效报错问题解决)

最近在跑demo的过程中,访问swagger页面的时候需要验证登录,记得在之前写的代码中是关闭了security验证,无需登录成功访问,直接在application.yml配置文件中添加上:

management:
  security:
    enabled: false

发现报错,其实在添加的过程中就发现 

springboot2.0 security关闭验证(security.basic.enabled=false失效报错问题解决)_第1张图片

此配置已经失效 ,经查阅发现spring boot 2.0+之后这样配置就不能生效了

但是我们可以在代码中去配置。
我们可以新建一个类SecurityConfig 继承WebSecurityConfigurerAdapter类,
然后重写父类中的configure(HttpSecurity http) 方法。
idea中重写方法的快捷键默认是crtl+o

springboot2.0 security关闭验证(security.basic.enabled=false失效报错问题解决)_第2张图片

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

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

 现在再来访问任意的接口,此处我访问swagger的默认地址http://localhost:8080/swagger-ui.html

springboot2.0 security关闭验证(security.basic.enabled=false失效报错问题解决)_第3张图片

就可以直接得到想要的swagger页了,不需要在输入用户名和密码了。
此外,再补充一点,security默认的username=user,password是我们运行程序的时候生成的

springboot2.0 security关闭验证(security.basic.enabled=false失效报错问题解决)_第4张图片

到这里就可以解决我们集成security每次访问url都需要登陆的麻烦啦! 

你可能感兴趣的:(☆1.编程语言,1.1Java,1.1.2技术点)