UserService.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package com.yihu.jw.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.security.core.userdetails.User;
  4. import org.springframework.security.core.userdetails.UserDetails;
  5. import org.springframework.security.core.userdetails.UserDetailsService;
  6. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  7. import org.springframework.security.crypto.password.PasswordEncoder;
  8. import org.springframework.stereotype.Service;
  9. import java.util.ArrayList;
  10. /**
  11. * Created by chenweida on 2017/11/29.
  12. * 处理用户校验
  13. */
  14. @Service
  15. public class UserService implements UserDetailsService {
  16. @Autowired
  17. private PasswordEncoder passwordEncoder;
  18. /**
  19. * 我们只需要把用户返回给spring-security 密码框架自己帮我们校验
  20. *
  21. * @param userName
  22. * @return
  23. * @throws UsernameNotFoundException
  24. */
  25. @Override
  26. public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
  27. if ("admin".equals(userName)) {
  28. System.out.printf("password:"+passwordEncoder.encode("123456"));
  29. return new User("admin",
  30. passwordEncoder.encode("123456"),
  31. true,
  32. true,
  33. true,
  34. true,
  35. new ArrayList<>() //权限
  36. );
  37. } else {
  38. throw new UsernameNotFoundException("用户不存在");
  39. }
  40. }
  41. }