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

cn.dreampie.common.util.SubjectUtils Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
package cn.dreampie.common.util;


import cn.dreampie.common.config.AppConstants;
import cn.dreampie.common.util.security.EncriptionUtils;
import cn.dreampie.common.model.User;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.subject.Subject;

/**
 * Created by wangrenhui on 14-4-24.
 */
public class SubjectUtils {

  private static SubjectUtils subjectUtils = new SubjectUtils();

  private static String[] baseRole = new String[]{"R_ADMIN", "R_MANAGER", "R_MEMBER", "R_USER"};

  private SubjectUtils() {
  }

  public static SubjectUtils me() {
    return subjectUtils;
  }

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

  public Session getSession() {
    Subject subject = SecurityUtils.getSubject();
    Session session = subject.getSession();
    if (session == null) {
      throw new UnknownSessionException("Unable found required Session");
    } else {
      return session;
    }
  }

  /**
   * 获取用户对象
   *
   * @param  User
   * @return T User
   */
  public  T getUser() {
    Session session = getSession();
    Object user = getSubject().getPrincipals().getPrimaryPrincipal();
    if (ValidateUtils.me().isNullOrEmpty(user))
      return null;
    else {
      T u = (T) user;
      u.remove("password","hasher","salt");
      u.removeNullValueAttrs();
      return (T) user;
    }
  }

  /**
   * login user
   *
   * @param username 用户名
   * @param password 密码
   * @param user     完整用户对象
   * @param       User
   * @return bolean
   */
  public  boolean login(String username, String password, T user) {
    return login(username, password, false, user);
  }

  public  boolean login(String username, String password, boolean rememberMe, T user) {
    UsernamePasswordToken token = new UsernamePasswordToken(username, password);
    try {
      token.setRememberMe(rememberMe);
      SecurityUtils.getSubject().login(token);
      return true;
    } catch (AuthenticationException e) {
      return false;
    }
  }

  /**
   * 验证验证码
   *
   * @param captchaToken token
   * @return boolean
   */
  public boolean doCaptcha(String captchaToken) {
    Session session = getSession();
    if (session.getAttribute(AppConstants.CAPTCHA_NAME) != null) {
      String captcha = session.getAttribute(AppConstants.CAPTCHA_NAME).toString();
      if (captchaToken != null &&
          captcha.equalsIgnoreCase(EncriptionUtils.encrypt(captchaToken))) {
        return true;
      }
    }
    return false;
  }

  /**
   * 判断是否已经登录
   *
   * @return boolean
   */
  public boolean wasLogin() {
    Subject subject = getSubject();
    if (subject != null && subject.getPrincipal() != null && subject.isAuthenticated()) {
      return true;
    }
    return false;
  }

  public boolean wasBaseRole(String roleValue) {

    if (ArrayUtils.contains(baseRole, roleValue)) {
      return true;
    }
    return false;
  }

  public static String[] getBaseRole() {
    return baseRole;
  }

  public static void setBaseRole(String[] baseRole) {
    SubjectUtils.baseRole = baseRole;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy