gpt4 book ai didi

grails - 在 authFail 闭包中检索尝试的密码

转载 作者:行者123 更新时间:2023-12-02 05:44:41 25 4
gpt4 key购买 nike

Grails 1.3.7 + spring-security-core 插件

有没有办法从 authFail 闭包中检索在登录表单中输入的密码?我可以通过以下方式获取用户名

session[UsernamePasswordAuthenticationFilter.SPRING_SECURITY_LAST_USERNAME_KEY] 

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

session[UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_PASSWORD_KEY] 

始终返回 null。

最佳答案

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

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

复制 attemptsAuthentication 的内容并将您需要的任何内容添加到 session 中。在完成所需操作后,从 session 中删除密码是非常明智的做法

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);
}
}

关于grails - 在 authFail 闭包中检索尝试的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8902121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com