mongodb - MongoUserDetailsService in java config returns null -
anybody can see why null when returning customer? i'm using java config , able connect db mongoconfig.
but when wants login seems problem mongouserdtailsservice.
thanks
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 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 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>(); (string role : roles) { authorities.add(new simplegrantedauthority(role)); } return authorities; } }
solved
it conflict in mongouserdetailsservice.
here new 1 working well!(obs! new log in email)
@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; } }
Comments
Post a Comment