We have answer of your question!

100% solved queries, no empty question

0

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.

Question author Tanay-mathur | Source

Answer


1


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()
                .authorizeRequests()
                    .anyRequest().authenticated();
    }

}
Answer author Shazin

Tickanswer.com is providing the only single recommended solution of the question Spring Security throwing 403 after basic authentication under the categories i.e java , spring , spring-security , basic-authentication , . Our team of experts filter the best solution for you.

Related Search Queries:


You may also add your answer

Thanks for contributing an answer to Tick Answer!