|
|
|
@ -1,43 +1,68 @@
|
|
|
|
|
package com.tamguo.utils;
|
|
|
|
|
|
|
|
|
|
import org.apache.shiro.SecurityUtils;
|
|
|
|
|
import org.apache.shiro.session.Session;
|
|
|
|
|
import org.apache.shiro.subject.Subject;
|
|
|
|
|
|
|
|
|
|
import com.tamguo.modules.member.model.MemberEntity;
|
|
|
|
|
import org.apache.shiro.SecurityUtils; // 导入 Shiro 的安全工具类
|
|
|
|
|
import org.apache.shiro.session.Session; // 导入 Shiro 的会话类
|
|
|
|
|
import org.apache.shiro.subject.Subject; // 导入 Shiro 的主体类
|
|
|
|
|
import com.tamguo.modules.member.model.MemberEntity; // 导入会员实体类
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ShiroUtils 类,提供与 Shiro 相关的工具方法
|
|
|
|
|
*/
|
|
|
|
|
public class ShiroUtils {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前会话
|
|
|
|
|
*/
|
|
|
|
|
public static Session getSession() {
|
|
|
|
|
return SecurityUtils.getSubject().getSession();
|
|
|
|
|
return SecurityUtils.getSubject().getSession(); // 获取 Shiro 主体的会话
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前主体
|
|
|
|
|
*/
|
|
|
|
|
public static Subject getSubject() {
|
|
|
|
|
return SecurityUtils.getSubject();
|
|
|
|
|
return SecurityUtils.getSubject(); // 获取 Shiro 主体
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前登录的会员实体
|
|
|
|
|
*/
|
|
|
|
|
public static MemberEntity getMember() {
|
|
|
|
|
return (MemberEntity)SecurityUtils.getSubject().getPrincipal();
|
|
|
|
|
return (MemberEntity)SecurityUtils.getSubject().getPrincipal(); // 获取 Shiro 主体的主要对象(会员实体)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前登录会员的 ID
|
|
|
|
|
*/
|
|
|
|
|
public static String getMemberId() {
|
|
|
|
|
return getMember().getId();
|
|
|
|
|
return getMember().getId(); // 获取会员实体的 ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置会话属性
|
|
|
|
|
*/
|
|
|
|
|
public static void setSessionAttribute(Object key, Object value) {
|
|
|
|
|
getSession().setAttribute(key, value);
|
|
|
|
|
getSession().setAttribute(key, value); // 在会话中设置属性键值对
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取会话属性
|
|
|
|
|
*/
|
|
|
|
|
public static Object getSessionAttribute(Object key) {
|
|
|
|
|
return getSession().getAttribute(key);
|
|
|
|
|
return getSession().getAttribute(key); // 获取会话中指定键的属性值
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断是否登录
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isLogin() {
|
|
|
|
|
return SecurityUtils.getSubject().getPrincipal() != null;
|
|
|
|
|
return SecurityUtils.getSubject().getPrincipal()!= null; // 判断 Shiro 主体的主要对象是否不为空
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 注销登录
|
|
|
|
|
*/
|
|
|
|
|
public static void logout() {
|
|
|
|
|
SecurityUtils.getSubject().logout();
|
|
|
|
|
SecurityUtils.getSubject().logout(); // 执行 Shiro 主体的注销操作
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|