Em razão do curso estar numa versão muito mais antiga, muitos métodos e classes estão depreciadas, até o BasicToken consegui contornar, mas daqui pra frente não consigo seguir o curso, preciso de ajuda:
Classe SecurityConfiguration:
package br.com.architectbudgeplanner.config
import br.com.architectbudgeplanner.security.JWTAuthenticationFilter
import br.com.architectbudgeplanner.security.JWTLoginFilter
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpMethod
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.invoke
import org.springframework.security.config.http.SessionCreationPolicy
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.web.SecurityFilterChain
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
@Configuration
@EnableWebSecurity
class SecurityConfiguration(
val jwtUtil: JWTUtil
) {
@Bean
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http.invoke {
csrf { disable() } //TODO habilitar csrf para aumentar segurança
authorizeRequests {
authorize(HttpMethod.POST,"/login", permitAll)
authorize(anyRequest, authenticated)
}
http.addFilterBefore(JWTLoginFilter(authManager = authenticationManager!!, jwtUtil = jwtUtil), UsernamePasswordAuthenticationFilter::class.java)
http.addFilterBefore(JWTAuthenticationFilter(jwtUtil = jwtUtil), UsernamePasswordAuthenticationFilter::class.java)
sessionManagement {
sessionCreationPolicy = SessionCreationPolicy.STATELESS
}
headers { frameOptions { disable() } }
}
return http.build()
}
@Bean
fun encoder(): PasswordEncoder = BCryptPasswordEncoder()
}
Classe JWTUtil
package br.com.architectbudgeplanner.config
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.SignatureAlgorithm
import org.springframework.beans.factory.annotation.Value
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.Authentication
import org.springframework.stereotype.Component
import java.util.*
import javax.crypto.spec.SecretKeySpec
@Component
class JWTUtil (
private val expiration: Long = 60000,
@Value("\${jwt.secret}") private var secret: String
){
private val key = SecretKeySpec(secret.toByteArray(), "HmacSHA512")
fun generateToken(username: String): String? {
return Jwts.builder()
.subject(username)
.expiration(Date(System.currentTimeMillis() + expiration))
.signWith(key, SignatureAlgorithm.HS512)
.compact()
}
fun isValid(jwt: String?): Boolean {
return try {
Jwts.parser()
.setSigningKey(key)
.build()
.parseClaimsJws(jwt)
true
} catch (e: Exception) {
false
}
}
fun getAuthentication(jwt: String?): Authentication {
val username = Jwts.parser()
.setSigningKey(key)
.build()
.parseClaimsJws(jwt)
.body
.subject
return UsernamePasswordAuthenticationToken(username, null, null)
}
}
Classe JWTAuthenticationFilter
package br.com.architectbudgeplanner.security
import br.com.architectbudgeplanner.config.JWTUtil
import jakarta.servlet.FilterChain
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.web.filter.OncePerRequestFilter
class JWTAuthenticationFilter(
private val jwtUtil: JWTUtil
) : OncePerRequestFilter() {
override fun doFilterInternal(
request: HttpServletRequest,
response: HttpServletResponse,
filterChain: FilterChain
) {
val token = request.getHeader("Authorization")
val jwt = getTokenDetail(token)
if(jwtUtil.isValid(jwt)){
val authentication = jwtUtil.getAuthentication(jwt)
SecurityContextHolder.getContext().authentication = authentication
}
filterChain.doFilter(request, response)
}
private fun getTokenDetail(token: String?): String? {
return token?.let { jwt ->
jwt.startsWith("Bearer ")
jwt.substring(7, jwt.length)
}
}
}
Continua: