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

com.github.dennisit.vplus.data.utils.ShiroUtils Maven / Gradle / Ivy

There is a newer version: 2.0.8
Show newest version
/*--------------------------------------------------------------------------
 *  Copyright (c) 2010-2020, Elon.su All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the elon developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: Elon.su, you can also mail [email protected]
 *--------------------------------------------------------------------------
 */
package com.github.dennisit.vplus.data.utils;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import java.util.Collection;
import java.util.Optional;

/**
 * shiro权限套件
 *
 * @author Elon.su
 */
public class ShiroUtils {

    /**
     * 检测用户是否被授权
     *
     * @return 授权与否
     */
    public static boolean isAuthenticated() {
        return Optional.ofNullable(SecurityUtils.getSubject()).map(x -> x.isAuthenticated()).orElse(false);
    }

    /**
     * 获取客户端IP地址
     *
     * @return 客户端IP地址
     */
    public static String getClientIp() {
        return getSession().getHost();
    }


    /**
     * 获取会话对象
     *
     * @return session会话
     */
    public static Session getSession() {
        return Optional.ofNullable(SecurityUtils.getSubject()).map(x -> x.getSession(true)).orElse(null);
    }

    /**
     * 保存映射到会话
     *
     * @param key   会话key
     * @param value 会话值
     */
    public static void setSessionAttibute(final String key, final Object value) {
        Session session = getSession();
        if (session != null) {
            session.setAttribute(key, value);
        }
    }

    /**
     * 从会话中获取对象
     *
     * @param key 会话key
     * @return 会话值
     */
    public static Object getSessionAttibute(final String key) {
        Object result = null;
        Session session = getSession();
        if (session != null) {
            result = session.getAttribute(key);
        }
        return result;
    }


    /**
     * 登出系统
     */
    public static void logout() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            subject.logout();
        }
    }


    /**
     * 检查用户是否Remembered
     *
     * @return 用户是否Remembered
     */
    public static boolean isRemembered() {
        return Optional.ofNullable(SecurityUtils.getSubject()).map(x -> x.isRemembered()).orElse(false);
    }


    /**
     * 检查用户是有具有某个Permission
     *
     * @param permission 权限
     * @return 是否有权限
     */
    public static boolean isPermitted(final String permission) {
        boolean flag = false;
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            flag = subject.isPermitted(permission);
        }
        return flag;
    }

    /**
     * 检查用户是有具有多个Permission
     *
     * @param permissions 权限
     * @return 是否有权限
     */
    public static boolean isPermittedAll(final Collection permissions) {
        boolean flag = false;
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            flag = subject.isPermittedAll(permissions);
        }
        return flag;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy