shiro框架权限demo
立即下载
资源介绍:
shiro框架权限demo
package com.yueke.gemini.shiro.realm;
/**
* Created by admin on 2019/2/23.
*/
import com.yueke.gemini.shiro.constants.Global;
import com.yueke.gemini.shiro.entity.Menu;
import com.yueke.gemini.shiro.entity.Role;
import com.yueke.gemini.shiro.entity.User;
import com.yueke.gemini.shiro.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 自定义的指定Shiro验证用户登录的类
* 在本例中定义了2个用户:jadyer和玄玉,jadyer具有admin角色和admin:manage权限,玄玉不具有任何角色和权限
* @User: coding99
*
*/
@Service
public class SystemAuthorizingRealm extends AuthorizingRealm {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private UserService userService;
// @Autowired
// private RoleService roleService;
// @Autowired
// private MenuService menuService;
/**
* 为当前登录的Subject授予角色和权限
* 经测试:本例中该方法的调用时机为需授权资源被访问时
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//获取当前登录的用户名,等价于(String)principals.fromRealm(this.getName()).iterator().next()
String currentAccount = (String) super.getAvailablePrincipal(principals);
List roleNameList = new ArrayList();
List permissionList = new ArrayList();
//从数据库中获取当前登录用户的详细信息
User user = getUser();
if (null != user) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
List roleList = null;// roleService.selectRoleByUserId(user.getId());
Map map = new HashMap();
map.put("userId", user.getId());
map.put("permission", "permission");
List