第一:只是不要这样做。我敢打赌,无论你想做什么都会破坏安全。 将密码写入HTML表单中的密码字段是其中一种不好的情况。 在会话中存储密码也不是一个好主意。
要回答您的问题:覆盖UsernamePasswordAuthenticationFilter并在弹簧配置中使用您的自定义类。
复制attemptAuthentication的内容并将所需的任何内容添加到会话中。 这将是非常明智的,从会话删除密码,你做了以后有什么需要
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);
}
}