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

xv8emn3q  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(90)

我有以下安全适配器用于我的Spring REST服务以使用HTTP基本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();
}
}

字符串

qybjjes1

qybjjes11#

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

protected void configure(HttpSecurity http) throws Exception {
http
    .authorizeRequests()
        .anyRequest().authenticated()
        .and()
    .formLogin()
        .and()
    .httpBasic();
}

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

相关问题