如何处理/限制用户访问servlet和jsp?

MrT*_*thy 11 java jsp servlets user-management java-ee

我目前正在用Java编写一个小的动态Web应用程序.该应用程序应该是一个事件平台,您可以在其中创建用户帐户,登录,然后您可以看到所有打开的事件(在以后的迭代中,用户可以创建/参与这些事件).

现在,web-app的结构可以(简化)描述如下:

Register-Servlet -> Register.jsp
        |
        V
Login-Servlet -> Login.jsp
        |
        V
Main-page-Servlet -> Main.jsp
Run Code Online (Sandbox Code Playgroud)

所以现在,用户可以访问Login.jsp,他的登录信息将被发送到Login-Servlet,后者将对其进行验证,然后将其发送到Main-Page-Servlet.然后,Main-Page-Servlet(在再次验证登录之后)从数据库获取所有当前事件,将其附加到请求,并将其转发到Main.jsp,Main.jsp显示它供用户查看.

现在,如果用户想直接访问Main.jsp(没有来自Main-Page-Servlet),它显然无法显示可用事件.我正在使用的解决方法是进行空检查以查看事件是否存在,如果没有,则重定向到Main-Page-Servlet.

困扰我解决我的问题,因为我不认为这是最好的做法,我认为它会产生很多其他问题,我的应用程序越大.

我首先想到的是,如果我可以简单地"隐藏"用户的所有.jsp,那么用户将只能登陆servlet并且无法以不同的方式访问.jsp.

有没有办法做到这一点?或者,如果不是,如果我要编写专业的企业级应用程序,最佳实践解决方案是什么?

Lui*_*oza 11

这可以在一个Filter并且在StackOverflow Servlet-Filter wiki中有很好的解释和示例.

根据您的问题调整代码(请注意方法的添加和使用needsAuthentication):

@WebFilter("/*")
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);

        String requestPath = httpServletRequest.getRequestURI();

        if (needsAuthentication(requestPath) ||
            session == null ||
            session.getAttribute("user") == null) { // change "user" for the session attribute you have defined

            response.sendRedirect(request.getContextPath() + "/login"); // 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.
    }

    //basic validation of pages that do not require authentication
    private boolean needsAuthentication(String url) {
        String[] validNonAuthenticationUrls =
            { "Login.jsp", "Register.jsp" };
        for(String validUrl : validNonAuthenticationUrls) {
            if (url.endsWith(validUrl)) {
                return false;
            }
        }
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

我建议将所有需要身份验证的页面移动到文件夹中app,然后将Web过滤器更改为

@WebFilter("/app/*")
Run Code Online (Sandbox Code Playgroud)

这样,您可以从过滤器中删除needsAuthentication方法.