Hola buenas tardes, tengo este problema y no lo he podido solucionar, agradezco si me pueden ayudar porque en el metodo get no me aparecen el listado de medicos, gracias. adjunto fotos y los codigos de las clases.
Gracias!
@Configuration
@EnableWebSecurity
public class SecurityConfigurations {
@Autowired
private SecurityFilter securityFilter;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http.csrf(csrf -> csrf.disable())
.sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(req -> {
req.requestMatchers(HttpMethod.POST, "/login").permitAll();
req.anyRequest().authenticated();
})
.addFilterBefore(securityFilter, UsernamePasswordAuthenticationFilter.class)
.build();
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
@Component
public class SecurityFilter extends OncePerRequestFilter {
@Autowired
private TokenService tokenService;
@Autowired
private UsuarioRepository usuarioRepository;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
System.out.println("el filtro esta siendo llamado");
//obtener el token del header
var autHeader = request.getHeader("Authorization");
if(autHeader != null){
var token = autHeader.replace("Bearer ", "");
var nombreUsuario = tokenService.getSubject(token);
if (nombreUsuario != null) {
//token valido
var usuario = usuarioRepository.findByLogin(nombreUsuario);
var authentication = new UsernamePasswordAuthenticationToken(usuario, null, usuario.getAuthorities());
usuarioRepository.findByLogin(nombreUsuario);
SecurityContextHolder.getContext().setAuthentication(authentication);
}
}
filterChain.doFilter(request, response);
}
}
@Service
public class TokenService {
@Value("${api.security.secret}")
private String apiSecret;
public String generarToken(Usuario usuario){
try {
Algorithm algorithm = Algorithm.HMAC256(apiSecret);
return JWT.create()
.withIssuer("voll med")
.withSubject(usuario.getLogin())
.withClaim("id", usuario.getId())
.withExpiresAt(generarFechaExpiracion())
.sign(algorithm);
}catch (JWTCreationException exception){
throw new RuntimeException();
}
}
public String getSubject(String token) {
if (token != null) {
throw new RuntimeException();
}
DecodedJWT verifier = null;
try {
Algorithm algorithm = Algorithm.HMAC256(apiSecret);
verifier = JWT.require(algorithm)
.withIssuer("voll med")
.build()
.verify(token);
verifier.getSubject();
} catch (JWTVerificationException exception) {
System.out.println(exception.toString());
}
if (verifier.getSubject()== null ) {
throw new RuntimeException("Verifier invalido");
}
return verifier.getSubject();
}
private Instant generarFechaExpiracion(){
return LocalDateTime.now().plusHours(246).toInstant(ZoneOffset.of("-05:00"));
}
}