1. 程式人生 > 其它 >常用公共工具類——Shiro工具類

常用公共工具類——Shiro工具類

public class ShiroUtils {

    public static Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }

    public static Subject getSubject() {
        return SecurityUtils.getSubject();
    }

    public static SysUserEntity getUserEntity() {
        return (SysUserEntity)SecurityUtils.getSubject().getPrincipal();
    }

    
public static Long getUserId() { return getUserEntity().getUserId(); } public static void setSessionAttribute(Object key, Object value) { getSession().setAttribute(key, value); } public static Object getSessionAttribute(Object key) { return getSession().getAttribute(key); }
public static boolean isLogin() { return SecurityUtils.getSubject().getPrincipal() != null; } public static String getKaptcha(String key) { Object kaptcha = getSessionAttribute(key); if(kaptcha == null){ throw new RRException("驗證碼已失效"); } getSession().removeAttribute(key);
return kaptcha.toString(); } }
一個小小後端的爬行痕跡