Mongodb java配置中的MongoUserDetailsService返回null
任何人都可以看到为什么我在返回客户时得到空值? 我正在使用java配置,并且能够通过我的mongoConfig连接到DB 但是当想要登录时,问题似乎是我的MongouseRDtails服务 谢谢Mongodb java配置中的MongoUserDetailsService返回null,mongodb,Mongodb,任何人都可以看到为什么我在返回客户时得到空值? 我正在使用java配置,并且能够通过我的mongoConfig连接到DB 但是当想要登录时,问题似乎是我的MongouseRDtails服务 谢谢 public class MongoUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws
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;
}
}
公共类MongoUserDetails服务实现UserDetails服务
{
@凌驾
public UserDetails loadUserByUsername(字符串用户名)引发AuthenticationServiceException
{
MongoOperations MongoOperations=null;
尝试
{
mongoOperations=新MongoTemplate(新MongoClient(),“预订”);
}
捕获(未知后异常e)
{
e、 printStackTrace();
}
捕获(MongoException e)
{
e、 printStackTrace();
}
尝试
{
查询查询=新查询(条件,其中(“电子邮件”)为(用户名));
System.out.println(“查询准备就绪”);
Customer=mongoOperations.findOne(查询,Customer.class);
System.out.println(“查询完成”);
System.out.println(“用户:+customer”);
如果(客户==null)
{
抛出新的AuthenticationServiceException(“用户身份验证失败”+用户名);
}
布尔启用=真;
布尔值accountNonExpired=true;
布尔CredentialsNoExpired=true;
布尔值accountNonLocked=true;
返回新用户(customer.getUsername()、customer.getPassword().toLowerCase()、已启用、AccountNoExpired、CredentialsNoExpired、AccountNonClocked、,
(二),;
}
捕获(例外e)
{
System.out.println(“查询失败”);
抛出新的运行时异常(e);
}
}
公共收集已解决
这是我的MongouseRdetails服务中的冲突
这是一个运行良好的新版本!(注意!新的登录是通过电子邮件进行的)
@组成部分
公共类MongoUserDetailsService实现UserDetailsService
{
公共MongoOperations MongoOperations;
私人用户详细信息;
@凌驾
public UserDetails loadUserByUsername(字符串电子邮件)引发UsernameNotFoundException
{
布尔启用=真;
布尔值accountNonLocked=true;
布尔值accountNonExpired=true;
布尔CredentialsNoExpired=true;
尝试
{
mongoOperations=新MongoTemplate(新MongoClient(),“预订”);
客户用户=getUserByEmail(电子邮件);
userDetails=新用户(User.getEmail()、User.getPassword()、已启用、AccountNoExpired、CredentialsNoExpired、AccountNoLocked、,
getAuthorities(user.getRoleAsInt());
}
捕获(未知后异常e)
{
e、 printStackTrace();
}
返回用户详细信息;
}
公共列表权限(整数角色)
{
List authList=new ArrayList();
if(role.intValue()==2)
{
添加(新的SimpleGrantedAuthority(“角色管理”);
添加(新的SimpleGrantedAuthority(“角色用户”);
}
if(role.intValue()==1)
{
添加(新的SimpleGrantedAuthority(“角色用户”);
}
返回authList;
}
公共客户getUserByEmail(字符串电子邮件)
{
查询=新查询();
query.addCriteria(Criteria.where(“email”).is(email));
Customer=mongoOperations.findOne(查询,Customer.class);
退货客户;
}
}
@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;
}
}