gpt4 book ai didi

java - Spring Boot 2 安全基础认证

转载 作者:行者123 更新时间:2023-11-29 09:58:59 29 4
gpt4 key购买 nike

为什么以下基本安全配置不应用 inMemoryAuthentication() 子句?

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.httpBasic()
.and()
.authorizeRequests()
.anyRequest().authenticated();
super.configure(http);
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("username").password("password");
super.configure(auth);
}

}

应用初始化后,仍然只有Spring自己默认生成的user,没有username这样的用户。

最佳答案

不要从 void configure(AuthenticationManagerBuilder auth) 调用 super 方法。它将 disableLocalConfigureAuthenticationBldr 标志设置为 true,这会导致您的 AuthenticationManagerBuilder 被忽略。最后,您的 void configure(AuthenticationManagerBuilder auth) 方法应如下所示:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("username").password("password").roles("USER");
}

关于java - Spring Boot 2 安全基础认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49543394/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com