shiro实例(三) MD5

转载:https://my.oschina.net/Tsher2015/blog/655167

测试实例:

package com.sojson;
import static org.junit.Assert.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;
public class testCustomRealmMd5 {

    @Test
    public void testCustomRealmMd5() {

        // 创建securityManager工厂,通过ini配置文件创建securityManager工厂
        Factory<SecurityManager> factory = new IniSecurityManagerFactory(
                "classpath:shiro-realm-md5.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);

    }

}
md5实例

package com.sojson;

import org.apache.shiro.SecurityUtils;
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.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.apache.shiro.util.Factory;
import org.junit.Test;

public class CustomRealmMd5 extends AuthorizingRealm {

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

        // 用于认证
        @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 = "f3694f162729b7d0254c6e40260bf15c";
            // 从数据库获取salt
            String salt = "qwerty";
            // 上边散列值和盐对应的明文:111111

            // 如果查询到返回认证信息AuthenticationInfo
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                    userCode, password, ByteSource.Util.bytes(salt), this.getName());

            return simpleAuthenticationInfo;
        }

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


}
配置文件:

[main]
#\u5b9a\u4e49\u51ed\u8bc1\u5339\u914d\u5668
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
#\u6563\u5217\u7b97\u6cd5
credentialsMatcher.hashAlgorithmName=md5
#\u6563\u5217\u6b21\u6570
credentialsMatcher.hashIterations=1

#\u5c06\u51ed\u8bc1\u5339\u914d\u5668\u8bbe\u7f6e\u5230realm
customRealm=cn.itcast.shiro.realm.CustomRealmMd5
customRealm.credentialsMatcher=$credentialsMatcher
securityManager.realms=$customRealm

输出结果:

是否认证通过:true
posted @ 2017-03-23 17:27  albert_think  阅读(258)  评论(0编辑  收藏  举报