Shiro核心——Realm


在Shiro中,Realm是一个非常灵活和强大的安全组件,它能够与各种数据源进行集成,满足各种安全需求。通过实现自定义的Realm,我们可以轻松地定制身份验证、授权和加密逻辑,实现更加细粒度的访问控制。在使用Realm时,我们需要将其配置到SecurityManager中,并通过配置文件或编程方式来指定使用哪些Realm。Shiro提供了多个默认的Realm实现,例如IniRealm和JdbcRealm,我们也可以通过继承AbstractAuthorizingRealm或AbstractAuthenticatingRealm来创建自定义的Realm实现。

Realm的作用

Realm的主要作用是执行身份验证和授权操作。当一个Subject需要进行身份验证时,它会调用SecurityManager中的authenticate方法,该方法会委托给所有配置的Realm来进行身份验证。当验证成功后,Realm会返回一个SimpleAuthenticationInfo对象,其中包含了身份验证信息(如用户名、密码等),这些信息会在会话管理中使用。

当一个Subject需要进行授权操作时,它会调用SecurityManager中的authorize方法,该方法会委托给所有配置的Realm来进行授权操作。Realm会返回一个AuthorizationInfo对象,其中包含了该Subject的所有权限信息(如角色、权限等),这些信息会被用于控制Subject在应用程序中的访问权限。

除了身份验证和授权操作之外,Realm还可以用于加密和解密操作。例如,使用Realm中的CredentialsMatcher接口可以对密码进行加密和验证。

Realm的实现

Realm是一个接口,它定义了一系列方法,用于处理身份验证和授权操作。在实际应用中,我们可以通过实现自定义的Realm来满足不同的安全需求。Shiro提供了多个默认的Realm实现,包括:

  • IniRealm:通过一个INI配置文件来存储用户、角色和权限信息。
  • JdbcRealm:通过JDBC来访问数据库,存储用户、角色和权限信息。
  • ActiveDirectoryRealm:通过LDAP协议来访问Active Directory,存储用户、角色和权限信息。
  • LdapRealm:通过LDAP协议来访问LDAP服务器,存储用户、角色和权限信息。
  • TextConfigurationRealm:通过一个文本文件来存储用户、角色和权限信息。

此外,我们也可以通过继承AbstractAuthorizingRealm或AbstractAuthenticatingRealm来创建自定义的Realm实现。

Realm的配置

在使用Realm时,我们需要将其配置到SecurityManager中。Shiro提供了一个ini配置文件来方便地进行配置。在ini配置文件中,我们可以使用[main]部分来配置SecurityManager,并通过配置securityManager.realms属性来指定使用哪些Realm。
例如,下面的配置文件中使用了IniRealm和JdbcRealm来实现身份验证和授权操作:

[main]
securityManager.realms = $iniRealm, $jdbcRealm

[iniRealm]
# IniRealm的配置

[jdbcRealm]
# JdbcRealm的配置

在实际应用中,我们也可以通过编程方式来配置Realm,例如:

IniRealm iniRealm = new IniRealm("classpath:shiro.ini");
JdbcRealm jdbcRealm = new JdbcRealm();
securityManager.setRealms(Arrays.asList(iniRealm, jdbcRealm));

实例

下面是一个自定义的Realm实现,该Realm实现基于数据库进行身份验证和授权操作:

public class CustomRealm extends AuthorizingRealm {
    
    

    private UserService userService; // UserService为自定义的用户服务接口

    public CustomRealm(UserService userService) {
    
    
        this.userService = userService;
    }

    /**
     * 认证方法,用于身份验证
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    
    
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        String username = upToken.getUsername();
        User user = userService.getUserByUsername(username);

        if (user == null) {
    
    
            throw new UnknownAccountException("用户不存在");
        }

        String password = user.getPassword(); // 从数据库中获取密码
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, password, getName());
        return info;
    }

    /**
     * 授权方法,用于权限控制
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    
    
        String username = (String) principals.getPrimaryPrincipal();
        User user = userService.getUserByUsername(username);

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        for (Role role : user.getRoles()) {
    
    
            info.addRole(role.getName()); // 添加角色
            for (Permission permission : role.getPermissions()) {
    
    
                info.addStringPermission(permission.getName()); // 添加权限
            }
        }

        return info;
    }
}

上述代码中,CustomRealm继承了AuthorizingRealm类,并实现了doGetAuthenticationInfodoGetAuthorizationInfo方法,用于身份验证和授权操作。在认证方法中,我们首先从传入的token中获取用户名,然后通过userService从数据库中获取用户信息,如果用户不存在则抛出异常,否则将密码信息封装在SimpleAuthenticationInfo对象中返回。

在授权方法中,我们首先从PrincipalCollection中获取用户名,然后通过userService从数据库中获取用户信息。我们将用户的所有角色和权限信息添加到SimpleAuthorizationInfo对象中,并将该对象返回。这样,当Subject需要进行授权操作时,CustomRealm就会被调用,返回该Subject的所有权限信息。

需要注意的是,CustomRealm中的userService需要通过构造函数进行注入,以便从外部获取用户信息。另外,在使用CustomRealm时,我们需要将其配置到SecurityManager中。例如,下面的代码演示了如何将CustomRealm配置到SecurityManager中:

CustomRealm customRealm = new CustomRealm(userService);
DefaultSecurityManager securityManager = new DefaultSecurityManager();
securityManager.setRealm(customRealm);
SecurityUtils.setSecurityManager(securityManager);

通过上述代码,我们可以将CustomRealm配置到SecurityManager中,并使用该SecurityManager进行身份验证和授权操作。
除了上述示例中的基于数据库的自定义Realm实现,我们还可以根据具体的业务需求,实现各种自定义Realm,例如:

  • 基于LDAP的Realm:通过LDAP协议验证用户身份和获取用户授权信息。
  • 基于OAuth的Realm:通过OAuth协议验证用户身份和获取用户授权信息。
  • 基于第三方API的Realm:通过调用第三方API验证用户身份和获取用户授权信息。

下面是一个基于LDAP的自定义Realm示例:

public class LdapRealm extends AuthenticatingRealm {
    
    

    private LdapContextFactory contextFactory;

    public LdapRealm(LdapContextFactory contextFactory) {
    
    
        this.contextFactory = contextFactory;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    
    
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        String username = upToken.getUsername();
        String password = new String(upToken.getPassword());

        try {
    
    
            LdapContext ldapContext = contextFactory.getLdapContext(username, password);
            ldapContext.close();
        } catch (AuthenticationException e) {
    
    
            throw new IncorrectCredentialsException("用户名或密码不正确");
        } catch (NamingException e) {
    
    
            throw new AuthenticationException("无法连接到LDAP服务器");
        }

        return new SimpleAuthenticationInfo(username, password, getName());
    }
}

在上述代码中,我们实现了基于LDAP协议的身份验证逻辑。我们通过 LdapContextFactory获取LDAP连接,并使用用户名和密码进行身份验证。如果验证失败,则抛出异常;否则将用户名和密码封装在SimpleAuthenticationInfo对象中返回。

需要注意的是,LdapContextFactory是一个自定义的工厂类,用于创建LDAP连接。该工厂类需要根据具体的LDAP服务器配置进行实现。

通过自定义Realm实现,我们可以灵活地扩展Shiro的功能,满足各种不同的安全需求。

猜你喜欢

转载自blog.csdn.net/qq_54351538/article/details/129397343