zoukankan      html  css  js  c++  java
  • shiro学习(三,shiro加密)

    shiro加密

     使用MD5加密  认证

    //自定义的Realm  域
    public class CustomRealmSecret extends AuthorizingRealm {
    
        @Override  //授权
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
            String username = (String) principals.getPrimaryPrincipal();
            //通过用户名username,获取角色
            Set<String> roles=getRolesByUsername(username);
            //通过用户名username,获取角色de权限
            Set<String> permissions=getPermissionsByUsrname(username);
    
            //返回AuthorizationInfo对象,先创建
            SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
            //设置授权  ---角色
            info.setRoles(roles);
            //设置授权  ---权限
            info.setStringPermissions(permissions);
            return info;
        }
    
        private Set<String> getPermissionsByUsrname(String username) {
            //也是从数据库去,这里写死
            Set<String> permissions=new HashSet<>();
            permissions.add("user:select");
            permissions.add("user:update");
            permissions.add("user:delete");
            permissions.add("user:insert");
            return permissions;
        }
    
        private Set<String> getRolesByUsername(String username) {
            //也是从数据库去,这里写死
            Set<String> roles=new HashSet<>();
            roles.add("admin");
            roles.add("user");
            return roles;
        }
    
        @Override  //renz
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
            String username = (String) token.getPrincipal();
            String pwd=getPswByUsername(username);
            if (pwd==null){
                return null;
            }
            SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(username,pwd,"customRealmSecret");
    
            return info;
        }
    
        private String getPswByUsername(String username) {
            Map<String,String> map=new HashMap<>();
            //加密的密码admin   --->21232f297a57a5a743894a0e4a801fc3
            map.put("admin","21232f297a57a5a743894a0e4a801fc3");
            super.setName("customRealmSecret");
            return map.get(username);
        }
    
        @Test
        public void MD5Test() {
            Md5Hash md5Hash=new Md5Hash("admin");
            System.out.println(md5Hash);
        }
    }

    测试类

    public class CustomRealmSecretTest {
        @Test
        public void testcustomRealmSecret(){
            CustomRealmSecret realmSecret=new CustomRealmSecret();
    
            //密码加密
            HashedCredentialsMatcher matcher=new HashedCredentialsMatcher();
            //使用MD5加密
            matcher.setHashAlgorithmName("MD5");
            //加密次数 1次
            matcher.setHashIterations(1);
            //自定的域中加入加密
            realmSecret.setCredentialsMatcher(matcher);
    
    
            DefaultSecurityManager securityManager=new DefaultSecurityManager();
            securityManager.setRealm(realmSecret);
    
            SecurityUtils.setSecurityManager(securityManager);
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token=new UsernamePasswordToken("admin","admin");
            //认证
            subject.login(token);
            System.out.println(subject.isAuthenticated());
            //授权
    //        subject.checkRoles("admin","user");
    //        subject.checkPermissions("user:delete","user:update");
    
        }
    }

    使用MD5,再  加盐salt 

    测试类(其实没有变到,和之前代码一样,变的是自定义realm类,realm添加了盐salt的操作)

    public class CustomRealmSecretTest {
        @Test
        public void testcustomRealmSecret(){
            CustomRealmSecret realmSecret=new CustomRealmSecret();
    
            //密码加密
            HashedCredentialsMatcher matcher=new HashedCredentialsMatcher();
            //使用MD5加密
            matcher.setHashAlgorithmName("MD5");
            //加密次数 1次
            matcher.setHashIterations(1);
            //自定的域中加入加密
            realmSecret.setCredentialsMatcher(matcher);
    
    
            DefaultSecurityManager securityManager=new DefaultSecurityManager();
            securityManager.setRealm(realmSecret);
    
            SecurityUtils.setSecurityManager(securityManager);
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token=new UsernamePasswordToken("admin","admin");
            //认证
            subject.login(token);
            System.out.println(subject.isAuthenticated());
            //授权
    //        subject.checkRoles("admin","user");
    //        subject.checkPermissions("user:delete","user:update");
    
        }
    }

    自定义realm//自定义的Realm 域

    //自定义的Realm  域
    public class CustomRealmSecret extends AuthorizingRealm {
    
        @Override  //授权
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
            String username = (String) principals.getPrimaryPrincipal();
            //通过用户名username,获取角色
            Set<String> roles=getRolesByUsername(username);
            //通过用户名username,获取角色de权限
            Set<String> permissions=getPermissionsByUsrname(username);
    
            //返回AuthorizationInfo对象,先创建
            SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
            //设置授权  ---角色
            info.setRoles(roles);
            //设置授权  ---权限
            info.setStringPermissions(permissions);
            return info;
        }
    
        private Set<String> getPermissionsByUsrname(String username) {
            //也是从数据库去,这里写死
            Set<String> permissions=new HashSet<>();
            permissions.add("user:select");
            permissions.add("user:update");
            permissions.add("user:delete");
            permissions.add("user:insert");
            return permissions;
        }
    
        private Set<String> getRolesByUsername(String username) {
            //也是从数据库去,这里写死
            Set<String> roles=new HashSet<>();
            roles.add("admin");
            roles.add("user");
            return roles;
        }
    
        @Override  //renz
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
            String username = (String) token.getPrincipal();
            String pwd=getPswByUsername(username);
            if (pwd==null){
                return null;
            }
            SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(username,pwd,"customRealmSecret");
            info.setCredentialsSalt(ByteSource.Util.bytes("salt"));
            return info;
        }
    
        private String getPswByUsername(String username) {
            Map<String,String> map=new HashMap<>();
            //加密的密码admin再加盐   --->c657540d5b315892f950ff30e1394480
            map.put("admin","c657540d5b315892f950ff30e1394480");
            super.setName("customRealmSecret");
            return map.get(username);
        }
    
        @Test
        public void MD5Test() {
            Md5Hash md5Hash=new Md5Hash("admin","salt");
            System.out.println(md5Hash);
            //c657540d5b315892f950ff30e1394480
        }
    }
  • 相关阅读:
    (转)Java 调用 C++ (Java 调用 dll)
    用Gvim建立IDE编程环境 (Windows篇)
    (转)python调取C/C++的dll生成方法
    C/C++协程的实现方式总结
    时钟周期,机器周期,指令周期,总线周期
    (转)MongoDB和Redis区别
    linux cpu占用100%排查
    (转)linux 打开文件数 too many open files 解决方法
    Python下载网页图片
    python抓网页数据【ref:http://www.1point3acres.com/bbs/thread-83337-1-1.html】
  • 原文地址:https://www.cnblogs.com/shiguanzui/p/11883439.html
Copyright © 2011-2022 走看看