zoukankan      html  css  js  c++  java
  • IniRealm讲解

    Shiro自定义Realm

    内置Realm:

    user.ini
    [users]
    Mark=123456,admin
    [roles]
    admin=user:delete,user:update
    
    IniRealm
    public class IniRealmTest {
    
        @Test
        public void testAuthentication() {
    
            IniRealm iniRealm = new IniRealm("classpath:user.ini");
    
            // 1. 构建SecurityManager环境
            DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
            defaultSecurityManager.setRealm(iniRealm);
            // 2. 主体提交认证请求
            SecurityUtils.setSecurityManager(defaultSecurityManager);
            Subject subject = SecurityUtils.getSubject();
    
            UsernamePasswordToken token = new UsernamePasswordToken("Mark", "123456");
            subject.login(token);
    
            System.out.println("isAuthenticated:" + subject.isAuthenticated());
    
            subject.checkRole("admin");
    
            subject.checkPermission("user:delete");
        }
    
    }
    
    JdbcRealm
    • 权限表
    id   role_name   permission
    1     admin         user:select
    
    public class JdbcRealmTest {
    
        @Test
        public void testAuthentication() {
    
            DruidDataSource dataSource = new DruidDataSource();
            {
                dataSource.setUrl("jdbc:mysql://localhost:3306/test");
                dataSource.setUsername("root");
                dataSource.setPassword("123456");
            }
    
            JdbcRealm jdbcRealm = new JdbcRealm();
            jdbcRealm.setDataSource(dataSource);
            jdbcRealm.setPermissionsLookupEnabled(true);
    
            String sql = "select password from test_user where user_name = ?";
            jdbcRealm.setAuthenticationQuery(sql);
    
            // 1. 构建SecurityManager环境
            DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
            defaultSecurityManager.setRealm(jdbcRealm);
            // 2. 主体提交认证请求
            SecurityUtils.setSecurityManager(defaultSecurityManager);
            Subject subject = SecurityUtils.getSubject();
    
            UsernamePasswordToken token = new UsernamePasswordToken("Mark", "123456");
            subject.login(token);
    
            System.out.println("isAuthenticated:" + subject.isAuthenticated());
        }
    }
    
    自定义Realm
    • CustomRealm
    public class CustomRealm extends AuthorizingRealm {
    
        Map<String, String> userMap = new HashMap<>(16);
    
        {
            userMap.put("Mark", "123456");
    
            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");
            return authenticationInfo;
        }
    
        private String getPasswordByUserName(String userName) {
            return userMap.get(userName);
        }
    
    }
    
    • CustomRealmTest
    public class CustomRealmTest {
        @Test
        public void testAuthentication() {
            CustomRealm customRealm = new CustomRealm();
    
            // 1. 构建SecurityManager环境
            DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
            defaultSecurityManager.setRealm(customRealm);
    
            // 2. 主体提交认证请求
            SecurityUtils.setSecurityManager(defaultSecurityManager);
            Subject subject = SecurityUtils.getSubject();
    
            UsernamePasswordToken token = new UsernamePasswordToken("Mark", "123456");
            subject.login(token);
    
            System.out.println("isAuthenticated:" + subject.isAuthenticated());
    
            subject.checkRole("admin");
    
            subject.checkPermissions("user:add", "user:delete");
    
    
        }
    
    }
    
  • 相关阅读:
    项目管理--PMBOK 读书笔记(4)【项目整合管理】
    数论(二)
    数论(一)
    Jmeter连接mysql数据库
    minicom工具的使用
    centos7 docker 挂载文件思路
    go语言的init函数
    go操作elasticsearch
    UML交互图
    Linux环境下mysql的安装
  • 原文地址:https://www.cnblogs.com/sanjun/p/10002633.html
Copyright © 2011-2022 走看看