【问题标题】:Spring Security - Authentication's username is null in AuthenticationSuccessHandlerSpring Security - AuthenticationSuccessHandler 中的身份验证用户名为空
【发布时间】:2012-01-09 19:48:49
【问题描述】:

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

onAuthenticationSuccess 方法中,我尝试从Authentication 对象中获取用户名。然而,这个对象是一个User 对象。如果我尝试从中获取username,我会得到空值。 我可以设法使Authority 对象成为Customer 对象吗?还是我的 Customer 类必须扩展 User 类?

更新

更多细节:

我有我的用户类。它完全是自己编写的,不实现或扩展任何接口/类。我没有实现 UserDetailsS​​ervice 的类。我的applicationContext-security.xml<form-login> 部分如下所示:

<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'/>

authSuccHandler 看起来像这样:(必要的 bean 定义已到位)

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
            super.onAuthenticationSuccess(request, response, authentication);
        }
}

表单重定向到j_spring_security_check

【问题讨论】:

  • 您能否添加更多关于您所做的非标准配置的详细信息(例如 UserDetailsS​​ervice、自定义过滤器、身份验证提供程序等)。准确地梳理出您自定义的内容以及您如何尝试覆盖默认的预期 Spring Security 实现类集有点困难。另外,请告诉我们您使用的版本。
  • 添加了更多细节。

标签: java spring authentication null spring-security


【解决方案1】:

Authentication 不能是 User,因为它们不会相互继承。

如果您的UserDetailsService 生成自定义UserDetails,您应该能够通过在Authentication 上调用getDetails() 来获取它。

【讨论】:

  • 我仍然无法让它工作。身份验证的 getDetails() 我的 Customer 类实现了 UserDetails,但我仍然得到 ClassCastException。如果我使用 Authentication 的 getPrincipal() 返回的对象是 User 类型并且用户名是 null。我在这里错过了什么?
【解决方案2】:

我认为您正在寻找的方法是 getPrincipal on Authentication。然后你必须对返回到你的自定义类的对象进行大小写。

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

【讨论】:

    【解决方案3】:

    当请求进入身份验证成功处理程序时,它希望您重定向到所需的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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      • 2021-03-28
      • 2014-08-09
      • 1970-01-01
      • 2020-11-04
      • 2022-01-15
      • 2014-02-26
      相关资源
      最近更新 更多