【问题标题】:Spring security works with GET but not with other methodsSpring security 适用于 GET 但不适用于其他方法
【发布时间】:2018-09-24 21:16:02
【问题描述】:

我的 Spring REST 服务具有以下安全适配器,以使用 HTTP 基本 http 身份验证。 现在,当我尝试向任何 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();
}
}

【问题讨论】:

    标签: java spring-boot spring-security


    【解决方案1】:

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

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

    尝试将.anyRequest().authenticated() 添加到您的代码中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-17
      • 2021-04-07
      • 2020-08-21
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      • 2021-09-23
      • 2018-12-07
      相关资源
      最近更新 更多