In Spring Security Library the class WebSecurityConfigurerAdapter will contain all default configuration of spring security. This is the most important class in the Spring Security Framework that we have to extend it for any custom spring security configuration.
If you look inside WebSecurityConfigurerAdapter class, you will see there is an method configure(HttpSecurity http) as below:
ProjectSecurityConfig.java
12345678
protectedvoidconfigure(HttpSecurityhttp)throwsException{this.logger.debug("Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity).");http.authorizeRequests((requests)->{((AuthorizedUrl)requests.anyRequest()).authenticated();//any come to application will be authenticated});http.formLogin();http.httpBasic();}
This method contain the default security configuration of Spring Security Framework. As you can see the configure requests.anyRequest()).authenticated() means that any request that come to your application has to be authenticated. Authenticated means that requests have to be validated by credentials.
Then you and see http.formLogin() this configure indicate that all requests that come to the application from browser can be authenticated by using form login.
Then the configuration http.httpBasic() indicate that all requests that come to your application by back-end Apis, like other Spring Boot services that using Rest template or Postman.
In conclusion, the default security will secure all the request to all apis of your application.
Then we will create a class named ProjectSecurityConfig which will extend class WebSecurityConfigurerAdapter . Then we will ovveride the method configure(HttpSecurity http) to set authenticated and non-authenticated for apis that we want. See the code below:
Now, let's start your application and use post man to call /v1/contact and /v1/notice apis without username/password input because these are public apis. Then you should see the result as below:
Then if you call to other apis as /v1/balance, /v1/accounts/**, /v1/loan and /v1/card without username/password credentials then you will get the error 401 as below
Then when you put the username/password and call again, you will get 200 result.
It happens the same for /v1/balance, /v1/accounts/** and /v1/card apis.