java - Shiro Authorization 使用远程角色填充授权

标签 java tapestry shiro

我正在使用使用 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 方法 公共(public)类 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);
}

最佳答案

扩展AuthorizingRealm如果您需要身份验证和授权,这是一个很好的起点。此外,正如 PepperBob 已经说过的,当您使用它时,Account 接口(interface)及其 SimpleAccount 实现在单个接口(interface)中支持身份验证和授权,因此您不必doGetAuthenticationInfo()doGetAuthorizationInfo() 不需要很多单独的代码,并且可以从这两种方法返回相同的对象。

要获取授权信息,需要做两件事:

  • 通过 getAvailablePrincipal() 方法(在 AuthorizingRealm).
  • 加载您的角色并将它们传递给帐户对象上的 setRoles()

...大功告成。

编辑添加:

这是一种非常简单的存储角色的方法,直到您需要它们为止。请注意,实际身份验证是在领域中完成的,它依赖于 RemoteLoginClient

public class MyRealm extends AuthorizingRealm {

    private RemoteLoginClient client = ...;

    private final Map<String, Set<String>> emailToRoles = new ConcurrentHashMap<>();

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
             AuthenticationToken token) {
        final UsernamePasswordToken userPass = (UsernamePasswordToken) token;

        final RemoteSubject authenticate = this.client.authenticate(
            userPass.getUserName(), userPass.getPassword());
        if (authenticate != null) { //assuming this means success
            this.emailToRoles.put(userPass.getUserName(), authenticate.getRoles());
            return new SimpleAuthenticationInfo(...);
        } else {
            return null;
        }
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(
            PrincipalCollection principals) {
         final String username = (String) principals.getPrimaryPrincipal();
         final Set<String> roles = this.emailToRoles.get(username);
         return new SimpleAuthorizationInfo(roles);
    }

}

关于java - Shiro Authorization 使用远程角色填充授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10401903/

相关文章:

tomcat - 如何将 Apache Shiro 指向多个动态生成的 LDAP 服务器?

java - 如何检查 Shiro 元素列表的权限?

java - 事件派发线程如何工作?

java - 检测数字是否在特定范围内

java - JAVA 中的环回替代方案

java - 如何使用 Android 移动应用程序单击网页上的按钮

java - 如何将信息放入 Tapestry5 的输出流中?

Tapestry 单选按钮事件?

tapestry - Tapestry 5中如何获取资源路径

shiro - 在 apache shiro 上为主题添加信息