Problem: Authenticate user using H2 database in Spring Security
Context: The application is done using Spring, the user class is this
@Entity
data class Usuario(
@NotEmpty
var nome:String = "",
@NotEmpty
var login:String = "",
@NotEmpty
var senha:String = "",
@OneToMany(cascade= arrayOf(CascadeType.ALL), mappedBy="usuario")
var simulacoes:MutableSet<Simulacao> = mutableSetOf(),
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
var id:Int = 0
)
Class repository:
interface UsuarioRep:JpaRepository<Usuario,Int>{
fun findByLogin(login:String):Usuario
}
SpringSecurity Configuration:
@Configuration
class SecurityConfig : WebSecurityConfigurerAdapter() {
override fun configure(http: HttpSecurity): Unit {
http
.authorizeRequests()
.antMatchers("/","/cadastro").permitAll()
.antMatchers("/principal").hasRole("USER")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll()
http.exceptionHandling().accessDeniedPage("/");
}
@Autowired
fun configAuthentication(auth: AuthenticationManagerBuilder){
//
}
}
I spent a few hours researching and unsuccessfully, the official documentation is in Java and the tutorials are also scarce.
For what I understand I should create a UserDetailsService
, however how can this be done in kotlin?