使用带有 ajax 请求的表单调用 Servlet (FIlter)(必须使用授权 headers)
Call a Servelt (FIlter) using a form with an ajax request (must use authorization headers)
我不明白为什么它不起作用。也许问题出在 url 模式中?或者我不明白数据流?请注意,只有 login.jsp 可以修改。回复看link授权header未设置。我尝试了所有在网上找到的东西,但它不起作用。也许你可以提示我一个教程来做到这一点。提前感谢您的宝贵时间。
login.jsp
<html>
<head>
<title>Login</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#lsubmit").click(function(){
var username = $("#lmail").val();
var password = $("#lpsw").val();
$.ajax({
url : '/seller/*',
method : 'POST',
data: '{"username": "' + username + '", "password" : "' +
password + '"}',
beforeSend : function(req) {
req.setRequestHeader('Authorization', "inserirebase48");
}
});
});
});
</script> </head>
<body>
<form>
Email: <input type="text" id="lmail"><br>
Password: <input type="password" id="lpsw"><br>
<input type="button" id="lsubmit" value="Login">
</form>
</body>
</html>
web.xml的部分
<filter>
<filter-name>filterAuthenticationSeller</filter-name>
<filter-class>servlets.FilterAuthenticationSeller</filter-class>
</filter>
<filter-mapping>
<filter-name>filterAuthenticationSeller</filter-name>
<url-pattern>/seller/*</url-pattern>
</filter-mapping>
FilterAutheniticationSeller.java
package servlets;
import utils.authenticationManager.AuthenticationService;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import static utils.Utils.createResponse_UNAUTHORIZED;
@WebFilter(filterName = "FilterAuthenticationSeller")
public class FilterAuthenticationSeller implements Filter {
AuthenticationService authenticationService = new
AuthenticationService();
public void init(FilterConfig config) { }
public void destroy() { }
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws ServletException, IOException {
if(servletRequest instanceof HttpServletRequest) {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
// if registration : POST (path: '/users' + body) go on the next servlet
// if login : POST (path: '/users/0' + body ) go on the next servlet
if(httpServletRequest.getMethod().equals("POST")
&& httpServletRequest.getServletPath().contains("users")
&& (httpServletRequest.getPathInfo() == null || httpServletRequest.getPathInfo().equals("/0"))) {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
// else, check user and go on OR unauthorized
String authorization = httpServletRequest.getHeader("Authorization");
boolean authenticated = authenticationService.authenticateSeller(authorization);
if(authenticated)
filterChain.doFilter(servletRequest, servletResponse);
else
createResponse_UNAUTHORIZED((HttpServletResponse) servletResponse);
}
}
}
尝试从 ajax post 中删除 seller/* 并指定正确的目标 url
好的 url 是正确的,但现在问题出在 ajax 响应上,我添加
.............
success: function(){
alert('success!');
},
error: function (request, status, error) {
alert(request.status);
}
...........
状态是404,为什么?
我不明白 FilterAutheniticationSeller.java 中的最后两行代码,它是用 "doFilter" 调用自己的吗?
我用我的参数进行了测试和验证,这是真的
我不明白为什么它不起作用。也许问题出在 url 模式中?或者我不明白数据流?请注意,只有 login.jsp 可以修改。回复看link授权header未设置。我尝试了所有在网上找到的东西,但它不起作用。也许你可以提示我一个教程来做到这一点。提前感谢您的宝贵时间。
login.jsp
<html>
<head>
<title>Login</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#lsubmit").click(function(){
var username = $("#lmail").val();
var password = $("#lpsw").val();
$.ajax({
url : '/seller/*',
method : 'POST',
data: '{"username": "' + username + '", "password" : "' +
password + '"}',
beforeSend : function(req) {
req.setRequestHeader('Authorization', "inserirebase48");
}
});
});
});
</script> </head>
<body>
<form>
Email: <input type="text" id="lmail"><br>
Password: <input type="password" id="lpsw"><br>
<input type="button" id="lsubmit" value="Login">
</form>
</body>
</html>
web.xml的部分
<filter>
<filter-name>filterAuthenticationSeller</filter-name>
<filter-class>servlets.FilterAuthenticationSeller</filter-class>
</filter>
<filter-mapping>
<filter-name>filterAuthenticationSeller</filter-name>
<url-pattern>/seller/*</url-pattern>
</filter-mapping>
FilterAutheniticationSeller.java
package servlets;
import utils.authenticationManager.AuthenticationService;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import static utils.Utils.createResponse_UNAUTHORIZED;
@WebFilter(filterName = "FilterAuthenticationSeller")
public class FilterAuthenticationSeller implements Filter {
AuthenticationService authenticationService = new
AuthenticationService();
public void init(FilterConfig config) { }
public void destroy() { }
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws ServletException, IOException {
if(servletRequest instanceof HttpServletRequest) {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
// if registration : POST (path: '/users' + body) go on the next servlet
// if login : POST (path: '/users/0' + body ) go on the next servlet
if(httpServletRequest.getMethod().equals("POST")
&& httpServletRequest.getServletPath().contains("users")
&& (httpServletRequest.getPathInfo() == null || httpServletRequest.getPathInfo().equals("/0"))) {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
// else, check user and go on OR unauthorized
String authorization = httpServletRequest.getHeader("Authorization");
boolean authenticated = authenticationService.authenticateSeller(authorization);
if(authenticated)
filterChain.doFilter(servletRequest, servletResponse);
else
createResponse_UNAUTHORIZED((HttpServletResponse) servletResponse);
}
}
}
尝试从 ajax post 中删除 seller/* 并指定正确的目标 url
好的 url 是正确的,但现在问题出在 ajax 响应上,我添加
.............
success: function(){
alert('success!');
},
error: function (request, status, error) {
alert(request.status);
}
...........
状态是404,为什么? 我不明白 FilterAutheniticationSeller.java 中的最后两行代码,它是用 "doFilter" 调用自己的吗? 我用我的参数进行了测试和验证,这是真的