任何人都可以看到为什么我在退回客户时会得到空? 我正在使用java配置,并且能够通过我的mongoConfig连接到数据库。
但是当想要登录时,似乎问题是我的mongouserdtailsservice。
谢谢
public class MongoUserDetailsService implements UserDetailsService
{
@Override
public UserDetails loadUserByUsername(String username) throws AuthenticationServiceException
{
MongoOperations mongoOperations = null;
try
{
mongoOperations = new MongoTemplate(new MongoClient(), "booking");
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (MongoException e)
{
e.printStackTrace();
}
try
{
Query query = new Query(Criteria.where("email").is(username));
System.out.println("query ready to go");
Customer customer = mongoOperations.findOne(query, Customer.class);
System.out.println("query done");
System.out.println("user: "+customer);
if (customer == null)
{
throw new AuthenticationServiceException("Authentication failed for user " + username);
}
boolean enabled = true;
boolean accountNonExpired = true;
boolean credentialsNonExpired = true;
boolean accountNonLocked = true;
return new User(customer.getUsername(), customer.getPassword().toLowerCase(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked,
getAuthorities(2));
}
catch (Exception e)
{
System.out.println("query failed");
throw new RuntimeException(e);
}
}
public Collection<? extends GrantedAuthority> getAuthorities(Integer role)
{
List<GrantedAuthority> authList = getGrantedAuthorities(getRoles(role));
return authList;
}
public List<String> getRoles(Integer role)
{
List<String> roles = new ArrayList<String>();
if (role.intValue() == 1)
{
roles.add("ROLE_USER");
roles.add("ROLE_ADMIN");
}
else if (role.intValue() == 2)
{
roles.add("ROLE_USER");
}
return roles;
}
public static List<GrantedAuthority> getGrantedAuthorities(List<String> roles)
{
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for (String role : roles)
{
authorities.add(new SimpleGrantedAuthority(role));
}
return authorities;
}
}
答案 0 :(得分:0)
解决
这是我的MongoUserDetailsService中的冲突。
这是一个运作良好的新版本!(OBS!新登录是通过电子邮件发送的)
@Component public class MongoUserDetailsService implements UserDetailsService { public MongoOperations mongoOperations; private User userDetails; @Override public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { boolean enabled = true; boolean accountNonLocked = true; boolean accountNonExpired = true; boolean credentialsNonExpired = true; try { mongoOperations = new MongoTemplate(new MongoClient(), "booking"); Customer user = getUserByEmail(email); userDetails = new User(user.getEmail(), user.getPassword(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, getAuthorities(user.getRoleAsInt())); } catch (UnknownHostException e) { e.printStackTrace(); } return userDetails; } public List getAuthorities(Integer role) { List authList = new ArrayList(); if (role.intValue() == 2) { authList.add(new SimpleGrantedAuthority("ROLE_ADMIN")); authList.add(new SimpleGrantedAuthority("ROLE_USER")); } if (role.intValue() == 1) { authList.add(new SimpleGrantedAuthority("ROLE_USER")); } return authList; } public Customer getUserByEmail(String email) { Query query = new Query(); query.addCriteria(Criteria.where("email").is(email)); Customer customer = mongoOperations.findOne(query, Customer.class); return customer; } }