com.penglecode.common.web.shiro.ShiroUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons Show documentation
Show all versions of commons Show documentation
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 extends Realm> 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;
}
}