Search in sources :

Example 1 with ActiveUser

use of com.megagao.production.ssm.domain.customize.ActiveUser in project production_ssm by megagao.

the class CustomRealm method doGetAuthorizationInfo.

/**
 * realm的授权方法
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    // 从 principals获取主身份信息
    // 将getPrimaryPrincipal方法返回值转为真实身份类型(在上边的doGetAuthenticationInfo认证通过填充到SimpleAuthenticationInfo中身份类型),
    ActiveUser activeUser = (ActiveUser) principals.getPrimaryPrincipal();
    // 根据身份信息从数据库获取到权限数据
    List<SysPermission> permissionList = null;
    try {
        permissionList = sysService.findPermissionListByUserId(activeUser.getUserid());
    } catch (Exception e) {
        logger.error(e.getMessage());
    }
    List<String> permissions = new ArrayList<String>();
    if (permissionList != null) {
        for (SysPermission sysPermission : permissionList) {
            permissions.add(sysPermission.getPercode());
        }
    }
    // 查到权限数据,返回授权信息(要包括 上边的permissions)
    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
    // 将上边查询到授权信息填充到simpleAuthorizationInfo对象中
    simpleAuthorizationInfo.addStringPermissions(permissions);
    return simpleAuthorizationInfo;
}
Also used : SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) ActiveUser(com.megagao.production.ssm.domain.customize.ActiveUser) ArrayList(java.util.ArrayList) SysPermission(com.megagao.production.ssm.domain.authority.SysPermission) AuthenticationException(org.apache.shiro.authc.AuthenticationException)

Example 2 with ActiveUser

use of com.megagao.production.ssm.domain.customize.ActiveUser in project production_ssm by megagao.

the class CustomRealm method doGetAuthenticationInfo.

/**
 * realm的认证方法,从数据库查询用户信息
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    // token是用户输入的用户名和密码,第一步从token中取出用户名
    String username = (String) token.getPrincipal();
    // 第二步:根据用户输入的username从数据库查询
    SysUser sysUser = null;
    try {
        sysUser = sysService.getSysUserByName(username);
    } catch (Exception e) {
        logger.error(e.getMessage());
    }
    // 如果查询不到返回null
    if (sysUser == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("user not exist!");
        }
        return null;
    }
    String password = sysUser.getPassword();
    // 如果查询到返回认证信息AuthenticationInfo
    // activeUser就是用户身份信息
    ActiveUser activeUser = new ActiveUser();
    activeUser.setUserid(sysUser.getId());
    activeUser.setUsername(sysUser.getUsername());
    activeUser.setUserStatus(sysUser.getLocked());
    RoleVO sysRole = null;
    try {
        sysRole = roleService.findRoleByUserId(sysUser.getId());
    } catch (Exception e) {
        logger.error(e.getMessage());
    }
    activeUser.setRolename(sysRole.getRoleName());
    activeUser.setRoleStatus(sysRole.getAvailable());
    logger.info(activeUser.getUsername());
    // 根据用户id取出菜单
    List<SysPermission> menus = null;
    try {
        // 通过service取出菜单
        menus = sysService.findMenuListByUserId(sysUser.getId());
    } catch (Exception e) {
        logger.error(e.getMessage());
    }
    // 将用户菜单设置到activeUser
    activeUser.setMenus(menus);
    // ByteSource q = ByteSource.Util.bytes(sysUser.getSalt());
    // 将activeUser设置simpleAuthenticationInfo
    SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(activeUser, password, this.getName());
    return simpleAuthenticationInfo;
}
Also used : RoleVO(com.megagao.production.ssm.domain.vo.RoleVO) SysUser(com.megagao.production.ssm.domain.authority.SysUser) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) ActiveUser(com.megagao.production.ssm.domain.customize.ActiveUser) SysPermission(com.megagao.production.ssm.domain.authority.SysPermission) AuthenticationException(org.apache.shiro.authc.AuthenticationException)

Example 3 with ActiveUser

use of com.megagao.production.ssm.domain.customize.ActiveUser in project production_ssm by megagao.

the class FirstController method home.

// 首页
@RequestMapping("/home")
public String home(HttpSession session, Model model) throws Exception {
    Subject subject = SecurityUtils.getSubject();
    ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
    List<SysPermission> permissionList = null;
    try {
        permissionList = sysService.findPermissionListByUserId(activeUser.getUserid());
    } catch (Exception e) {
        logger.error(ERROR_HAPPENS, e.getMessage());
    }
    List<String> sysPermissionList = CollectionsFactory.newArrayList();
    if (permissionList != null) {
        for (int i = 0; i < permissionList.size(); i++) {
            sysPermissionList.add(permissionList.get(i).getPercode());
        }
    }
    model.addAttribute(ACTIVE_USER, activeUser);
    session.setAttribute("sysPermissionList", sysPermissionList);
    return "home";
}
Also used : ActiveUser(com.megagao.production.ssm.domain.customize.ActiveUser) SysPermission(com.megagao.production.ssm.domain.authority.SysPermission) Subject(org.apache.shiro.subject.Subject) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with ActiveUser

use of com.megagao.production.ssm.domain.customize.ActiveUser in project production_ssm by megagao.

the class AuthorityJudgeController method authorityJudge.

@RequestMapping("*/*_judge")
public Map<String, Object> authorityJudge(HttpServletRequest request) throws Exception {
    Subject subject = SecurityUtils.getSubject();
    ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
    // 根据uri,使用shiro判断相应权限
    String uri = request.getRequestURI();
    String[] names = uri.split("/");
    String featureName = names[2];
    String operateName = names[3].split("_")[0];
    Map<String, Object> map = CollectionsFactory.newHashMap();
    if (!activeUser.getUserStatus().equals("1")) {
        if (logger.isDebugEnabled()) {
            logger.debug(NO_PERMISSION, "账户已被锁定!");
        }
        map.put("msg", "您的账户已被锁定,请切换账户登录!");
    } else if (!activeUser.getRoleStatus().equals("1")) {
        if (logger.isDebugEnabled()) {
            logger.debug(NO_PERMISSION, "角色已被锁定!");
        }
        map.put("msg", "当前角色已被锁定,请切换账户登录!");
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug(NO_PERMISSION, "没有权限!");
        }
        if (!subject.isPermitted(featureName + ":" + operateName)) {
            map.put("msg", "您没有权限,请切换用户登录!");
        }
    }
    return map;
}
Also used : ActiveUser(com.megagao.production.ssm.domain.customize.ActiveUser) Subject(org.apache.shiro.subject.Subject) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ActiveUser (com.megagao.production.ssm.domain.customize.ActiveUser)4 SysPermission (com.megagao.production.ssm.domain.authority.SysPermission)3 AuthenticationException (org.apache.shiro.authc.AuthenticationException)2 Subject (org.apache.shiro.subject.Subject)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 SysUser (com.megagao.production.ssm.domain.authority.SysUser)1 RoleVO (com.megagao.production.ssm.domain.vo.RoleVO)1 ArrayList (java.util.ArrayList)1 SimpleAuthenticationInfo (org.apache.shiro.authc.SimpleAuthenticationInfo)1 SimpleAuthorizationInfo (org.apache.shiro.authz.SimpleAuthorizationInfo)1