【发布时间】:2012-05-11 05:28:33
【问题描述】:
我正在使用使用 Apache Shiro 的 Tapestry-Security
我有一个处理授权和身份验证的自定义领域。从技术上讲,我们的身份验证使用远程服务进行,该服务返回用户名和一组角色。我只是将用户名传递给我的自定义 AuthenticationToken,它允许我查询我们的本地数据库并设置 SimpleAuthenticationInfo。
我不知道如何使用从我们的远程服务返回给我的角色列表来填充 AuthorizationInfo doGetAuthorizationInfo 方法。下面是我用来填充领域的代码。
登录类
//Remote authentication service
RemoteLoginClient client = new RemoteLoginClient();
RemoteSubject authenticate = client.authenticate(username, password);
//tapestry security authentication
Subject currentUser = SecurityUtils.getSubject();
CustomAuthenticationToken token = new
CustomAuthenticationToken(authenticate.getUsername());
System.out.println("roles" + authenticate.getRoles());
currentUser.login(token);
customRealm 中的 AuthorizationInfo 方法 公共类 CustomRealm 扩展 AuthorizingRealm {
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
CustomAuthenticationToken upToken = (CustomAuthenticationToken ) token;
String email = upToken.getUsername();
ApplicationUser applicationUser = (ApplicationUser) session.createCriteria(ApplicationUser.class)
.add(Restrictions.like("email", email + "%"))
.uniqueResult();
if (applicationUser == null) {
throw new UnknownAccountException("User doesn't exist in EPRS database");
}
return buildAuthenticationInfo(applicationUser.getId());
}
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//Not sure how to populate the principle or
//read the principle to populate the SimpleAuthorizationInfo
return new SimpleAuthorizationInfo(roleNames);
}
【问题讨论】:
-
几周前,我写下了如何对付 Shiro 和 Guice。但是,您能否检查一下“实现领域”部分是否涵盖了您的问题 - readyareyou.blogspot.de/2012/03/guice-jersey-shiro.html ?
-
@PepperBob 感谢您的回复,在我看来,您提供给我的链接中的示例仍在使用数据库来查找用户角色。在我的场景中,角色在登录时从我们的 Web 服务列表中提供给我。我想知道如何需要将该角色列表传递到我目前丢失的 SimpleAuthorizationInfo 中。还有其他想法吗?
-
我认为您应该能够将所有内容都粘贴到帐户接口 (shiro.apache.org/static/current/apidocs/org/apache/shiro/authc/…) 的实现中,因为它捆绑了身份验证和授权,例如。 SimpleAccount 或定制的东西。