java - 替换 AuthenticationException.getAuthentication();

标签 java spring hibernate

我们将应用程序从 Spring & Hibernate 3.x 升级到 4.x。问题是 AuthenticationException.getAuthentication() 现在已被弃用。任何人都可以建议任何替代方案吗? 下面是我们使用的代码:

public ModelAndView init(HttpServletRequest request, HttpServletResponse response){
    AuthenticationException exception = (AuthenticationException)request.getSession().getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);

    Authentication loginAuthentication = exception.getAuthentication();

    //Set the user name for the change password screen
    return new ModelAndView("common/changePassword", "userName", loginAuthentication.getPrincipal());   
}

有人可以告诉我这个的替代品吗?网上提供的内容不多。

最佳答案

如果在 AuthenticationException 情况下执行以下代码,则意味着您从登录表单提交的用户名和密码在 HttpServletRequest 中仍然可用。所以我认为你可以从那里获取用户名。

public ModelAndView init(HttpServletRequest request, HttpServletResponse response){
    AuthenticationException exception = (AuthenticationException)request.getSession().getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);

    String username = request.getParameter("<Name of your username parameter>");

    //Set the user name for the change password screen
    return new ModelAndView("common/changePassword", "userName", username);   
}

关于java - 替换 AuthenticationException.getAuthentication();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41671347/

相关文章:

java - 我无法修复此 : java. lang.ArrayIndexOutOfBoundsException

java - 使用keytool的 keystore 中 "key password"的意义是什么

java - 使用 HQL 查询的 Hibernate 批量更新

java - 如何获取 Hibernate 对象并将其映射到 JSON

java - 如何让 Ruby 程序与 Java 程序一起工作?

java - 在其他系统上使用 Javamail 连接到 IMAP

带有 quartz 调度程序的 spring 4

java - Spring REST 总是返回 404 Not Found

java - 根据配置文件中的变量使用不同的bean实现

java - Tomcat 7.0 中 Hibernate 的数据源 JNDI 配置