gpt4 book ai didi

java - User (UserAccount) 和 Realm 的概念在 Apache Shiro 中如何关联?

转载 作者:搜寻专家 更新时间:2023-10-31 20:12:26 26 4
gpt4 key购买 nike

我正在阅读 Apache Shiro想看看我的心智模型是否正确。

From the docs: "A Realm is a component that can access application-specific security data such as users, roles, and permissions". .. "Realms usually have a 1-to-1 correlation with a data source such as a relational database, LDAP directory, file system, or other similar resource. "

此外,我了解到一个应用程序可能包含多个realms以用于其身份验证和授权目的。

很好,但是这个 Realms 与用户的概念有什么关系?

  • 每个 Realm 都应该是用户空间的一个分区吗?即:User 可能只会出现在 1 个 Realm
  • 或者,这正是我所期待的,Realms 可用于在彼此之上分层身份验证和授权,并且可能在相同的上工作用户。但是在那种情况下,User 在哪里管理?我想它应该在 Realm 外部的某个地方,但是在哪里?

也许我对此感到困惑,因为我将 User 视为单个实体(例如:我只能有一个)。而应该将 User 视为 UserAccount。即:每个 Realm 管理它自己的 Useraccounts(在文档中称为 User),但是一个 User 可能有多个UserAcounts。那是对的吗?

假设以上是正确的:

  • 是否有任何逻辑可以让我查询给定用户的所有 UserAccounts?即:基本上将所有 Useraccounts 合并在一起以获得 User 的完整 View ?
  • 在这种情况下 User 的概念(1 个 User 可能有多个 UserAccounts)是否存在于 Shiro 中?

最佳答案

您在 authenticationStrategy 中定义 Realms 之间的关系。让我们看看这个例子。只有当他通过所有领域的认证时,用户才会被认证。您可以制作自己的 authenticationStrategy 实现,它表示只需一次成功的身份验证就足够了。

在示例中,我们结合 JDBC 领域来存储用户名(无密码)并根据 LDAP 对其进行身份验证。

假设您将添加另一个 LDAP 领域并创建 authenticationStrategy,其中不需要所有针对领域的身份验证。但只需一次成功的 LDAP 身份验证就足够了。

shiro.ini

ds = org.apache.shiro.jndi.JndiObjectFactory
ds.requiredType = javax.sql.DataSource
ds.resourceName = java:comp/env/jdbc/xxx

noPassWordCredentialMatcher = eu.corp.domain.auth.NoPassMatcher

ldapRealm = eu.corp.domain.auth.CustomActiveDirectoryRealm
ldapRealm.searchBase = OU=USERS,OU=EN,DC=our,DC=corp
ldapRealm.url = ldap://our.corp:389
ldapRealm.principalSuffix = @our.corp

jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.permissionsLookupEnabled = true
jdbcRealm.dataSource = $ds
jdbcRealm.credentialsMatcher = $noPassWordCredentialMatcher

jdbcRealm.authenticationQuery = SELECT name FROM auth WHERE name = ?
jdbcRealm.userRolesQuery = SELECT role.shortcut FROM auth LEFT JOIN auth_role ON auth_role.auth_id = auth.id LEFT JOIN role ON role.id = auth_role.role_id WHERE auth.name = ?
jdbcRealm.permissionsQuery = SELECT permission.shortcut FROM role JOIN role_permission ON role_permission.role_id = role.id JOIN permission ON permission.id = role_permission.permission_id WHERE role.shortcut = ?

cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
securityManager.cacheManager = $cacheManager

securityManager.realms = $ldapRealm, $jdbcRealm
authcStrategy = org.apache.shiro.authc.pam.AllSuccessfulStrategy
securityManager.authenticator.authenticationStrategy = $authcStrategy

关于java - User (UserAccount) 和 Realm 的概念在 Apache Shiro 中如何关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19124192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com