Shiro入门—认证

  1. 什么是shiro
    shiro是apache的一个开源权限管理框架,实现用户认证、用户授权。
    spring中有spring security (原名Acegi),是一个权限框架,它和spring依赖过于紧密,没有shiro使用简单。
    shiro不依赖于spring,shiro不仅可以实现 web应用的权限管理,还可以实现c/s系统,分布式系统权限管理,shiro属于轻量框架,使用shiro实现系统权限管理,可以有效提高开发效率,降低开发成本。
    先看一下认证和授权时的关键对象:
    subject:主体。理解为用户,可能是程序,要去访问系统的资源,系统需要对subject进行身份认证。
    principal:身份信息。通常是唯一的,一个主体还有多个身份信息,但是都有一个主身份信息(primary principal)
    credential:凭证信息。可以是密码 、证书、指纹。
    主体在进行身份认证时需要提供身份信息和凭证信息。
  2. shiro架构
    subject:主体。可以是用户也可以是程序,主体访问系统,系统对主体进行认证、授权。
    securityManager:安全管理器。主体进行认证和授权都 是通过securityManager进行。
    authenticator:认证器。主体进行认证最终通过authenticator进行的。
    authorizer:授权器。主体进行授权最终通过authorizer进行的。
    sessionManager:web应用中一般是用web容器(如Tomcat)对session进行管理,shiro也提供了一套session管理的方式。
    SessionDao: 通过SessionDao管理session数据,针对个性化的session数据存储需要使用sessionDao。
    cache Manager:缓存管理器。主要对session和授权数据进行缓存,比如将授权数据通过cacheManager进行缓存管理,和ehcache整合对缓存数据进行管理。
    realm:域,领域。相当于数据源,通过realm存取认证、授权相关数据。
    注意:在realm中存储授权和认证的逻辑。
    cryptography:密码管理。提供了一套加密/解密的组件。比如提供常用的散列、加/解密等功能(如Md5)。
  3. shiro认证流程
    这里写图片描述
  4. shiro认证入门程序
    /**
     * @Description 测试登录登出
     * @Author 刘俊重
     * @date 2017年8月1日 
     * @return void
     */
    @Test
    public void testLoginAndLogout(){
        //通过ini配置文件创建SecurityManager工厂
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-first.ini");
        //使用SecurityManagerFactory获取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) {
            e.printStackTrace();
        }
        //获取认证结果
        boolean result = subject.isAuthenticated();
        System.out.println("是否认证通过"+result);
        //退出操作
        subject.logout();
        //再次获取认证结果
        boolean againResult = subject.isAuthenticated();
        System.out.println("第二次认证结果"+againResult);
    }

通过shiro-first.ini配置文件创建SecurityManager工厂,配置文件代码如下:

#对用户信息进行配置
[users]
#用户账号和密码 
zhangsan=111111
lisi=22222

上面程序的执行流程如下:
1通过ini配置文件创建securityManager
2调用subject.login方法主体提交认证,提交的token
3securityManager进行认证,securityManager最终由ModularRealmAuthenticator进行认证。
4ModularRealmAuthenticator调用IniRealm(给realm传入token) 去ini配置文件中查询用户信息
5IniRealm根据输入的token(UsernamePasswordToken)从 shiro-first.ini查询用户信息,根据账号查询用户信息(账号和密码)
如果查询到用户信息,就给ModularRealmAuthenticator返回用户信息(账号和密码)
如果查询不到,就给ModularRealmAuthenticator返回null
6ModularRealmAuthenticator接收IniRealm返回Authentication认证信息
如果返回的认证信息是null,ModularRealmAuthenticator抛出异常(org.apache.shiro.authc.UnknownAccountException)
如果返回的认证信息不是null(说明inirealm找到了用户),对IniRealm返回用户密码 (在ini文件中存在)和 token中的密码 进行对比,如果不一致抛出异常(org.apache.shiro.authc.IncorrectCredentialsException)
5. 自定义Realm
实际开发需要realm从数据库中查询用户信息。所以需要自定义Realm。

package com.catchu.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;
/**
 * @author 刘俊重
 * @Description 自定义Realm
 * @date 2017年8月1日
 */
public class CustomRealm extends AuthorizingRealm {
    /**
     * @Description 用于授权
     * @Author 刘俊重
     * @date 2017年8月1日
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
        return null;
    }
    /**
     * @Description 用于认证
     * @Author 刘俊重
     * @date 2017年8月1日
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {    
        //从token获取principal(身份信息)
        String principal = (String) token.getPrincipal();
        //模拟从数据库中获取到了credentials(授权信息)
        String credentials = "111111";
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(principal, credentials, getName());
        return simpleAuthenticationInfo;
    }
}

单元测试代码如下:

/**
     * @Description 测试自定义realm
     * @Author 刘俊重
     * @date 2017年8月1日 
     * @return void
     */
    @Test
    public void testCustomRealm(){
        //生成SecurityManager工厂
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-realm.ini");
        //使用SecurityManager工厂获取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) {
            e.printStackTrace();
        }
        //获取授权结果
        boolean result = subject.isAuthenticated();
        System.out.println("认证结果:"+result);
    }

