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 :