Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Security throwing 403 after basic authentication

I am using Spring Security for basic authentication to secure my REST APIs.

Below is the configuration code:

@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();
    }
}

I am getting forbidden (403) error on authenticating myself using the correct username and password.

enter image description here

Please suggest the modifications to get it working.

like image 573
Tanay Mathur Avatar asked Jan 10 '17 11:01

Tanay Mathur


2 Answers

You haven't enabled HTTP Basic Authentication you have to use 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();
    }

}
like image 185
shazin Avatar answered Nov 20 '22 05:11

shazin


Updated

@Override
protected void configure(HttpSecurity http) throws Exception {
  http.csrf().disable().httpBasic().and().authorizeRequests().anyRequest().authenticated();
}
like image 34
Infomaster Avatar answered Nov 20 '22 05:11

Infomaster