【问题标题】:Shiro: where credential should be stored?Shiro:凭证应该存储在哪里?
【发布时间】:2014-03-06 15:24:28
【问题描述】:
小史前史:
我开发 RESTful 服务。该服务从 Web 前端接收请求并将其重新发送到具有实际业务逻辑的另一台服务器。我使用 Shiro 来保护我的服务。问题是某些业务逻辑功能需要用户密码。当然,我可以将密码存储在我的主体中,但我认为在其中存储凭据是不正确的。
问题
那么,我应该在什么位置存储凭据以在我的 REST 服务中进行访问?
更新
好的,我也可以在 Shiro 会话中存储密码,但我不认为这是正确的地方。
【问题讨论】:
标签:
security
rest
credentials
shiro
【解决方案1】:
通常,信息保存在 AuthenticationToken 的实现中。此接口有两个方法:getPrincipal(例如登录或电子邮件)和 getCredentials()。最后一个通常用于存储密码。
如果你看一下类UsernamePasswordToken,它是这个接口的一个实现,你会发现这两个确实用于用户名和密码。
现在我们所做的是为我们自己的身份验证机制扩展类 AuthorizingRealm,并在身份验证方法中将令牌存储在主体中。
@Override
public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {
... authentication logic
SimplePrincipalCollection principalCollection = new SimplePrincipalCollection(login, realmName);
principalCollection.add(token, realmName);
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(principalCollection, login.getPasswordHash());
return simpleAuthenticationInfo;
}
现在您可以稍后获取令牌:
PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
AuthenticationToken token = principals.oneByType(AuthenticationToken.class);
【讨论】:
-
我如何理解 AuthenticationToken 或 AuthenticationInfo 实体仅在身份验证过程中使用,然后不存储在应用程序中(我认为这是出于安全原因) .所以,认证后是不可能得到它们的。正如here 所说,看起来 Principal 和 Session 只是我们可以存储一些额外的用户相关信息的实体。
-