重定向过滤器不加载样式 [英] Filter on redirect does not load styling
问题描述
我正在尝试创建一个过滤器,以确保用户在进入任何其他页面之前均已登录
I am trying to create a filter that makes sure the user is logged in before going to any other page
这里有2个问题(我知道).
Here I have 2 issues (that I know of).
1)我只允许使用jsp文件时遇到问题.当我尝试访问我的页面时,tomcat引发错误
1) I am having issues with only allowing jsp files. When I try to access my page tomcat throws an error
java.lang.IllegalArgumentException: Invalid <url-pattern> /public/*.jsp in filter mapping
但是当我的网址映射是/public/*时,它可以按预期工作
but when my url mapping is /public/* it sorta works as intended
事实证明,由于以下注释,我使用的映射不正确,对于任何访问此页面的人,这是解决方案的一部分:
EDIT 1: Turns out I am using an incorrect mapping, thanks to some comments below, for anyone coming to this page here is that part of the solution: http://www.roguewave.com/portals/0/products/hydraexpress/docs/3.5.0/html/rwsfservletug/4-3.html
2)当我确实使用/public/*
进行重定向时,我能够进入我的登录页面,但是缺少所有样式
2) When I did get a redirect with /public/*
I was able to get to my login page, but all of the styling was missing
这是我在web.xml中的过滤器
Here is my filter in web.xml
EDIT2 :下面的代码段现在反映了我对答案所做的更改
The code snippets below now reflect changes I have made regarding answers
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>authentication.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>authentication.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
这就是我要在过滤器中尝试的内容
This is what I am attempting in my filter
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession session = req.getSession(false);
// Get the requested address
String from = URLEncoder.encode(req.getRequestURI(), "UTF-8");
if(req.getQueryString() != null){
from += "?" + req.getQueryString();
}
System.err.println("from str: " + from);
System.out.println("Serv path: " + req.getServletPath());
if(!req.getServletPath().startsWith("/public/login")){
if(session == null || session.getAttribute("username") == null){
res.sendRedirect(req.getContextPath() + "/public/login.jsp?from="+from);
}else{
System.out.println("Username: " + session.getAttribute("username"));
// pass the request along the filter chain
chain.doFilter(request, response);
}
}else{
chain.doFilter(request, response);
}
}
对我的过滤器有什么建议吗?
Any suggestions for improvements to my filter?
推荐答案
URL模式不是真正的全局匹配,仅支持两种通配符:/someting/*
和*.something
URL pattern is not a true glob match and only supports two types of wildcards: /someting/*
and *.something
http: //www.roguewave.com/portals/0/products/hydraexpress/docs/3.5.0/html/rwsfservletug/4-3.html
在样式方面,您必须允许登录页面使用的资源(CSS,Javascript,图像等)无需会话即可加载,就像允许访问登录页面本身一样.
When it comes to styling you have to allow the resources (CSS, Javascript, images etc) used by the login page to be loaded without a session the same way you allow access to the login page itself.
这篇关于重定向过滤器不加载样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!