Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3643

Unable to access Rest endpoints even after successful login in spring security through react form

$
0
0

I am trying to implement login authentication in spring boot ,I have used Spring security and used a react form in the frontend to authenticate , I am using axios to call my /login which works well, the form opens and authenticates perfectly but the problem lies here is that after login is successful i am unable to access other endpoints declared in my spring controller

This is my Spring security config

   @Bean    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {        return http                .authorizeHttpRequests(auth->auth                        .requestMatchers("/","/login/**","/register")                        .permitAll()                        .anyRequest()                        .authenticated())                .logout(logout->logout                        .logoutUrl("/logout")                        .logoutSuccessUrl("/")                        .deleteCookies("JSESSIONID")                        .invalidateHttpSession(true))                .csrf(AbstractHttpConfigurer::disable)                .sessionManagement(session->session.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED))                .build();    }

My login controller

    @PostMapping("/login")    public ResponseEntity<?> login(@RequestBody RegisterUser registerUser) {        try {            Authentication authentication = authenticationManager.authenticate(                    new UsernamePasswordAuthenticationToken(registerUser.getEmail(), registerUser.getPassword())            );            if (authentication.isAuthenticated()) {                return ResponseEntity.ok().body("Login successful");            } else {                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Login failed");            }        } catch (AuthenticationException e) {            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Login failed");        }    }

This is my react form

axios.defaults.baseURL="http://localhost:8080"axios.defaults.withCredentials=true;const Login=()=>{    const [email, setEmail]=React.useState('');    const [password, setpassword]=React.useState('');    const [error, setError] = React.useState('');    const navigate =useNavigate();    const handlesubmit= async (e)=>{        e.preventDefault();        try {            const response = await axios.post("/login" ,{                email,                password,            });            if(response.status===200){                console.error(email,password)                navigate('/');            }            else {                navigate('/logs')            }        }        catch (e){            console.error(e)            setError("login failed")        }    }

Tried using login->login.form("/login") and loginprocesssurl("/login") in security config yet doesnt work, I have also set my proxy to the port where spring is running yet i have no results

What i want now is after the login is successful ( it is ) i want to access the other endpoints which only authenticated users can access


Viewing all articles
Browse latest Browse all 3643

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>