Spring security只支持GET,不支持其他方法

xv8emn3q  于 2024-01-09  发布在  Spring
关注(0)|答案(1)|浏览(235)

我有以下安全适配器用于我的Spring REST服务以使用HTTP基本http auth。现在,当我尝试向任何GET HTTP端点发送请求时,请求成功授权和处理。但所有其他HTTP方法都返回401。你有什么想法吗?

  1. @EnableWebSecurity
  2. public class WebSecurityAdapter extends WebSecurityConfigurerAdapter {
  3. @Override
  4. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  5. auth.inMemoryAuthentication()
  6. .withUser("user").password(passwordEncoder().encode("password")).roles("USER");
  7. }
  8. @Override
  9. protected void configure(HttpSecurity http) throws Exception {
  10. http.authorizeRequests().antMatchers("/**").hasRole("USER").and().httpBasic();
  11. }
  12. @Bean
  13. public PasswordEncoder passwordEncoder() {
  14. return new BCryptPasswordEncoder();
  15. }
  16. }

字符串

qybjjes1

qybjjes11#

你有一个AuthenticationException,它是一个运行时异常。在这里阅读Spring Security Authentication。WebSecurity的默认配置,如这里所说的HttpSecurity是:

  1. protected void configure(HttpSecurity http) throws Exception {
  2. http
  3. .authorizeRequests()
  4. .anyRequest().authenticated()
  5. .and()
  6. .formLogin()
  7. .and()
  8. .httpBasic();
  9. }

字符串
尝试将.anyRequest().authenticated()添加到代码中。

相关问题