【问题标题】:Spring Security throwing 403 after basic authentication基本身份验证后 Spring Security 抛出 403
【发布时间】:2017-01-10 11:24:09
【问题描述】:

我正在使用 Spring Security 进行基本身份验证以保护我的 REST API。

下面是配置代码:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user")
                    .password("password")
                    .roles("admin");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
            .csrf().disable()
            .authorizeRequests()
                .anyRequest().authenticated();
    }
}

我在使用正确的用户名和密码进行身份验证时遇到禁止 (403) 错误。

请提出修改建议以使其正常工作。

【问题讨论】:

    标签: java spring spring-security basic-authentication


    【解决方案1】:

    您尚未启用 HTTP 基本身份验证,您必须使用 HttpSecurity.httpBasic()

    @Configuration
    @EnableWebSecurity
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
    
    
        @Autowired
        public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception{
            auth.inMemoryAuthentication().withUser("user").password("password").roles("admin");
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
            http.csrf().disable()
                    .httpBasic()
                    .and()
                    .authorizeRequests()
                        .anyRequest().authenticated();
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      更新

      @Override
      protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().httpBasic().and().authorizeRequests().anyRequest().authenticated();
      }
      

      【讨论】:

        猜你喜欢
        • 2011-02-11
        • 2016-03-20
        • 1970-01-01
        • 2014-12-08
        • 2016-04-30
        • 2013-01-11
        • 2015-07-22
        • 2013-05-13
        • 2018-10-04
        相关资源
        最近更新 更多