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

com.maxplus1.access.starter.config.shiro.utils.ShiroUtils Maven / Gradle / Ivy

The newest version!
package com.maxplus1.access.starter.config.shiro.utils;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.SimplePrincipalMap;

import java.util.Map;

public class ShiroUtils {

    public static final String USER_ID = "userId";
    public static final String USER_NAME = "userName";

    public static String getUserId() {
        SimplePrincipalMap simplePrincipalMap = (SimplePrincipalMap) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        for (Map.Entry entry : simplePrincipalMap.entrySet()) {
            if(USER_ID.equals(entry.getKey())){
                return entry.getValue().toString();
            }
        }
        return null;
    }


    public static String getUserName() {
        SimplePrincipalMap simplePrincipalMap = (SimplePrincipalMap) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        for (Map.Entry entry : simplePrincipalMap.entrySet()) {
            if(USER_NAME.equals(entry.getKey())){
                return entry.getValue().toString();
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy