QuestionI am a new Java Spring Boot developer, and I am trying to create a REST API project. I want to log in with a username and password using Postman. However, I am encountering an error when I use the .httpBasic(withDefaults()) method.
Here is my security configuration code:
@Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { return httpSecurity.csrf(AbstractHttpConfigurer::disable) .authorizeHttpRequests(registry -> { registry.requestMatchers("/registration/**", "/").permitAll(); registry.requestMatchers("/user/**").hasAnyRole("ADMIN", "USER"); registry.requestMatchers("/admin/**").hasRole("ADMIN"); registry.anyRequest().authenticated(); }) .httpBasic(withDefaults()) .build();}
My GoalI want to be able to log in using Postman with a username and password, like a fully RESTful API, but without using JWT.
The ProblemWhen I use the method .httpBasic(withDefaults()), I get an error. How can I configure my application to use HTTP Basic authentication correctly so that I can log in with Postman?
What I TriedI configured my Spring Security setup as shown in the code above. I used the .httpBasic(withDefaults()) method to enable HTTP Basic authentication. I expected that I would be able to log in using Postman by providing a username and password in the authorization header.