【发布时间】:2014-08-25 22:50:42
【问题描述】:
我是 Spring 和 Shiro 平台的新手。
我有两个网址集 /admin/-- 和 /vendor/--。两个客户端集都在使用特定领域进行身份验证。我已扩展 ModularRealmAuthenticator 类以选择正确的域进行身份验证。
ModularRealmAuthenticator.java
@Override
protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
assertRealmsConfigured();
MultiLoginAuthenticationToken mlat = null;
Realm loginRealm = null;
if (!(authenticationToken instanceof MultiLoginAuthenticationToken)) {
throw new AuthenticationException("Unrecognized token , not a typeof MultiLoginAuthenticationToken ");
} else {
mlat = (MultiLoginAuthenticationToken) authenticationToken;
logger.debug("realm name is : {}", mlat.getRealmName());
loginRealm = lookupRealm(mlat.getRealmName());
}
return doSingleRealmAuthentication(loginRealm, mlat);
}
protected Realm lookupRealm(String realmName) throws AuthenticationException {
Collection<Realm> realms = getRealms();
for (Realm realm : realms) {
if (realm.getName().equalsIgnoreCase(realmName)) {
logger.debug("look up realm name is : {}", realm.getName());
return realm;
}
}
throw new AuthenticationException("No realm configured for Client " + realmName);
}
但是,当我将不同数据源集的角色和权限分配给两个客户端(管理员和供应商)时。它按照我在 applicationContext.xml 文件中定义的顺序迭代领域。
我的 ApplicationContext.xml
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="authenticator">
<bean class="com.yatra.mp.security.MultiLoginAuthenticator"/>
</property>
<!-- Single realm app (realm configured next, below). If you have multiple
realms, use the 'realms' property instead. -->
<property name="realms">
<util:list>
<ref bean="adminAuthRealm" />
<ref bean="vendorAuthRealm" />
</util:list>
</property>
<property name="cacheManager" ref="cacheManager" />
</bean>
在这两个领域都扩展了 AuthorizingRealm 类,并且都具有 doGetAuthorizationInfo 和 doGetAuthenticationInfo 方法。我在其中定义了我的自定义实现。
是否需要扩展 ModularRealmAuthorizer 类?如果是,你能告诉我我覆盖了哪种方法吗?
【问题讨论】:
-
不清楚您的实际问题是什么。
-
您好 Wouter,我的问题是:我的应用程序中有两种类型的用户(管理员和供应商)。两种类型的用户都使用两个表进行身份验证,并且都具有不同的权限,这些权限是使用来自同一数据库的不同表进行授权的。我正在为扩展 AuthorizingRealm 类的两种类型的用户使用两个领域进行身份验证和授权。我在上面写了自定义身份验证器,它调用正确的领域进行身份验证。但是在进行授权时,它调用了错误的领域
-
(即领域正在按照我们在 applicationContext.xml 文件中定义的顺序执行授权)。所以我的问题是如何调用正确的领域来授权特定用户?因为两个领域都有 doGetAuthenticationInfo 方法来将角色和权限分配给特定类型的用户。如果您对此有任何其他问题,请告诉我。我从几天开始就陷入了这个问题。请帮助我。提前致谢。