The problem is that i'm calling a POST request to '/logout' but the browser logs a GET request
import React from 'react';
import axios from "axios";
import { useState } from 'react';
function Logout() {
const [userIsLoggedIn, setUserIsLoggedIn] = useState(false);
const handleLogout = async () => {
try {
// Make a request to your backend logout endpoint
await axios.post('http://localhost:8080/logout'); // Update the endpoint URL accordingly
// Remove the JWT token from localStorage
localStorage.removeItem('jwtToken');
// Update the userIsLoggedIn state to false
setUserIsLoggedIn(false);
} catch (error) {
console.error('Error during logout:', error);
}
};
return (
<div>
<button onClick={handleLogout}>Log uit</button>
</div>
);
}
export default Logout;
I'm trying to use the Spring Security default '/logout' functionality
package com.example.solarproject.config;
import com.example.solarproject.filter.JwtRequestFilter;
import com.example.solarproject.service.CustomUserDetailsService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig {
public final CustomUserDetailsService customUserDetailsService;
private final JwtRequestFilter jwtRequestFilter;
public SpringSecurityConfig(CustomUserDetailsService customUserDetailsService, JwtRequestFilter jwtRequestFilter) {
this.customUserDetailsService = customUserDetailsService;
this.jwtRequestFilter = jwtRequestFilter;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {
return http.getSharedObject(AuthenticationManagerBuilder.class)
.userDetailsService(customUserDetailsService)
.passwordEncoder(passwordEncoder())
.and()
.build();
}
@Bean
protected SecurityFilterChain filter (HttpSecurity http) throws Exception {
http
.csrf().disable()
.httpBasic().disable()
.cors().and()
.authorizeHttpRequests()
.requestMatchers("/**").permitAll()
.requestMatchers(HttpMethod.POST, "/users").permitAll()
.anyRequest().denyAll()
.and()
.logout()
.logoutSuccessUrl("/") // Redirect after successful logout
.permitAll() // Allow access to the logout URL
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
}
Before i realised Spring Security had its own default '/logout' path and functionality, i have tried creating my own logout functionality inside my Authentication controller. But when i tested that trough Postman, doing a POST request to /logout: It returned a 400 error and as a path it showed '/login' even though i was sending the post to '/logout'.
Also i tried to manually visit localhost:8080/logout and it would redirect me to localhost:8080/login?logout
After that i realised Spring Security has its own default '/logout' like i already said so i deleted the manual logout functionality inside the AuthenticateController