Search code examples
spring-bootspring-securityspring-repositories

Spring Boot Security + Spring Boot REST Repository config issue


I have Spring boot application as below enter image description here And the Web Security Config as

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated().and().formLogin();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // @formatter:off
        auth.inMemoryAuthentication().withUser("chiru").password("{noop}chiru").roles("ADMIN").and().withUser("user")
                .password("{noop}user").roles("USER");
        // @formatter:on
    }
}

And the i have Repository as below

public interface IssuesRepository extends CrudRepository<Issues, Integer> {

}

when i try to add data through REST Using Postman with Basic Authentication, its failingenter image description here enter image description here


Solution

  • Use httpBasic() instead of formLogin(), like http.authorizeRequests().anyRequest().authenticated().and().httpBasic();.

    formLogin() is used when you want to have login page to authenticate the user (so you have), but in your example you are using http basic to do that. Spring security doesn't recognizes your http basic header and returns login page.

    PS. You can use both methods http.httpBasic().and().formLogin()