Guest User

Untitled

a guest
Dec 4th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
  2. String username="tt";
  3. if (principal instanceof UserDetails) {
  4. username = ((UserDetails)principal).getUsername();
  5. } else {
  6. username = principal.toString();
  7. }
  8. return username;
  9.  
  10. @Configuration
  11.  
  12. /**In Memory Authentication
  13. * @param authenticationManagerBuilder Allow to build an AuthenticationManager
  14. */
  15.  
  16. @Bean
  17. public UserDetailsService mongoUserDetails() {
  18. return new UserService();
  19. }
  20.  
  21. @Autowired
  22. public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) {
  23. try {
  24.  
  25.  
  26. UserDetailsService userDetailsService = mongoUserDetails();
  27. authenticationManagerBuilder.userDetailsService(userDetailsService);
  28.  
  29. } catch (Exception exception) {
  30.  
  31. LOG.error("In Memory Authentication has failed", exception);
  32. }
  33.  
  34.  
  35. }
  36.  
  37. @Service
  38.  
  39. @Override
  40. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  41. MongoCollection<Document> collection = MongoConnection.getConnection().getCollection("users");
  42. Document document = collection.find(Filters.eq("username",username)).first();
  43. if(document!=null) {
  44. String username1 = document.getString("username");
  45. String password = document.getString("password");
  46. List<String> authorities = (List<String>) document.get("authorities");
  47. MongoUserDetails mongoUserDetails = new MongoUserDetails(username1,password,authorities.toArray(new String[authorities.size()]));
  48. return mongoUserDetails;
  49. }
  50. return null;
  51. }
Add Comment
Please, Sign In to add comment