Я столкнулся со странной ситуацией с использованием пружинной безопасности.Воспользовавшись:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
При следующей простой настройке безопасности:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
UserDetails user = User.builder().username("1").password("1").roles("USER").build();
auth.inMemoryAuthentication().withUser(user).passwordEncoder(new BCryptPasswordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests().antMatchers("/inquiry").authenticated().anyRequest().permitAll().and()
.httpBasic();
}
}
Я постоянно получаю код состояния 401
Http.Но я углубился в код и понял, что в ядре безопасности Spring есть небольшая проблема.Класс DaoAuthenticationProvider
пытается проверить, соответствует ли предоставленный пароль фактическим учетным данным с кодировщиком пароля (в моем случае BCrypt
).Итак,
if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword()))
Но в кодировщике сигнатура метода matches
имеет вид:
public boolean matches(CharSequence rawPassword, String encodedPassword)
Так что аутентификация не удалась.