应用程序记录所有请求的 url
。这意味着,不要使用 url 参数进行身份验证至关重要,因为这会导致日志中充满 (login=abc&password=123)
对的情况。为此,我将 spring-security
配置为从 request-body
读取参数。这是通过将以下行添加到 request-header
来完成的:
'Content-Type': 'application/x-www-form-urlencoded'
body 会是:
{'login':'admin', 'password':'password'}
没关系,但 QA 强制我禁用通过 url 参数进行身份验证的可能性。目前,对以下 URL 的 POST 也将进行身份验证:
https://example.com/foo?login=admin&password=password
有人知道禁用此选项的技巧吗?最好带注释。
由于评论,我决定为我的问题添加更多细节。我的 spring-security
配置了 WebSecurityConfigurerAdapter
。我有
http.usernameParameter("login")
.passwordParameter("password")
(...)
这使得 Spring
在参数和正文中搜索登录数据。我希望禁用在 url 中搜索这些参数。
最佳答案
This makes Spring searching login data in both - parameters and body. I wish to disable searching those parameters in the url.
我相信这是不可能的,因为这种行为不是由 Spring 而不是由 JavaEE 本身实现的。
HttpServletRequest.getParameter 文档状态:
Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.
但是您可以尝试使用看起来像这样的过滤器来改变它:
public class DisableGetAuthFiler extends OncePerRequestFilter {
...
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
filterChain.doFilter(
new HttpServletRequestWrapper(request) {
@Override
public String getParameter(String name) {
if (("login".equals(name) && getQueryString().contains("login"))
|| ("password".equals(name) && getQueryString().contains("password"))) {
return null;
} else {
return super.getParameter(name);
}
}
},
response
);
}
}
编辑 Haim Raman建议另一个solution使用现有过滤器而不是引入新过滤器。只有我会建议覆盖 obtainUsername()
和 obtainPassword()
而不是 attemptAuthentication()
。
关于java - 如何禁用解析作为 url 参数/从 url 传递的登录参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38716703/