Spring boot basic auth always gives 401 error

Viewed 2949

I have configured spring boot security, by adding the dependency in pom.xml

 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>

And the following class:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(final HttpSecurity http) throws Exception {
  http.authorizeRequests().antMatchers(HttpMethod.GET, "/api").authenticated().and().httpBasic();
http.csrf().disable();
  }
}

I have also added These properties in yml:

security:
 user: user
 Password: user

And am using postman, my URL to secure is:

http://localhost:8080/api

and am adding authorization : type is Basic Auth and username is user and Password is user are also added

But everytime i do Get on this URL, i get 401 unauthorized error.

Please any help is great

1 Answers

Please correct the settings in your application.yml as follows:

spring:
  name:
    user: user
    password: user

BTW, you may see following message in your console output after starting up your application if the configuration hasn't been read correctly:

Using default security password: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Hope this helps to you!

Related