Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在用户登录后,我应该如何具体实例化属性(即currentUser)?_Java_Spring_Hibernate_Jsf_Login - Fatal编程技术网

Java 在用户登录后,我应该如何具体实例化属性(即currentUser)?

Java 在用户登录后,我应该如何具体实例化属性(即currentUser)?,java,spring,hibernate,jsf,login,Java,Spring,Hibernate,Jsf,Login,我们的项目是Spring+springsecurity+Maven+Hibernate+JSF+Icefaces 我们需要为currentUser定义值,但只有在用户登录后才能定义 public static UserDetails currentUserDetails(){ SecurityContext securityContext = SecurityContextHolder.getContext(); Authentication authentication = s

我们的项目是Spring+springsecurity+Maven+Hibernate+JSF+Icefaces

我们需要为currentUser定义值,但只有在用户登录后才能定义

public static UserDetails currentUserDetails(){
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    if (authentication != null) {
        Object principal = authentication.getPrincipal();
        return (UserDetails) (principal instanceof UserDetails ? principal : null);
    }
    return null;
}
若我们试图在服务器启动时包含定义,就会抛出一个nullpointerException(如预期的那个样)

我们当前的方法是在getter方法中惰性地定义它。因此,无论何时JSF调用它,都会运行HQL,但这非常昂贵

是否有人可以就如何在用户登录后只运行currentUserDetails方法一次给出建议?

如果在security.xml文件中使用
标记,则可以定义成功登录回调,如

<form-login 
authentication-success-handler-ref="authenticationSuccessHandler"/>

<bean id="authenticationSuccessHandler" class="com.test.MyAuthenticationSuccessHandler"/>

我希望这会有帮助。祝你好运。

登录过程完成后将其保存在会话中?如果HQL在每次getter方法调用上都运行,那么它肯定不是懒惰的。在延迟加载中存在设计问题。每个不同的getter方法都会延迟运行一次。我想预先定义一次userDetails,这样每个getter方法现在都在调用一个已经定义的值。
public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

  @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
   // use authentication object to fetch the user object and update its timestamp
   }
}