gpt4 book ai didi

java - 访问资源文件夹时无限循环

转载 作者:太空宇宙 更新时间:2023-11-04 11:13:57 25 4
gpt4 key购买 nike

在我的 spring 项目中,我编写了一个过滤器来处理所有请求。它工作正常,但在访问资源时发生无限循环。

我在扩展 WebMvcConfigurerAdapter 的类下定义了我的资源文件夹这是我用来定义资源文件夹的代码:-

这是我的请求网址:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
http://localhost:8080/abc/resources/images/chart/receivableaccounts.png

通过浏览器访问此网址时,请求在控制台(eclipse)中处理多次。

我无法确定真正的问题是什么。

这是我的请求过滤器:

package org.abc.app.config;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.businesscognizance.app.util.Session;

/**
* <b>Custom filter created for handling all incoming,outgoing
requests</b>
*
* @author Jishnu
*
*/

public class RequestFilter implements Filter {

private static Logger logger =
LogManager.getLogger(RequestFilter.class);

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
String context = httpServletRequest.getContextPath();
String requestUri = httpServletRequest.getRequestURI();
int status = httpServletResponse.getStatus();
System.out.println("hey there>>>>" + requestUri + "check this" + context + "/resources");
if (isAuthenticated(httpServletRequest) || requestUri.equals(context + "/login")
|| requestUri.startsWith(context + "/resources")) {

logger.info("access granted for a device with ip address" + " '" + httpServletRequest.getRemoteAddr() + "' "
+ "for requested url" + " '" + httpServletRequest.getRequestURL() + "'");

if (isAuthorized()) {

} else {
httpServletResponse.reset();
httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
return;

}
chain.doFilter(request, response);

}

else {
httpServletResponse.reset();
if (status == HttpServletResponse.SC_ACCEPTED || status == HttpServletResponse.SC_OK) {
httpServletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
} else {
httpServletResponse.setStatus(status);
}
httpServletResponse.getWriter().println("server rejected your request");

return;

}

}

@Override
public void destroy() {

}

public boolean isAuthorized() {
return true;
}

public boolean isAuthenticated(HttpServletRequest request) {
String accessToken = request.getHeader("access_token");
boolean result = false;
if (accessToken != null) {
if (Session.ifTokenExists(accessToken)) {
result = true;
}

}

return result;
}

}

如果您需要进一步的信息,请告诉我。

任何建议都会有用。

谢谢

最佳答案

这似乎是一个做作的实现:

  1. 预先验证的请求。
  2. 请求静态资源。

这看起来完全脱离了 2003 年的代码库;我建议您首先扩展 OncePerRequestFilter 并使过滤器成为 @Component。由于您的 isAuthorized 方法不执行任何操作,因此您可以通过覆盖 shouldNotFilter 直接绕过某些 URL 的过滤器。方法。您读过 HttpServletResponse 的 Javadoc 了吗?调用reset重置状态代码,所以这种代码是无用的 status == HttpServletResponse.SC_ACCEPTED ||状态== HttpServletResponse.SC_OK

我知道这并不能解决所谓的无限循环问题,但是一旦你清理掉它,你可能就不会再遇到这样的问题了。最后但并非最不重要的一点是,使用调试器来帮助自己;您有代码,我们没有。

关于java - 访问资源文件夹时无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45686798/

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