我使用 JSF、Hibernate、Spring 创建了一个 Web 应用程序。我添加了一个过滤器来检查会话。我的过滤器代码是:
public class AdminFilter implements Filter{
private ArrayList<String> urlList;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
String urls = filterConfig.getInitParameter("avoid-urls");
StringTokenizer token = new StringTokenizer(urls, ",");
urlList = new ArrayList<String>();
while (token.hasMoreTokens()) {
urlList.add(token.nextToken());
}
}
// Checking if user is logged in
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req= (HttpServletRequest) request;
HttpServletResponse resp= (HttpServletResponse) response;
String url = req.getServletPath();
HttpSession session = req.getSession();
if(!urlList.contains(url) && session.getAttribute("user")==null)
{
resp.sendRedirect(req.getContextPath() + "/backend/login/index.xhtml");
}
chain.doFilter(req, resp);
}
@Override
public void destroy() {
// throw new UnsupportedOperationException("Not supported yet.");
}
}
在过滤器的 init 方法中,我有一些避免 URL 的会话检查应该被跳过,比如登录页面本身。这工作正常,但此过滤器限制我的 CSS、图像和 JS 在登录页面上加载。建议我过滤器有什么问题?