I have 2 classes that contains user password and roles. I have my own userDetailsService that get the user password and roles with the username and return an objet User with the username, password and Set of grantedauthorities qith all the roles of the user.
But i cant manage to login i tried a lot of thing and dont know how to proceed.
i have
package com.example.application.security;
import com.example.application.data.Persona;
import com.example.application.data.PersonaRepository;
import com.example.application.data.Rol;
import com.example.application.data.RolRepository;
import com.example.application.services.CrmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.*;
@Servicepublic class TribunalesUserDetailsService implements UserDetailsService {
@Autowired private CrmService crm;
@Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
PersonaRepository personaRepository = crm.getPersonaRepository();
RolRepository rolRepository = crm.getRolRepository();
Set<GrantedAuthority> authorityList = new HashSet<>();
List<Persona> p = personaRepository.search(username);
Iterator<Persona> it = p.iterator();
if(it.hasNext()){
Persona persona = it.next();
List<Rol> roles = rolRepository.search(persona.getUsuario());
for (Rol rol : roles) {
authorityList.add(new SimpleGrantedAuthority(rol.getCodigo()));
}
return new org.springframework.security.core.userdetails.User(persona.getUsuario(), persona.getContrasena(), authorityList);
}else{
throw new UsernameNotFoundException("Usuario no encontrado");
}
}
}
And the securityConfig
package com.example.application.security;
import com.example.application.views.LoginView;
import com.vaadin.flow.spring.security.VaadinWebSecurity;
import org.springframework.beans.factory.annotation.Autowired;
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.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@EnableWebSecurity@Configurationpublic class SecurityConfig extends VaadinWebSecurity {
@Autowired private TribunalesUserDetailsService userDetailsService;
@Override protected void configure(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth ->
auth.requestMatchers(
AntPathRequestMatcher.antMatcher(HttpMethod.GET, "/images/*.png")).permitAll());
super.configure(http);
setLoginView(http, LoginView.class);
}
@Bean public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean public UserDetailsService users() {
return userDetailsService;
}
}
I tried logging whit plain text, bcrypt password bcrypt with {bcrypt}before the hash and nothing resulted
New contributor