美文网首页
Shiro学习(六) Shiro 加密

Shiro学习(六) Shiro 加密

作者: JiangCheng97 | 来源:发表于2020-02-19 21:29 被阅读0次

    1、创建HashedCredentialsMatcher

    package com.zjc.test;
    
    import com.zjc.shiro.realm.CustomRealm;
    import org.apache.shiro.SecurityUtils;
    import org.apache.shiro.authc.UsernamePasswordToken;
    import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
    import org.apache.shiro.mgt.DefaultSecurityManager;
    import org.apache.shiro.subject.Subject;
    import org.junit.Test;
    
    public class CustomRealmTest {
    
        @Test
        public void customRealmTest(){
    
            CustomRealm customRealm = new CustomRealm();
    
            DefaultSecurityManager manager = new DefaultSecurityManager();
    
            manager.setRealm(customRealm);
    
            //Shiro 加密
            HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
            //设置算法名称
            matcher.setHashAlgorithmName("md5");
            //设置加密次数
            matcher.setHashIterations(1);
    
            customRealm.setCredentialsMatcher(matcher);
    
    
            SecurityUtils.setSecurityManager(manager);
    
            Subject subject = SecurityUtils.getSubject();
    
            UsernamePasswordToken token = new UsernamePasswordToken("zjc","123456");
    
            subject.login(token);
    
            System.out.println("isAuthenticated:"+subject.isAuthenticated());
    
            subject.checkRole("admin");
    
            subject.checkPermission("user:delete");
        }
    }
    
    

    2、自定义Realm(普通MD5加密)

    package com.zjc.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.authz.SimpleAuthorizationInfo;
    import org.apache.shiro.crypto.hash.Md5Hash;
    import org.apache.shiro.realm.AuthorizingRealm;
    import org.apache.shiro.subject.PrincipalCollection;
    import org.apache.shiro.util.ByteSource;
    
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    
    public class CustomRealm extends AuthorizingRealm {
    
        Map<String,String> userMap = new HashMap<>();
    
        {
            //加密后的数据
            userMap.put("zjc","e10adc3949ba59abbe56e057f20f883e");
            super.setName("customRealm");
        }
    
        /**
         * 自定义授权过程
         * @param principals
         * @return
         */
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
            String userName = (String) principals.getPrimaryPrincipal();
    
            //从数据或者缓存中获取角色数据
            Set<String> roles = getRolesByUserName(userName);
    
            //从数据或者缓存中获取权限数据
            Set<String> permissions = getPermissionsByUserName(userName);
    
    
            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
    
            simpleAuthorizationInfo.setStringPermissions(permissions);
            simpleAuthorizationInfo.setRoles(roles);
            return simpleAuthorizationInfo;
    
    
        }
    
        private Set<String> getPermissionsByUserName(String userName) {
            Set<String> sets = new HashSet<>();
            sets.add("user:delete");
            sets.add("user:add");
            return  sets;
    
        }
    
        private Set<String> getRolesByUserName(String userName) {
            Set<String> sets = new HashSet<>();
            sets.add("admin");
            sets.add("user");
            return  sets;
    
        }
    
        /**
         * 自定义认证的过程
         * @param token
         * @return
         * @throws AuthenticationException
         */
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    
            //1、从主体传过来的认证信息中,获得用户名
            String userName = (String) token.getPrincipal();
    
            //2、通过用户名到数据库中获取凭证
            String password = getPasswordByUserName(userName);
    
            if (password == null){
                return null;
            }
    
            SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo("zjc",password,"customRealm");
            return authenticationInfo;
        }
    
        /**
         * 模拟数据库查询凭证
         * @param userName
         * @return
         */
        private String getPasswordByUserName(String userName) {
            return userMap.get(userName);
        }
    
    
        public static void main(String[] args) {
            Md5Hash md5Hash = new Md5Hash("123456");
            System.out.println(md5Hash);
        }
    
    }
    
    

    3、自定义Realm(普通MD5加密+盐)

    package com.zjc.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.authz.SimpleAuthorizationInfo;
    import org.apache.shiro.crypto.hash.Md5Hash;
    import org.apache.shiro.realm.AuthorizingRealm;
    import org.apache.shiro.subject.PrincipalCollection;
    import org.apache.shiro.util.ByteSource;
    
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    
    public class CustomRealm extends AuthorizingRealm {
    
        Map<String,String> userMap = new HashMap<>();
    
        {
            userMap.put("zjc","bfe76993b7209d38fe91de3021fa2b36");
            super.setName("customRealm");
        }
    
        /**
         * 自定义授权过程
         * @param principals
         * @return
         */
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
            String userName = (String) principals.getPrimaryPrincipal();
    
            //从数据或者缓存中获取角色数据
            Set<String> roles = getRolesByUserName(userName);
    
            //从数据或者缓存中获取权限数据
            Set<String> permissions = getPermissionsByUserName(userName);
    
    
            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
    
            simpleAuthorizationInfo.setStringPermissions(permissions);
            simpleAuthorizationInfo.setRoles(roles);
            return simpleAuthorizationInfo;
    
    
        }
    
        private Set<String> getPermissionsByUserName(String userName) {
            Set<String> sets = new HashSet<>();
            sets.add("user:delete");
            sets.add("user:add");
            return  sets;
    
        }
    
        private Set<String> getRolesByUserName(String userName) {
            Set<String> sets = new HashSet<>();
            sets.add("admin");
            sets.add("user");
            return  sets;
    
        }
    
        /**
         * 自定义认证的过程
         * @param token
         * @return
         * @throws AuthenticationException
         */
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    
            //1、从主体传过来的认证信息中,获得用户名
            String userName = (String) token.getPrincipal();
    
            //2、通过用户名到数据库中获取凭证
            String password = getPasswordByUserName(userName);
    
            if (password == null){
                return null;
            }
    
            SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo("zjc",password,"customRealm");
    
            //设置盐
            authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("zjc"));
    
            return authenticationInfo;
        }
    
        /**
         * 模拟数据库查询凭证
         * @param userName
         * @return
         */
        private String getPasswordByUserName(String userName) {
            return userMap.get(userName);
        }
    
    
        public static void main(String[] args) {
            Md5Hash md5Hash = new Md5Hash("123456","zjc");
            System.out.println(md5Hash);
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:Shiro学习(六) Shiro 加密

          本文链接:https://www.haomeiwen.com/subject/xwuzfhtx.html