Depois de configurar a classe
@Component
public class SecurityFilter extends OncePerRequestFilter {
@Autowired
private TokenService tokenService;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
tokenService.getSubject(retrieveToken(request));
filterChain.doFilter(request, response);
}
private String retrieveToken(HttpServletRequest request) {
var authHeader = request.getHeader("Authorization");
if(authHeader == null){
throw new RuntimeException("Token não enviado no cabeçalho Authorization");
}
return authHeader.replace("Bearer ", "");
}
}
não consigo logar pelo insomnia
seguem as classes:
SecurityConfiguration
@Configuration
@EnableWebSecurity
public class SecurityConfiguration implements WebMvcConfigurer {
@Autowired
private SecurityFilter securityFilter;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{
return http.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and().authorizeHttpRequests()
.requestMatchers(HttpMethod.POST, "/login").permitAll()
.anyRequest().authenticated()
.and().addFilterBefore(securityFilter, UsernamePasswordAuthenticationFilter.class)
.build();
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception{
return configuration.getAuthenticationManager();
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
TokenService
@Service
public class TokenService {
@Value("{api.security.token.secret}")
private String secret;
public String tokenGenerator(Users users){
try {
var algorithm = Algorithm.HMAC256(secret);
return JWT.create()
.withIssuer("API Sica.School")
.withSubject(users.getLogin())
.withClaim("registration", users.getRegistration())
.withClaim("username", users.getUsername())
//.withExpiresAt(expirationData())
.sign(algorithm);
} catch (JWTCreationException exception){
// Invalid Signing configuration / Couldn't convert Claims.
throw new RuntimeException("Erro ao gerar o token", exception);
}
}
public String getSubject(String tokenJWT){
try {
var algorithm = Algorithm.HMAC256(secret);
return JWT.require(algorithm)
.withIssuer("API Sica.School")
.build()
.verify(tokenJWT)
.getSubject();
} catch (JWTVerificationException exception) {
throw new RuntimeException("Token JWT inválido ou expirado!");
}
}
private Instant expirationData() {
return LocalDateTime.now().plusHours(2).toInstant(ZoneOffset.of("-3:00"));
}
}
uma curiosidade, parece que a classe SecurityConfiguration não está sendo carregada