ShiroUtils.java 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package com.kmall.admin.utils;
  2. import com.kmall.admin.fromcomm.entity.SysUserEntity;
  3. import org.apache.shiro.SecurityUtils;
  4. import org.apache.shiro.session.Session;
  5. import org.apache.shiro.subject.Subject;
  6. /**
  7. * Shiro工具类
  8. *
  9. * @author Scott
  10. * @email
  11. * @date 2016年11月12日 上午9:49:19
  12. */
  13. public class ShiroUtils {
  14. public static Session getSession() {
  15. return SecurityUtils.getSubject().getSession();
  16. }
  17. public static Subject getSubject() {
  18. return SecurityUtils.getSubject();
  19. }
  20. public static SysUserEntity getUserEntity() {
  21. return (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
  22. }
  23. public static Long getUserId() {
  24. return getUserEntity().getUserId();
  25. }
  26. public static void setSessionAttribute(Object key, Object value) {
  27. getSession().setAttribute(key, value);
  28. }
  29. public static Object getSessionAttribute(Object key) {
  30. return getSession().getAttribute(key);
  31. }
  32. public static boolean isLogin() {
  33. return SecurityUtils.getSubject().getPrincipal() != null;
  34. }
  35. public static void logout() {
  36. SecurityUtils.getSubject().logout();
  37. }
  38. public static String getKaptcha(String key) {
  39. String kaptcha = getSessionAttribute(key).toString();
  40. getSession().removeAttribute(key);
  41. return kaptcha;
  42. }
  43. }