AuthServiceImpl.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package jnpf.service.impl;
  2. import jnpf.base.ActionResult;
  3. import jnpf.base.UserInfo;
  4. import jnpf.config.ConfigValueUtil;
  5. import jnpf.constant.MsgCode;
  6. import jnpf.exception.LoginException;
  7. import jnpf.exception.TenantDatabaseException;
  8. import jnpf.exception.TenantInvalidException;
  9. import jnpf.granter.TokenGranter;
  10. import jnpf.granter.TokenGranterBuilder;
  11. import jnpf.model.LoginVO;
  12. import jnpf.service.AuthService;
  13. import jnpf.service.LogService;
  14. import jnpf.util.*;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Service;
  18. import java.util.Map;
  19. /**
  20. * 登录与退出服务 其他服务调用
  21. */
  22. @Service
  23. @Slf4j
  24. public class AuthServiceImpl implements AuthService {
  25. @Autowired
  26. private TokenGranterBuilder tokenGranterBuilder;
  27. @Autowired
  28. private LogService logApi;
  29. @Autowired
  30. private ConfigValueUtil configValueUtil;
  31. @Autowired
  32. private RedisUtil redisUtil;
  33. /**
  34. * 登录
  35. * @param parameters {grant_type}
  36. * @return
  37. * @throws LoginException
  38. */
  39. @Override
  40. public ActionResult<LoginVO> login(Map<String, String> parameters) throws LoginException{
  41. long millis = System.currentTimeMillis();
  42. TokenGranter tokenGranter = tokenGranterBuilder.getGranter(parameters.getOrDefault("grant_type", ""));
  43. ActionResult<LoginVO> result;
  44. UserInfo userInfo = new UserInfo();
  45. try {
  46. String account = parameters.get("account");
  47. userInfo.setUserAccount(account);
  48. UserProvider.setLocalLoginUser(userInfo);
  49. result = tokenGranter.granter(parameters);
  50. //写入日志
  51. if (userInfo.getUserId() != null && !UserProvider.isTempUser(userInfo)) {
  52. logApi.writeLogAsync(userInfo.getUserId(), userInfo.getUserName() + "/" + userInfo.getUserAccount(), MsgCode.OA015.get(), (System.currentTimeMillis() - millis));
  53. }
  54. } catch (TenantDatabaseException tdex){
  55. throw tdex;
  56. } catch (Exception e){
  57. if(!(e instanceof LoginException || e instanceof TenantInvalidException)){
  58. String msg = e.getMessage();
  59. if(msg == null){
  60. msg = MsgCode.OA007.get();
  61. }
  62. log.error(MsgCode.OA007.get()+", Account: {}, Error: {}", parameters.getOrDefault("account", ""), e.getMessage(), e);
  63. throw new LoginException(msg);
  64. }
  65. String userName = StringUtil.isNotEmpty(userInfo.getUserName()) ? userInfo.getUserName()+"/"+userInfo.getUserAccount() : userInfo.getUserAccount();
  66. logApi.writeLogAsync(userInfo.getUserId(), userName, e.getMessage(), userInfo, 0, null, (System.currentTimeMillis()-millis));
  67. throw e;
  68. }finally{
  69. LoginHolder.clearUserEntity();
  70. TenantProvider.clearBaseSystemIfo();
  71. // 请求之后就删除验证码 不论结果
  72. String imgCode = parameters.get("timestamp");
  73. if(StringUtil.isNotEmpty(imgCode)) {
  74. redisUtil.remove(imgCode);
  75. }
  76. }
  77. return result;
  78. }
  79. /**
  80. * 踢出用户, 用户将收到Websocket下线通知
  81. * 执行流程:认证服务退出用户->用户踢出监听->消息服务发送Websocket推送退出消息
  82. * @param tokens
  83. */
  84. @Override
  85. public ActionResult kickoutByToken(String... tokens){
  86. UserProvider.kickoutByToken(tokens);
  87. return ActionResult.success();
  88. }
  89. /**
  90. * 踢出用户, 用户将收到Websocket下线通知
  91. * 执行流程:认证服务退出用户->用户踢出监听->消息服务发送Websocket推送退出消息
  92. * @param userId
  93. * @param tenantId
  94. */
  95. @Override
  96. public ActionResult kickoutByUserId(String userId, String tenantId){
  97. UserProvider.kickoutByUserId(userId, tenantId);
  98. return ActionResult.success();
  99. }
  100. }