Advertisement
zaknafein83

Login through social-security

Jan 30th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.34 KB | None | 0 0
  1.     public UserDetails loadUserByUsername(String mail) throws UsernameNotFoundException {
  2.         ServletRequestAttributes servletAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
  3.         User user = null;
  4.         if (ConstantPWCabinet.SOCIAL_LOGIN_FACEBOOK.equalsIgnoreCase(servletAttributes.getRequest().getParameter(ConstantPWCabinet.LOGIN_TYPE))) {
  5.             User facebookInfo = dao.getFacebookInfo(new FacebookTemplate(servletAttributes.getRequest().getParameter(ConstantPWCabinet.FACEBOOK_TOKEN)));
  6.             user = dao.getUserSelectedMail(facebookInfo.getEmail());
  7.             List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
  8.             authorities.add(new SimpleGrantedAuthority(Role.ROLE_USER_FACEBOOK.toString()));
  9.             Authentication auth = new UsernamePasswordAuthenticationToken(user, null, authorities);
  10.             SecurityContextHolder.getContext().setAuthentication(auth);
  11.         } else {
  12.             user = dao.getUserSelectedMail(mail);
  13.         }
  14.         List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
  15.         String role = user.getRole().toString();
  16.         if (StringUtils.isEmpty(role))
  17.             authorities.add(new SimpleGrantedAuthority(Role.ROLE_USER.toString()));
  18.         else
  19.             authorities.add(new SimpleGrantedAuthority(role));
  20.         return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), authorities);
  21.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement