JWTLoginFilter
package br.com.architectbudgeplanner.security
import br.com.architectbudgeplanner.config.JWTUtil
import br.com.architectbudgeplanner.config.UserDetail
import br.com.architectbudgeplanner.model.Credentials
import com.fasterxml.jackson.databind.ObjectMapper
import jakarta.servlet.FilterChain
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.springframework.security.authentication.AuthenticationManager
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.Authentication
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
class JWTLoginFilter(
private val authManager: AuthenticationManager,
private val jwtUtil: JWTUtil
) : UsernamePasswordAuthenticationFilter() {
override fun attemptAuthentication(request: HttpServletRequest?, response: HttpServletResponse?): Authentication {
val (username, password) = ObjectMapper().readValue(request?.inputStream, Credentials::class.java)
val token = UsernamePasswordAuthenticationToken(username, password)
return authManager.authenticate(token)
}
override fun successfulAuthentication(
request: HttpServletRequest?,
response: HttpServletResponse?,
chain: FilterChain?,
authResult: Authentication?
) {
val username = (authResult?.principal as UserDetail).username
val token = jwtUtil.generateToken(username)
response?.addHeader("Authorization", "Bearer $token")
}
}
Classe Credentials
package br.com.architectbudgeplanner.model
data class Credentials(
val username: String = "",
val password: String = "",
) {
}