1
resposta

O codigo não fuciona

Fiz o codigo conforme foi apresentado, mas parece que ele nao esta reconhecendo o usuario, mesmo quando eu coloco o usuario e a senha ainda recebo o erro 401, segue o codigo:

package br.com.alura.forumKotlin.config

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
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.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.config.http.SessionCreationPolicy
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder

@Configuration
@EnableWebSecurity
class SecurityConfiguration(
    private val userDetailsService: UserDetailsService
)


    : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity?) {
        http?.
        authorizeHttpRequests()?.
        anyRequest()?.
        authenticated()?.
                and()?.
                sessionManagement()?.sessionCreationPolicy(SessionCreationPolicy.STATELESS)?.
                and()?.
                formLogin()?.disable()?.
                httpBasic()
    }

    @Bean
    fun bCryptPasswordEncoder(): BCryptPasswordEncoder {
        return BCryptPasswordEncoder()
    }

    override fun configure(auth: AuthenticationManagerBuilder?) {
        auth?.userDetailsService(userDetailsService)?.passwordEncoder(bCryptPasswordEncoder())
    }
}
package br.com.alura.forumKotlin.service

import br.com.alura.forumKotlin.model.Usuario
import br.com.alura.forumKotlin.repository.UsuarioRepository
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.stereotype.Service

@Service
data class UsuarioService(
    private val repository: UsuarioRepository
) : UserDetailsService {

    fun buscarPorId(id: Long): Usuario {
        return repository.getById(id)
    }

    override fun loadUserByUsername(username: String?): UserDetails {
        val usuario = repository.findByEmail(username) ?: throw RuntimeException("email nao encontrado")
        return UserDetail(usuario)
    }
}
UPDATE usuario SET password = '$2a$12$wDQCBzBuo0RyvEKQjKLkH.drHjFJ8Ml/BfP.tjmQEtByZx4IOmMs2' WHERE id = 1
package br.com.alura.forumKotlin.service

import br.com.alura.forumKotlin.model.Usuario
import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.userdetails.UserDetails

class UserDetail(
    private val usuario: Usuario
) : UserDetails {

    override fun getAuthorities(): MutableCollection<out GrantedAuthority>? {
        return null
    }

    override fun getPassword(): String {
        return usuario.passoword
    }

    override fun getUsername(): String {
        return usuario.email
    }

    override fun isAccountNonExpired(): Boolean {
        return true
    }

    override fun isAccountNonLocked(): Boolean {
        return true
    }

    override fun isCredentialsNonExpired(): Boolean {
        return true
    }

    override fun isEnabled(): Boolean {
        return true
    }
}
package br.com.alura.forumKotlin.repository

import br.com.alura.forumKotlin.model.Usuario
import org.springframework.data.jpa.repository.JpaRepository

interface UsuarioRepository: JpaRepository<Usuario, Long> {

    fun findByEmail(username: String?): Usuario?
}
ALTER TABLE usuario ADD COLUMN password text
1 resposta

Guilherme, boa noite. Como você está informando o usuário e senha?