com.hesine.manager.sercurity.SystemAuthorizingRealm.java Source code

Java tutorial

Introduction

Here is the source code for com.hesine.manager.sercurity.SystemAuthorizingRealm.java

Source

/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">Hesine</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.hesine.manager.sercurity;

import com.hesine.manager.service.UserService;
import com.hesine.manager.utils.Constants;
import com.hesine.manager.vo.User;
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.cache.Cache;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * ?
 * @author ThinkGem
 * @version 2013-5-29
 */
@Service
@DependsOn({ "userDao" })
public class SystemAuthorizingRealm extends AuthorizingRealm {

    @Resource
    private UserService userService;

    /**
     * ?, 
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
            throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        User user = userService.getUserByLoginName(token.getUsername());
        if (user != null) {
            return new SimpleAuthenticationInfo(new SystemAuthorizingRealm.Principal(user), user.getPassword(),
                    this.getName());
        } else {
            return null;
        }
    }

    /**
     * ?, ???
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        Principal principal = (Principal) getAvailablePrincipal(principals);
        User user = userService.getUserByLoginName(principal.getLoginName());
        if (user != null) {
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            //         List<Menu> list = UserUtils.getMenuList();
            //         for (Menu menu : list){
            //            if (StringUtils.isNotBlank(menu.getPermission())){
            //               // Permission???
            //               for (String permission : StringUtils.split(menu.getPermission(),",")){
            //                  info.addStringPermission(permission);
            //               }
            //            }
            //         }
            this.setSession(Constants.HTTP_SESSION_LOGIN_USER, user);

            return info;
        } else {
            return null;
        }
    }

    /**
     * ?????
     */
    public void clearCachedAuthorizationInfo(String principal) {
        SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
        clearCachedAuthorizationInfo(principals);
    }

    /**
     * ??
     */
    public void clearAllCachedAuthorizationInfo() {
        Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
        if (cache != null) {
            for (Object key : cache.keys()) {
                cache.remove(key);
            }
        }
    }

    /**
     * ??
     */
    public static class Principal implements Serializable {

        private static final long serialVersionUID = 1L;

        private Long id;
        private String loginName;
        private String name;
        private Map<String, Object> cacheMap;

        public Principal(User user) {
            this.id = user.getId();
            this.loginName = user.getLoginName();
            this.name = user.getName();
        }

        public Long getId() {
            return id;
        }

        public String getLoginName() {
            return loginName;
        }

        public String getName() {
            return name;
        }

        public Map<String, Object> getCacheMap() {
            if (cacheMap == null) {
                cacheMap = new HashMap<String, Object>();
            }
            return cacheMap;
        }

    }

    /**
     * ?ShiroSession,
     *
     */
    private void setSession(Object key, Object value) {
        Subject currentUser = SecurityUtils.getSubject();
        if (null != currentUser) {
            Session session = currentUser.getSession();
            System.out.println("Session[" + session.getTimeout() + "]");
            if (null != session) {
                session.setAttribute(key, value);
            }
        }
    }
}