首页 > 技术文章 > 人人中的 shiro权限管理 简单说明

rogge7 2017-08-07 11:14 原文

maven  shiro包的引用路径 :C:\Users\yanfazhongxin\.m2\repository\org\apache\shiro\shiro-core\1.3.2\shiro-core-1.3.2.jar

有关的几个文件:
/renren-shiro/src/main/resources/renren-shiro.xml //shiroFilter和 其它bean的配置
/renren-shiro/src/main/java/io/renren/shiro/UserRealm.java //继承于AuthorizingRealm 自定义Realm,认证 和 授权
/renren-shiro/src/main/java/io/renren/shiro/VelocityShiro.java//判断public boolean hasPermission(String permission)  是否有权限,一个方法。
/renren-shiro/src/main/java/io/renren/utils/ShiroUtils.java//Shiro工具类,getSession  getSubject  getKaptcha等操作

 

    

 

    <!-- 继承自AuthorizingRealm的自定义Realm,即指定Shiro验证用户登录的类为自定义的UserRealm.java -->  
    <bean id="userRealm" class="io.renren.shiro.UserRealm"/>

    <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
        <!-- 设置session过期时间为1小时(单位:毫秒),默认为30分钟 -->
        <property name="globalSessionTimeout" value="3600000"></property>
        <property name="sessionValidationSchedulerEnabled" value="true"></property>
    </bean>
    
    <!-- Shiro默认会使用Servlet容器的Session,可通过sessionMode属性来指定使用Shiro原生Session -->  
    <!-- 即<property name="sessionMode" value="native"/>,详细说明见官方文档 -->  
    <!-- 这里主要是设置自定义的单Realm应用,若有多个Realm,可使用'realms'属性代替 -->  
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="sessionManager" ref="sessionManager"></property>
        <property name="realm" ref="userRealm"/>
    </bean>

UserRealm 是一个自定义的可以用来判断:根据输入的用户名 查数据库得到密码 和输入的密码对比,相同,则表示 输入的用户名和密码通过,认证成功。

 ==拓展:课程里面的例子:

// 自定义realm
    @Test
    public void testCustomRealm() {
        // 创建securityManager工厂,通过ini配置文件创建securityManager工厂
        Factory<SecurityManager> factory = new IniSecurityManagerFactory(
                "classpath:shiro-realm.ini");

        // 创建SecurityManager
        SecurityManager securityManager = factory.getInstance();

        // 将securityManager设置当前的运行环境中
        SecurityUtils.setSecurityManager(securityManager);

        // 从SecurityUtils里边创建一个subject
        Subject subject = SecurityUtils.getSubject();

        // 在认证提交前准备token(令牌)
        // 这里的账号和密码 将来是由用户输入进去
        UsernamePasswordToken token = new UsernamePasswordToken("zhangsan",
                "111111");

        try {
            // 执行认证提交
            subject.login(token);
        } catch (AuthenticationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // 是否认证通过
        boolean isAuthenticated = subject.isAuthenticated();

        System.out.println("是否认证通过:" + isAuthenticated);

    }
===== shiro-realm.ini文件
[main]
#自定义 realm
customRealm=cn.itcast.shiro.realm.CustomRealm
#将realm设置到 securityManager,相当于spring中注入
securityManager.realms=$customRealm

 如果:不自定义 realm的话,只能从配置 ini里面读出[users] 段的用户名和密码,就不能用自定义的 realm 的逻辑来判断。

自定义realm要继承于 AuthorizingRealm  :  public class CustomRealm extends AuthorizingRealm { 

package cn.itcast.shiro.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * 
 * <p>
 * Title: CustomRealm
 * </p>
 * <p>
 * Description:自定义realm
 * </p>
 * <p>
 * Company: www.itcast.com
 * </p>
 * 
 * @author 传智.燕青
 * @date 2015-3-23下午4:54:47
 * @version 1.0
 */
public class CustomRealm extends AuthorizingRealm {

    // 设置realm的名称
    @Override
    public void setName(String name) {
        super.setName("customRealm");
    }

    // 用于认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken token) throws AuthenticationException {

        // token是用户输入的
        // 第一步从token中取出身份信息
        String userCode = (String) token.getPrincipal();

        // 第二步:根据用户输入的userCode从数据库查询
        // ....
    

        // 如果查询不到返回null
        //数据库中用户账号是zhangsansan
        /*if(!userCode.equals("zhangsansan")){//
            return null;
        }*/
        
        
        // 模拟从数据库查询到密码
        String password = "111112";

        // 如果查询到返回认证信息AuthenticationInfo

        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                userCode, password, this.getName());

        return simpleAuthenticationInfo;
    }

    // 用于授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(
            PrincipalCollection principals) {
        // TODO Auto-generated method stub
        return null;
    }

}
CustomRealm

 

==========拓展如果要MD5 加盐 来加密(散列)密码

MD5 加盐  import org.apache.shiro.crypto.hash.Md5Hash;

Md5Hash md5Hash = new Md5Hash(userId, "fda45fda", 2); //new Md5Hash(source, salt, hashIterations)
md5Hash.toString();

 通过 ini配置 注入bean(通过spring要百度学习),和定义自定义的 realm

      

 注解 @RequiresPermissions("sys:menu:select")  原来是 shiro的东西,要求必须具备sys:menu:select 权限的才可以往下执行。

 

推荐阅读