你不能传递任何信息,直接与Hibernate或者JPA API的回调方法。
但还有另一种常用的解决方案:ThreadLocal
一个ThreadLocal
存储当前正在运行的线程的静态变量。由于请求通常只在一个线程中执行,因此可以从回调方法/侦听器访问该信息。一些UI框架为您创建了一个ThreadLocal
。
例如JSF提供了一个与FacesContext.getCurrentInstance()
。所以在JSF,你可以拨打:
FacesContext.getCurrentInstance().getExternalContext().getRemoteUser()
或者在春与RequestContextHolder
:
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getRemoteUser()
如果你有没有这样的框架,你可以建立自己的ThreadLocal
:
public final class UserIdHelper {
// You can add the static ThreadLocal to any class, just as an example
private static final ThreadLocal<String> userIds = new ThreadLocal<>();
public static void setCurrentUserId(String userId) {
userIds.set(userId);
}
public static void getCurrentUserId() {
return userIds.get();
}
public static void removeCurrentUserId() {
return userIds.remove();
}
}
现在,您可以设置用户id在Filter
或只是围绕y我们呼吁JPA:
UserIdHelper.setCurrentUserId(request.getRemoteUser());
try {
// ... Execute your JPA calls ...
} finally {
UserIdHelper.removeCurrentUserId();
}
重要的是要去除用户id在finally
块 - 否则在同一个线程中运行的后续请求可能“劫持”你以前用户id。
要访问您的回调方法的信息:
@PrePersist
private void prePersist() {
this.createdBy = UserIdHelper.getCurrentUserId();
}
你任何机会使用弹簧(安全)? – Forkmohit
@Forkmohit不,我没有使用它。 – user3359005