Java 如何在servlet容器中使用ApacheShiro仅在http请求期间缓存数据?
更具体地说,我发现我正在实现一个定制的Java 如何在servlet容器中使用ApacheShiro仅在http请求期间缓存数据?,java,jakarta-ee,servlets,shiro,Java,Jakarta Ee,Servlets,Shiro,更具体地说,我发现我正在实现一个定制的authorizationrealm,它分别声明了用于返回AuthorizationInfo和AuthorizationInfo对象的模板方法doGetAuthorizationInfo() 但是,当我在doGetAuthenticationInfo()中检索AuthenticationInfo(JPA实体)的数据时,我发现我已经拥有必要的AuthorizationInfo。唉,没有明显的好方法来保存这些数据,所以我不得不扔掉它,只是为了在授权过滤器最终在过
authorizationrealm
,它分别声明了用于返回AuthorizationInfo
和AuthorizationInfo
对象的模板方法doGetAuthorizationInfo()
但是,当我在doGetAuthenticationInfo()中检索AuthenticationInfo(JPA实体)的数据时,我发现我已经拥有必要的AuthorizationInfo。唉,没有明显的好方法来保存这些数据,所以我不得不扔掉它,只是为了在授权过滤器最终在过滤器链中轮到它时执行另一个JPA查找
瞧:
public class CustomRealm extends AuthorizingRealm {
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {
UsernamePasswordToken userPassToken = (UsernamePasswordToken) token;
String username = userPassToken.getUsername()
User user; // Contains username, password, and roles
// Perform JPA lookup by username...
return constructSimpleAuthenticationInfoFromUser(user);
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// Look up user again? :(
...
}
}
我考虑了许多可能性:
谢谢 我会选择选项4-使用
ThreadLocal
对象,因为您的需求明确指出对象的生存期必须是http请求
请看下面的讨论:
本地文件: