[英]spring boot basic authentication not work
I am new for spring boot and I tried to develop api with basic authentication.BUT I always gets "status:401 Unauthorized".我是 spring 引导的新手,我尝试使用基本身份验证开发 api。但我总是得到“状态:401 未经授权”。
*In pom.xml *在 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
*In application.properties *在application.properties中
management.security.enabled= false
spring.security.user.name = username
spring.security.user.password=123456
instead of set up security cofiguration in properties file, you can create a java class like this: it work well而不是在属性文件中设置安全配置,您可以像这样创建 java class:它运行良好
@Configuration
@EnableWebSecurity
public class ApplicatiponSecurityConfig extends WebSecurityConfigurerAdapter {
private final PasswordEncoder passwordEncoder;
@Autowired
public ApplicatiponSecurityConfig(PasswordEncoder p) {
this.passwordEncoder = p;
//provide to encode password, without it , it don't work
}
@Override
protected void configure(HttpSecurity http) throws Exception{
http
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic();
}
@Override
@Bean
protected UserDetailsService userDetailsService() {
UserDetails s = User.builder()
.username("username")
.password(passwordEncoder.encode("123456"))
.build();
return new InMemoryUserDetailsManager(
s
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.