gpt4 book ai didi

java - 使用带有 ajax 请求的表单调用 Servlet (FIlter)(必须使用授权 header )

转载 作者:行者123 更新时间:2023-12-02 04:33:26 26 4
gpt4 key购买 nike

我不明白为什么它不起作用。也许问题出在 url 模式上?或者我不明白数据流?注意只能修改login.jsp。响应查看链接授权 header 未设置。我尝试了网上找到的所有东西,但它不起作用。也许你可以提示我一个教程来做到这一点。预先感谢您的宝贵时间。

登录.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帖子中删除seller/*并指定正确的目标url

关于java - 使用带有 ajax 请求的表单调用 Servlet (FIlter)(必须使用授权 header ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56563457/

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