PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/src/main/java/com/ruoyi/framework/security/handle/LogoutSuccessHandlerImpl.java

https://gitlab.com/mrsunchangemyselfsun/ruoyi-vue
Java | 53 lines | 38 code | 3 blank | 12 comment | 1 complexity | d5cc1846545b6ebfd29722e5a115eaf6 MD5 | raw file
  1. package com.ruoyi.framework.security.handle;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.security.core.Authentication;
  9. import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
  10. import com.alibaba.fastjson.JSON;
  11. import com.ruoyi.common.constant.Constants;
  12. import com.ruoyi.common.constant.HttpStatus;
  13. import com.ruoyi.common.core.domain.AjaxResult;
  14. import com.ruoyi.common.core.domain.model.LoginUser;
  15. import com.ruoyi.common.utils.ServletUtils;
  16. import com.ruoyi.common.utils.StringUtils;
  17. import com.ruoyi.framework.manager.AsyncManager;
  18. import com.ruoyi.framework.manager.factory.AsyncFactory;
  19. import com.ruoyi.framework.web.service.TokenService;
  20. /**
  21. * 自定义退出处理类 返回成功
  22. *
  23. * @author ruoyi
  24. */
  25. @Configuration
  26. public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler
  27. {
  28. @Autowired
  29. private TokenService tokenService;
  30. /**
  31. * 退出处理
  32. *
  33. * @return
  34. */
  35. @Override
  36. public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
  37. throws IOException, ServletException
  38. {
  39. LoginUser loginUser = tokenService.getLoginUser(request);
  40. if (StringUtils.isNotNull(loginUser))
  41. {
  42. String userName = loginUser.getUsername();
  43. // 删除用户缓存记录
  44. tokenService.delLoginUser(loginUser.getToken());
  45. // 记录用户退出日志
  46. AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGOUT, "退出成功"));
  47. }
  48. ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(HttpStatus.SUCCESS, "退出成功")));
  49. }
  50. }