zoukankan      html  css  js  c++  java
  • Shiro加密

    Shiro散列配置
    • HashedCredentialsMatcher
    • 自定义Realm中使用散列
    • 盐的使用
    代码演示
    public class CustomRealm extends AuthorizingRealm {
    
        Map<String, String> userMap = new HashMap<>(16);
    
        {
            userMap.put("Mark", "283538989cef48f3d7d8a1c1bdf2008f");
    
            super.setName("customRealm");
        }
    
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
            String userName = (String) principalCollection.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;
        }
    
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
            // 1. 从主体传过来的认证信息中,获得用户名
            String userName = (String) authenticationToken.getPrincipal();
    
            // 2. 通过用户名到数据库中获取凭证
            String password = getPasswordByUserName(userName);
            if (password == null) {
                return null;
            }
            SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo("Mark", password, "customRealm");
            authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("Mark"));
            return authenticationInfo;
        }
    
        private String getPasswordByUserName(String userName) {
            return userMap.get(userName);
        }
    
        public static void main(String[] args) {
            Md5Hash md5Hash = new Md5Hash("123456", "Mark");
            System.out.println(md5Hash.toString());
        }
    }
    
    public class CustomRealmTest {
        @Test
        public void testAuthentication() {
            CustomRealm customRealm = new CustomRealm();
    
            // 1. 构建SecurityManager环境
            DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
            defaultSecurityManager.setRealm(customRealm);
    
            HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
            matcher.setHashAlgorithmName("md5");
            matcher.setHashIterations(1);
            customRealm.setCredentialsMatcher(matcher);
    
            // 2. 主体提交认证请求
            SecurityUtils.setSecurityManager(defaultSecurityManager);
            Subject subject = SecurityUtils.getSubject();
    
            UsernamePasswordToken token = new UsernamePasswordToken("Mark", "123456");
            subject.login(token);
    
            System.out.println("isAuthenticated:" + subject.isAuthenticated());
        }
    
    }
    
  • 相关阅读:
    php常用函数
    检测到有潜在危险的 Request.Form 值
    未能加载文件或程序集“XXX”或它的一个依赖项,试图加载格式不正确的程序
    尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题
    java基础知识
    .net中excel遇到的一些问题
    easyui验证
    .NET牛人需要了解的问题[转]
    关于easyui遇到的一些问题
    MVC 路由介绍
  • 原文地址:https://www.cnblogs.com/sanjun/p/10003680.html
Copyright © 2011-2022 走看看