Java Spring安全-身份验证';的用户名在AuthenticationSuccessHandler中为空

Java Spring安全-身份验证';的用户名在AuthenticationSuccessHandler中为空,java,spring,authentication,null,spring-security,Java,Spring,Authentication,Null,Spring Security,我有一个Customer类,它包含所有帐户信息。(它不扩展Spring的userdetails.User类) 我尝试在成功登录后做一些事情(例如设置新的上次登录时间)。为了实现这一点,我设置了一个自定义的AuthenticationSuccessHandler 在onAuthenticationSuccess方法中,我尝试从Authentication对象获取用户名。但是,此对象是用户对象。如果我试图从中获取用户名,我会得到null。 我能否设法使权限对象成为客户对象?或者我的Customer类

我有一个Customer类,它包含所有帐户信息。(它不扩展Spring的userdetails.User类) 我尝试在成功登录后做一些事情(例如设置新的上次登录时间)。为了实现这一点,我设置了一个自定义的
AuthenticationSuccessHandler

onAuthenticationSuccess
方法中,我尝试从
Authentication
对象获取用户名。但是,此对象是
用户
对象。如果我试图从中获取
用户名
,我会得到null。 我能否设法使
权限
对象成为
客户
对象?或者我的Customer类必须扩展User类吗

更新

更多详情:

我有我的用户类。它是完全自行编写的,不实现或扩展任何接口/类。我没有实现UserDetailsService的类。我的
applicationContext security.xml的
部分如下所示:

<form-login login-page="/index.htm"
                authentication-success-handler-ref='authSuccHandler'
                authentication-failure-handler-ref='authFailureHandler'
                default-target-url='/library/login.htm'
                always-use-default-target='true'/>

表单重定向到
j\u spring\u security\u check
身份验证
不能是
用户
,因为它们彼此不继承


如果您的
UserDetails服务
生成一个自定义的
UserDetails
,您应该能够通过调用
Authentication
上的
getDetails()
来获取它,我想您正在寻找的方法是getPrincipal on Authentication。然后,您必须对返回自定义类的对象设置大小写

User user = (User)authentication.getPrincipal();

当请求进入身份验证成功处理程序时,它希望您重定向到所需的
URL
。使用以下命令重定向到所需页面,如
home.htm
。 这肯定会有用的! 修改后的代码如下所示。检查一下,如果您有任何问题,请告诉我

 public class PostSuccessfulAuthenticationHandler extends  SimpleUrlAuthenticationSuccessHandler 
  {
        @Autowired
        private UserService userService;

        @Override
        public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
    Authentication authentication) throws ServletException, IOException 
    {
        userService.trackUserLogin(authentication.getName()); //NullPointerException
        response.sendRedirect("home.htm");
        //super.onAuthenticationSuccess(request, response, authentication);
    }
}

我还是不能让它工作。身份验证的getDetails()我的客户类实现了UserDetails,但我仍然得到了一个ClassCastException。如果使用身份验证的getPrincipal(),则返回的对象类型为User,用户名为null。我在这里遗漏了什么?请您添加更多关于您所做的非标准特定配置的详细信息(例如UserDetailsService、自定义筛选器、身份验证提供程序等)。要弄清楚您已经定制了什么,以及您是如何试图覆盖默认的预期Spring安全性实现类集合的,有点困难。另外,请让我们知道您使用的版本。添加了更多详细信息。
 public class PostSuccessfulAuthenticationHandler extends  SimpleUrlAuthenticationSuccessHandler 
  {
        @Autowired
        private UserService userService;

        @Override
        public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
    Authentication authentication) throws ServletException, IOException 
    {
        userService.trackUserLogin(authentication.getName()); //NullPointerException
        response.sendRedirect("home.htm");
        //super.onAuthenticationSuccess(request, response, authentication);
    }
}