2

estaba haciendo un curso de spring security, pero cuando login o cuando quiero ir al path de login me da un error y no me mustra el login que esta por defecto en spring (todo esto lo probe en el navegador)

Entonces lo probe en postman y cuando le doy al path /login me da error 401 (sin pasarle bassic auth ) Si le paso el auth ahi me da error 404 (supongo que es pq no tengo un controller para eso (pero yo quuiero usar el login de spring)

esto tengo en mi config

package com.api.security.infra.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
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.web.SecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception{
        return httpSecurity
                .authorizeHttpRequests(auth -> {
                    auth.requestMatchers("/api/v1/index2").permitAll();
                    auth.anyRequest().authenticated();
                })
                .formLogin(login ->{
                    login.loginPage("/login")
                    .successHandler(successHandler()) //donde se redirige luego del login
                    .permitAll();
                })
                .sessionManagement(sessionManagement ->{
                    sessionManagement.sessionCreationPolicy(SessionCreationPolicy.ALWAYS)
                    .invalidSessionUrl("/login")
                    .maximumSessions(1)
                    .expiredUrl("/login");
                    sessionManagement.sessionFixation().migrateSession();
                })
                .httpBasic(Customizer.withDefaults())
                .build();
    }


    public AuthenticationSuccessHandler successHandler(){
        return ((req, res, auth) -> {
            res.sendRedirect("/api/v1/index");
        });
    }
}

si comento la configuración del fromLogin() ahí si me pide usuario pero ya no me redirije a la url /api/v1/index

este es mi controller:

package com.api.security.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1")
public class CustomerController {
    
    @GetMapping("/index")
    public String index(){
        return "Hola mundo";
    }


    @GetMapping("/index2")
    public String index2(){
        return "Hola mundo sin sguridad";
    }
}

comparto mi repositorio también por si alguien quiere ver mas :

1 respuesta 1

0

asi me funciono, igual hago el mismo curso y tu codigo me ayudo a resolver un problema aunque yo aun no llego a la parte del successhandler

@Bean
    public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
        return httpSecurity
                .authorizeHttpRequests(auth -> {
                    auth.requestMatchers("/v1/index2").permitAll();
                    auth.anyRequest().authenticated();
                })
                .formLogin(login ->{
                    login.isCustomLoginPage();
                    //.successHandler(successHandler()) //donde se redirige luego del login
                   // .permitAll();
                }).build();
    }

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
    return httpSecurity
            .authorizeHttpRequests(auth -> {
                auth.requestMatchers("/v1/index2").permitAll();
                auth.anyRequest().authenticated();
            })
            .formLogin(login ->{
                login.loginProcessingUrl("/login")
                .successHandler(successHandler());
                //.successHandler(successHandler()) //donde se redirige luego del login
               // .permitAll();
            }).build();
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.