Вы пытались включить httpBasic
в своем фильтре безопасности?
@Override
protected void configure(HttpSecurity http) throws Exception {
.csrf().disable()
.authorizeRequests()
.antMatchers("/hello*").hasAnyRole("USER","ADMIN")
.and()
.httpBasic();
}
Я создал целый пример проекта , который использует как basic, так и formLogin
Конфигурация безопасности похожа на вашу, но я предпочитаю не показывать пароли в виде открытого текста в реальном приложении
@Bean
public PasswordEncoder passwordEncoder(){
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
@Bean
public UserDetailsService userDetailsService() {
return new InMemoryUserDetailsManager(
builder()
.username("user")
.password("{bcrypt}$2a$10$C8c78G3SRJpy268vInPUFu.3lcNHG9SaNAPdSaIOy.1TJIio0cmTK")
.roles("USER")
.build(),
builder()
.username("admin")
.password("{bcrypt}$2a$10$XvWhl0acx2D2hvpOPd/rPuPA48nQGxOFom1NqhxNN9ST1p9lla3bG")
.roles("ADMIN")
.build()
);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
//application security
.authorizeRequests()
.mvcMatchers("/non-secure/**").permitAll()
.anyRequest().hasAnyRole("ADMIN","USER")
.and()
.httpBasic()
.and()
.formLogin()
.and()
;
// @formatter:on
}
и мы можем продемонстрировать с помощью простого теста , что он работает
@Test
@DisplayName("user / 123 basic authentication")
void userBasic() throws Exception {
mvc.perform(
get("/secure")
.header("Authorization", "Basic " + Base64.encodeBase64String("user:123".getBytes()))
)
.andExpect(authenticated())
.andExpect(status().isOk())
;
}
Я написал простой тест , который закодировал мои пароли
@Test
void printPasswords() {
System.out.println("123 = "+passwordEncoder.encode("123"));
System.out.println("password = "+passwordEncoder.encode("password"));
}