【问题标题】:Retrieve the attempted password in the authFail closure在 authFail 闭包中检索尝试的密码
【发布时间】:2012-02-12 16:47:05
【问题描述】:

Grails 1.3.7 + spring-security-core 插件

有没有办法从 authFail 闭包中检索在登录表单中输入的密码?我可以通过

获取用户名
session[UsernamePasswordAuthenticationFilter.SPRING_SECURITY_LAST_USERNAME_KEY] 

但似乎没有办法获取密码。

session[UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_PASSWORD_KEY] 

总是返回 null。

【问题讨论】:

    标签: grails spring-security


    【解决方案1】:

    首先:不要这样做。我敢打赌,你想用它做什么都会破坏安全性。 将密码写入 HTML 表单中的密码字段就是其中一种糟糕的情况。 在会话中存储密码也不是一个好主意。

    回答您的问题:覆盖 UsernamePasswordAuthenticationFilter 并在 spring 配置中使用您的自定义类。

    复制尝试身份验证的内容并将您需要的任何内容添加到会话中。 在完成所需操作后从会话中删除密码是非常明智的

    request.getSession().removeAttribute(SPRING_SECURITY_LAST_PASSWORD_KEY);
    

    过滤器类:

        public class MyFilter extends UsernamePasswordAuthenticationFilter{
        public static final String SPRING_SECURITY_LAST_PASSWORD_KEY = "SPRING_SECURITY_LAST_PASSWORD";
    
    
        public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
            if (postOnly && !request.getMethod().equals("POST")) {
                throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
            }
    
            String username = obtainUsername(request);
            String password = obtainPassword(request);
    
            if (username == null) {
                username = "";
            }
    
            if (password == null) {
                password = "";
            }
    
            username = username.trim();
    
            UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
    
            // Place the last username attempted into HttpSession for views
            HttpSession session = request.getSession(false);
    
            if (session != null || getAllowSessionCreation()) {
                request.getSession().setAttribute(SPRING_SECURITY_LAST_USERNAME_KEY, TextEscapeUtils.escapeEntities(username));
                request.getSession().setAttribute(SPRING_SECURITY_LAST_PASSWORD_KEY, TextEscapeUtils.escapeEntities(password));
            }
    
            // Allow subclasses to set the "details" property
            setDetails(request, authRequest);
    
            return this.getAuthenticationManager().authenticate(authRequest);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-27
      • 2020-11-25
      • 2012-05-06
      • 2015-01-28
      • 2016-09-17
      • 2014-07-21
      • 2013-03-02
      • 2012-11-08
      • 2018-02-11
      相关资源
      最近更新 更多