All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.penglecode.common.web.shiro.ShiroUtils Maven / Gradle / Ivy

Go to download

commons is a little java tool to make your development easier in your work.

The newest version!
package com.penglecode.common.web.shiro;

import java.util.Iterator;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.Session;

import com.penglecode.common.util.CollectionUtils;

public class ShiroUtils {

	/**
	 * 获取Shiro会话
	 * @return
	 */
	public static Session getSession() {
		return SecurityUtils.getSubject().getSession();
	}
	
	/**
	 * 获取Shiro会话中的attribute值
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static  T getSessionAttribute(String key) {
		return (T) getSession().getAttribute(key);
	}
	
	/**
	 * 设置Shiro会话中的attribute值
	 * @return
	 */
	public static void setSessionAttribute(String key, Object value) {
		getSession().setAttribute(key, value);
	}
	
	@SuppressWarnings("unchecked")
	public static  T getRealm(Class realmType){
		RealmSecurityManager securityManager = (RealmSecurityManager) SecurityUtils.getSecurityManager();
		if(!CollectionUtils.isEmpty(securityManager.getRealms())){
			for(Iterator it = securityManager.getRealms().iterator(); it.hasNext();){
				Realm realm = it.next();
				if(realm.getClass().equals(realmType)){
					return (T) realm;
				}
			}
		}
		return null;
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy