JavaSpring安全性可用于GET,但不能用于其他方法
我的Spring REST服务有以下安全适配器,可以使用HTTP basic HTTP auth。 现在,当我尝试向任何GET HTTP端点发送请求时,请求都已成功授权和处理。但所有其他HTTP方法都返回401。知道吗
@EnableWebSecurity
public class WebSecurityAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password(passwordEncoder().encode("password")).roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/**").hasRole("USER").and().httpBasic();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
# 1 楼答案
您有一个AuthenticationException,它是运行时异常。在这里阅读Spring Security Authentication。 WebSecurity的默认配置,如这里所说HttpSecurity是:
尝试将
.anyRequest().authenticated()
添加到代码中