I have an app, with configured security, that uses basic authentication (login + password).
My main trouble is when I do a POST
request in Postman, I receive 401 Unauthorized
. But when I do the same request but change it to GET
request, it's returning data with status 200
. Here is my security configuration with additional screenshots.
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends VaadinWebSecurity {
private static final String LOGIN_URL = "/login";
private static final String LOGIN_PROCESSING_URL = "/login";
private static final String LOGIN_FAILURE_URL = "/login?error";
private static final String LOGOUT_SUCCESS_URL = "/";
private static final String DENIED_PAGE_URL = "/404";
private final UserService userService;
private final PasswordEncoder passwordEncoder;
public SecurityConfiguration(UserService userService, PasswordEncoder passwordEncoder) {
this.userService = userService;
this.passwordEncoder = passwordEncoder;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeHttpRequests(auth -> {
auth.requestMatchers("/login", "/register").permitAll();
auth.requestMatchers("/public/**").permitAll();
auth.requestMatchers("/icons/**").permitAll();
auth.requestMatchers("/images/**").permitAll();
auth.requestMatchers("/api/**").authenticated();
auth.requestMatchers("/private/**").authenticated();
auth.requestMatchers("/admin/**").hasAnyRole("ADMIN", "SUPER_ADMIN");
})
.formLogin(loginForm -> {
loginForm.loginPage(LOGIN_URL);
loginForm.loginProcessingUrl(LOGIN_PROCESSING_URL);
loginForm.failureUrl(LOGIN_FAILURE_URL);
})
.logout(logout -> logout.logoutSuccessUrl(LOGOUT_SUCCESS_URL))
.exceptionHandling(e -> {
e.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED));
e.accessDeniedPage(DENIED_PAGE_URL);
})
.httpBasic();
super.configure(http);
setLoginView(http, LoginView.class);
}
@Override
public void configure(WebSecurity web) throws Exception {
super.configure(web);
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);
daoAuthenticationProvider.setUserDetailsService(userService);
return daoAuthenticationProvider;
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
}
This is my request
@PostMapping("/save")
public Expense saveExpense(@RequestBody ExpenseRequest expenseRequest) {
Expense expense = expenseConvertor.convertToExpense(expenseRequest);
return expenseService.saveExpense(expense);
}
GET Request
Additional Information
GET
requests with provided auth in header works finePUT
, POST
, DELETE
requests, all throws 401 Unauthorized
Any help will be great, thanks
Instaed of http.csrf().disable() u should use http.csrf(csrf -> csrf.ignoringRequestMatchers( "/url1/","/url2/")) to let csrf know that u want to ignore perticular Apis calls, we dont use disable() this is provided by spring security itself to protect application from unwanted threats happens using browser cookies
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With