在Spring Boot Security的基本认证中,为什么默认密码没有被自定义密码覆盖?

14 浏览
0 Comments

在Spring Boot Security的基本认证中,为什么默认密码没有被自定义密码覆盖?

即使我扩展了WebSecurityConfigurerAdapter类并重写了它的方法:configure(HttpSecurity http)和configure(AuthenticationManagerBuilder auth),并使用自定义的用户和角色,无论我是添加还是删除了@Configuration和@EnableWebSecurity注解,都会生成默认密码,并且我不能使用在configure()方法中声明的自定义凭据进行http基本身份验证。

我在控制台上总是得到以下结果:

使用生成的安全密码:a2b4b374-65d2-4d20-a965-b34c00d44de9

通过排除SecurityAutoConfiguration.class来关闭Spring Security会禁用整个安全性,这是我不想要的。

pom.xml


    org.springframework.boot
    spring-boot-starter-parent
    2.0.5.RELEASE


    
        org.springframework.boot
        spring-boot-starter-security
    

Security Configuration

    @Configuration
    @EnableWebSecurity
    public class BookStoreSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication().withUser("admin").password("{noop}password").roles("USER");
        }
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.httpBasic().and().antMatcher("/**").authorizeRequests().anyRequest().hasRole("USER").and().csrf().disable();
        }
    }

App Entry Point

    @SpringBootApplication(scanBasePackages={"com.example.springbootsecurity.bookstore"})
    public class BookStoreApp {
        public static void main(String[] args) {
             SpringApplication.run(BookStoreApp.class, args);
        }
    }

我希望可以通过我的自定义凭据:admin/password通过postman进行基本身份验证。

但我总是在控制台上得到默认密码,并且我声明的用户凭据从不起作用,总是返回401未经授权的http状态。

请帮我解决这个问题!

0