Já vi as outras dúvidas do forum, mas mesmo assim não consegui resolver.
package br.com.alura.forum.config
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.authentication.dao.DaoAuthenticationProvider
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
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.core.userdetails.UserDetailsService
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.web.DefaultSecurityFilterChain
import org.springframework.security.web.SecurityFilterChain
@Configuration
@EnableWebSecurity
class SecurityConfiguration(
private val userDetailsService: UserDetailsService
) {
@Bean
fun filterChain(http: HttpSecurity): SecurityFilterChain? {
return http.csrf {
it.disable()
}.authorizeHttpRequests {
it.anyRequest().authenticated()
}.sessionManagement {
it.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
}.formLogin{
it.disable()
}.build()
}
@Bean
fun encoder(): PasswordEncoder?{
return BCryptPasswordEncoder()
}
@Bean
fun configure(auth: AuthenticationManagerBuilder?): DaoAuthenticationProvider {
val authProvider = DaoAuthenticationProvider()
authProvider.setUserDetailsService(userDetailsService)
authProvider.setPasswordEncoder(encoder())
// auth?.userDetailsService(userDetailsService)?.passwordEncoder(encoder())
return authProvider
}
}
package br.com.alura.forum.services
import br.com.alura.forum.model.Usuario
import org.springframework.security.core.userdetails.UserDetails
class UserDetail(
private val usuario: Usuario
) : UserDetails {
override fun getAuthorities() = null
override fun getPassword() = usuario.password
override fun getUsername() = usuario.email
override fun isAccountNonExpired() = true
override fun isAccountNonLocked() = true
override fun isCredentialsNonExpired() = true
override fun isEnabled() = true
}
package br.com.alura.forum.services
import br.com.alura.forum.exception.NotFoundException
import br.com.alura.forum.model.Usuario
import br.com.alura.forum.repository.UsuarioRepository
import org.springframework.data.repository.findByIdOrNull
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.stereotype.Service
import java.util.UUID
@Service
class UsuarioService(
private val usuarioRepository: UsuarioRepository,
private val notFoundMessage: String = "Usuario não encontrado"
) : UserDetailsService {
fun buscaPorId(id: Long): Usuario{
return usuarioRepository.findById(id).orElseThrow { NotFoundException(message = notFoundMessage) }
}
override fun loadUserByUsername(username: String?): UserDetails {
val usuario = usuarioRepository.findByEmail(username) ?: throw RuntimeException()
return UserDetail(usuario)
}
}