SecurityUtils.java 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package com.genersoft.iot.vmp.conf.security;
  2. import com.genersoft.iot.vmp.conf.security.dto.LoginUser;
  3. import com.genersoft.iot.vmp.storager.dao.dto.User;
  4. import org.springframework.security.authentication.AuthenticationManager;
  5. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  6. import org.springframework.security.core.Authentication;
  7. import org.springframework.security.core.context.SecurityContext;
  8. import org.springframework.security.core.context.SecurityContextHolder;
  9. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  10. import javax.security.sasl.AuthenticationException;
  11. import java.time.LocalDateTime;
  12. public class SecurityUtils {
  13. /**
  14. * 描述根据账号密码进行调用security进行认证授权 主动调
  15. * 用AuthenticationManager的authenticate方法实现
  16. * 授权成功后将用户信息存入SecurityContext当中
  17. * @param username 用户名
  18. * @param password 密码
  19. * @param authenticationManager 认证授权管理器,
  20. * @see AuthenticationManager
  21. * @return UserInfo 用户信息
  22. */
  23. public static LoginUser login(String username, String password, AuthenticationManager authenticationManager) throws AuthenticationException {
  24. //使用security框架自带的验证token生成器 也可以自定义。
  25. UsernamePasswordAuthenticationToken token =new UsernamePasswordAuthenticationToken(username,password);
  26. //认证 如果失败,这里会自动异常后返回,所以这里不需要判断返回值是否为空,确定是否登录成功
  27. Authentication authenticate = authenticationManager.authenticate(token);
  28. LoginUser user = (LoginUser) authenticate.getPrincipal();
  29. SecurityContextHolder.getContext().setAuthentication(token);
  30. return user;
  31. }
  32. /**
  33. * 获取当前登录的所有认证信息
  34. * @return
  35. */
  36. public static Authentication getAuthentication(){
  37. SecurityContext context = SecurityContextHolder.getContext();
  38. return context.getAuthentication();
  39. }
  40. /**
  41. * 获取当前登录用户信息
  42. * @return
  43. */
  44. public static LoginUser getUserInfo(){
  45. Authentication authentication = getAuthentication();
  46. if(authentication!=null){
  47. Object principal = authentication.getPrincipal();
  48. if(principal!=null && !"anonymousUser".equals(principal)){
  49. // LoginUser user = (LoginUser) authentication.getPrincipal();
  50. String username = (String) principal;
  51. User user = new User();
  52. user.setUsername(username);
  53. LoginUser loginUser = new LoginUser(user, LocalDateTime.now());
  54. return loginUser;
  55. }
  56. }
  57. return null;
  58. }
  59. /**
  60. * 获取当前登录用户ID
  61. * @return
  62. */
  63. public static int getUserId(){
  64. LoginUser user = getUserInfo();
  65. return user.getId();
  66. }
  67. /**
  68. * 生成BCryptPasswordEncoder密码
  69. *
  70. * @param password 密码
  71. * @return 加密字符串
  72. */
  73. public static String encryptPassword(String password) {
  74. BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  75. return passwordEncoder.encode(password);
  76. }
  77. }