shiro-realm.ini配置文件如下:

[main]
#自定义 realm
customRealm=com.catchu.realm.CustomRealm
#将realm设置到securityManager,相当 于spring中注入
securityManager.realms=$customRealm

6.md5散列算法和盐
实际开发中通常需要对密码进行散列,常用的有md5、sha。建议对md5进行散列时加salt(盐),进行加密相当于对原始密码+盐进行散列。
正常使用时散列方法:
在程序中对原始密码+盐进行散列,将散列值和盐存储到数据库中。
在进行密码对比时,使用相同方法,将输入密码+盐进行散列,与数据库存储的散列值进行比对。

import org.apache.shiro.crypto.hash.Md5Hash;
/**
 * @author 刘俊重
 * @Description md5散列测试
 * @date 2017年8月2日
 */
public class Md5Test {
    public static void main(String[] args) {
        //原始密码
        String source = "111111";
        //盐
        String salt = "qwerty";
        //散列次数(就是进行了几次md5加密)
        int hashIterations = 1;
        //散列一次的值:f3694f162729b7d0254c6e40260bf15c
        //散列两次的值:36f2dfa24d0a9fa97276abbe13e596fc
        Md5Hash md5Hash = new Md5Hash(source, salt, hashIterations);
        System.out.println("md5散列后的值:"+md5Hash.toString());
    }
}

7.自定义Realm支持Md5散列
自定义Realm如下:

package com.catchu.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;
import org.apache.shiro.util.ByteSource;
/**
 * @author 刘俊重
 * @Description 自定义Ream实现md5散列加密
 * @date 2017年8月2日
 */
public class CustomRealmMd5 extends AuthorizingRealm {
    /**
     * @Description 用于授权
     * @Author 刘俊重
     * @date 2017年8月2日
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    /**
     * @Description 用于认证
     * @Author 刘俊重
     * @date 2017年8月2日
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {    
        //从token中获取principal(身份信息),在这里指的就是用户名
        String principal = (String) token.getPrincipal();   
        //模拟从数据库中获取到了credentials(授权信息),在这里值的是密码在数据库中的散列值
        String credentials = "f3694f162729b7d0254c6e40260bf15c";
        //从数据库中获取salt(盐)
        String salt = "qwerty";
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(principal, credentials, 
                ByteSource.Util.bytes(salt),getName());
        return simpleAuthenticationInfo;
    }
}

单元测试代码如下:

/**
     * @Description 测试自定义realm,Md5版
     * @Author 刘俊重
     * @date 2017年8月2日 
     * @return void
     */
    @Test
    public void testCustomRealmMd5(){
        //根据ini配置文件生成SecurityManager工厂类
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-realm-md5.ini");  
        //生成SecurityManager(安全管理器)
        SecurityManager securityManager = factory.getInstance();
        //将安全管理器设置在当前运行环境中
        SecurityUtils.setSecurityManager(securityManager);
        //从SecurityUtils中生成subject(主体)
        Subject subject = SecurityUtils.getSubject();
        //在认证前准备token(令牌),正式环境中由用户输入
        UsernamePasswordToken token = new UsernamePasswordToken("zhangsan","111111");
        //提交认证
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
        //是否认证通过
        boolean isAuthenticated = subject.isAuthenticated();
        System.out.println("是否认证通过"+isAuthenticated);
    }

其中生成SecurityManager工厂类的配置文件shiro-realm-md5.ini如下:

[main]
#定义凭证匹配器
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
#散列算法
credentialsMatcher.hashAlgorithmName=md5
#散列次数
credentialsMatcher.hashIterations=1
#将凭证匹配器设置到realm
customRealm=com.catchu.realm.CustomRealmMd5
customRealm.credentialsMatcher=$credentialsMatcher
securityManager.realms=$customRealm

8.引入jar
上面忘记说了,与其它java开源框架类似,将shiro的jar包加入项目就可以使用shiro提供的功能了。shiro-core是核心包必须选用,还提供了与web整合的shiro-web、与spring整合的shiro-spring、与任务调度quartz整合的shiro-quartz等,下边是shiro各jar包的maven坐标。

    <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-web</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-ehcache</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-quartz</artifactId>
            <version>1.2.3</version>
        </dependency>

也可以通过引入shiro-all包括shiro所有的包:

    <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-all</artifactId>
            <version>1.2.3</version>
        </dependency>

参考代码:http://download.csdn.net/detail/u014532775/9920343

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值