Skip to content
Snippets Groups Projects
Commit cb0973d2 authored by ginquin's avatar ginquin
Browse files

modificado MyUserDerailsService

parent 5db23123
No related branches found
No related tags found
1 merge request!1Rama postgres
package es.uva.inf.tfg.ginquin.smellswisdom.security;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.Set;
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 es.uva.inf.tfg.ginquin.smellswisdom.domain.Authority;
import es.uva.inf.tfg.ginquin.smellswisdom.domain.User;
import es.uva.inf.tfg.ginquin.smellswisdom.services.UserService;
@Service
public class MyUserDetailsService implements UserDetailsService {
private static final Map<String, MyUser> USERS = new HashMap<String,MyUser>();
private static void add(MyUser mu){
USERS.put(mu.getUsername(), mu);
}
static{
/*
* private static final Map<String, MyUser> USERS = new
* HashMap<String,MyUser>(); private static void add(MyUser mu){
* USERS.put(mu.getUsername(), mu); } static{
*
* add(new MyUser("rod","81dc9bdb52d04dc20036dbd8313ed055", //password:1234 new
* String[]{"ROLE_USER", "ROLE_ADMIN"} ));
*
* add(new MyUser("dianne","81dc9bdb52d04dc20036dbd8313ed055", new
* String[]{"ROLE_USER", "ROLE_ADMIN"} ));
*
* add(new MyUser("scott","81dc9bdb52d04dc20036dbd8313ed055", new
* String[]{"ROLE_USER"} ));
*
* add(new MyUser("peter","81dc9bdb52d04dc20036dbd8313ed055", new
* String[]{"ROLE_USER"} )); }
*/
private UserService userService;
// Usuario
private User myUser;
// Conjunto de roles
private Set<Authority> roles;
private ArrayList<String> listaRoles;
add(new MyUser("rod","81dc9bdb52d04dc20036dbd8313ed055", //password:1234
new String[]{"ROLE_USER", "ROLE_ADMIN"} ));
// must return a value or throw UsernameNotFoundException
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
MyUser user = null;
add(new MyUser("dianne","81dc9bdb52d04dc20036dbd8313ed055",
new String[]{"ROLE_USER", "ROLE_ADMIN"} ));
// Se obtiene el usuario
myUser = userService.getUsuario(username);
add(new MyUser("scott","81dc9bdb52d04dc20036dbd8313ed055",
new String[]{"ROLE_USER"} ));
if (myUser != null) {
roles = myUser.getAuthorities();
add(new MyUser("peter","81dc9bdb52d04dc20036dbd8313ed055",
new String[]{"ROLE_USER"} ));
listaRoles = new ArrayList<String>();
try {
for (Authority sRol : roles) {
listaRoles.add(sRol.getAuthority());
}
// Creo el usuario NeoUser que extiende de UserDetails que es lo que entiende
// Spring Security
user = new MyUser(myUser.getUsername(), myUser.getPassword(),
listaRoles.toArray(new String[listaRoles.size()]));
// must return a value or throw UsernameNotFoundException
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
MyUser user = USERS.get(username);
if(user==null){
System.out.println(">>> cannot find user: "+username);
throw new UsernameNotFoundException("cannot found user: "+username);
} catch (Exception e) {
System.err.println("Error al obtener los roles");
System.err.println(e.toString());
}
//Return a clone object to avoid a user's credentials data being erased after success authentication.
//This behavior only happens from Spring Security 3.1 onwards.
//please refer to http://static.springsource.org/spring-security/site/docs/3.1.x/reference/core-services.html#core-services-erasing-credentials
return new MyUser(user.getUsername(), user.getPassword(), user.getAuthorities());
}
if (user == null) {
throw new UsernameNotFoundException(
"No se ha encontrado al usuario: " + username);
}
return user;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment