Pages restricted by login filter are still accessible by other users

杀马特。学长 韩版系。学妹 提交于 2019-12-12 04:13:04

问题


I am using Filters in my login application. I want some pages only accessed by admin. I have kept those pages in admin folder and implemented filters in my project. But pages are still accessible through URL by other users.

Where I am going wrong?

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 javax.servlet.http.HttpSession;


public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        // If you have any <init-param> in web.xml, then you could get them
        // here by config.getInitParameter("name") and assign it as field.
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        HttpSession session = request.getSession(false);

        if (session == null || session.getAttribute("user") == null) {
            response.sendRedirect(request.getContextPath() + "/Login.xhtml"); // No logged-in user found, so redirect to login page.
        } else {
            chain.doFilter(req, res); // Logged-in user found, so just continue request.
        }
    }

    @Override
    public void destroy() {
        // If you have assigned any expensive resources as field of
        // this Filter class, then you could clean/close them here.
    }

}

回答1:


Theoretically there are 2 possible reasons for this:

  • The filter doesn't run at all
  • The filter doesn't protect the pages of the application.

I know it sounds trivially but could you specify whether the filter runs at all, and if yes, do you come to chain.doFilter(req,res) ?



来源:https://stackoverflow.com/questions/12541311/pages-restricted-by-login-filter-are-still-accessible-by-other-users

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!