简体   繁体   English

Java Servlet Filter重定向问题

[英]Java Servlet Filter redirect problem

I'm having a problem with my authentication filter. 我的身份验证过滤器出了问题。 When the filter redirects to the login page, no images gets displayed in the login JSP. 当过滤器重定向到登录页面时,登录JSP中不会显示任何图像。 However, if I go to the login page manually after I'm logged in, the images are displayed. 但是,如果我在登录后手动进入登录页面,则会显示图像。

I don't understand why this is happening! 我不明白为什么会这样! I appreciate any help. 我感谢任何帮助。 :-) :-)

AuthFilter: AuthFilter筛选:

if (authorized == null && path.indexOf("Auth") == -1 && path.indexOf("Login") == -1 ) {
        httpResponse.sendRedirect("Login");  
        return;  
} else {  
        chain.doFilter(request, response);  
}

Login servlet: 登录servlet:

// Just using a servlet in case I want more data sent to the jsp
Dispatcher.dispatch("views/login.jsp", request, response);

login.jsp: login.jsp的:

<img src="images/logo.png" />

The jsp is otherwise "normal", all required HTML tags are present. jsp在其他方面是“正常的”,所有必需的HTML标记都存在。 The "images" folder is in the default "web" folder of the project, alongside all the other jsp's and javascripts. “images”文件夹位于项目的默认“web”文件夹中,与所有其他jsp和javascripts一起。

Thanks in advance for any help. 在此先感谢您的帮助。 :) :)
- Stian - Stian

It's because of the relative paths. 这是因为相对路径。

  • your Login is in the root of the context 您的Login位于上下文的根目录中
  • your images probably are /views/images/ 你的图片可能是/views/images/
  • when you forward, the browser knows only the requested URL. 转发时,浏览器只知道请求的URL。

So when you forward, the images are sought at /images (because they are relative to the current address) instead of /views/images/ 所以当你前进时,图像是在/images (因为它们是相对于当前地址)而不是/views/images/

How to resolve it. 如何解决它。 Two options: 两种选择:

  • don't forward from your servlet; 不要从你的servlet转发; redirect instead 改为重定向
  • don't redirect to the servlet from the filter; 不要从过滤器重定向到servlet; redirect to the login page directly 直接重定向到登录页面

Update: Make sure the images are NOT affected by the filter. 更新:确保图像不受过滤器影响。 two options: 两种选择:

  • they should not be matched by the filter pattern 它们不应与过滤器模式匹配
  • redirection should not happen for .png, .jpeg, .css, etc. in the filter. 在过滤器中不应该对.png,.jpeg,.css等进行重定向。 check this with request.getRequestURI() request.getRequestURI()检查这个

Could it be that your filter is also applied to image requests and redirects the request for logo.png to login.jsp ? 可能是您的过滤器也应用于图像请求并将logo.png的请求重定向到login.jsp

If so, you could adjust the filter-mapping in web.xml . 如果是这样,您可以在web.xml调整filter-mapping

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM