72 lines
1.7 KiB
Java
72 lines
1.7 KiB
Java
/**
|
|
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
|
*
|
|
* https://www.renren.io
|
|
*
|
|
* 版权所有,侵权必究!
|
|
*/
|
|
|
|
package com.peanut.common.utils;
|
|
|
|
import com.peanut.common.exception.RRException;
|
|
import com.peanut.modules.common.entity.MyUserEntity;
|
|
import com.peanut.modules.sys.entity.SysUserEntity;
|
|
import org.apache.shiro.SecurityUtils;
|
|
import org.apache.shiro.session.Session;
|
|
import org.apache.shiro.subject.Subject;
|
|
|
|
/**
|
|
* Shiro工具类
|
|
*
|
|
* @author Mark sunlightcs@gmail.com
|
|
*/
|
|
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 MyUserEntity getUser() {
|
|
return (MyUserEntity)SecurityUtils.getSubject().getPrincipal();
|
|
}
|
|
|
|
public static Integer getUId() {
|
|
MyUserEntity user = (MyUserEntity)SecurityUtils.getSubject().getPrincipal();
|
|
return user.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 String getKaptcha(String key) {
|
|
Object kaptcha = getSessionAttribute(key);
|
|
if(kaptcha == null){
|
|
throw new RRException("验证码已失效");
|
|
}
|
|
getSession().removeAttribute(key);
|
|
return kaptcha.toString();
|
|
}
|
|
|
|
}
|