ShiroUtils通用工具包



import com.aaa.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**
 * Created by cws
 * @author cws
 */
public class ShiroUtils {

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

	/**獲取shiro的連接器*/
	public static Subject getSubject() {
		return SecurityUtils.getSubject();
	}

	/**獲取登錄用戶的信息*/
	public static User getUser() {
		return (User) SecurityUtils.getSubject().getPrincipal();
	}

	/**獲取登錄用戶的id*/
	public static String getUserId() {
		return getUser().getId();
	}

	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 void logout() {
		SecurityUtils.getSubject().logout();
	}

	public static String getKaptcha(String key) {
		String kaptcha = getSessionAttribute(key).toString();
		getSession().removeAttribute(key);
		return kaptcha;
	}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM