如何通过在浏览器中键入URL来限制用户访问页面?

时间:2017-07-10 09:04:50

标签: java jsp servlets

在我的应用程序中,我使用ajax调用java项目我在没有登录时遇到问题用户也可以键入url访问页面,因为我使用了下面的代码,但是当我添加下面的代码时,它无法正常工作。即使我无法获得登录页面,我也会收到Page not found错误。

     @WebFilter("/*")
public class LoginFilters 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.
        }
     private static final String AJAX_REDIRECT_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                + "<partial-response><redirect url=\"%s\"></redirect></partial-response>";
 @Override
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {    
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;
            HttpSession session = request.getSession(false);
            String loginURL = request.getContextPath() + "/Login.jsp";

            boolean loggedIn = (session != null) && (session.getAttribute("Username") != null);
            boolean loginRequest = request.getRequestURI().equals(loginURL);
            boolean resourceRequest = request.getRequestURI().startsWith(request.getContextPath() + "/Login.jsp");
            boolean ajaxRequest = "partial/ajax".equals(request.getHeader("Faces-Request"));

            if (loggedIn || loginRequest || resourceRequest) {
                if (!resourceRequest) { // Prevent browser from caching restricted resources. See also https://stackoverflow.com/q/4194207/157882
                    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
                    response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
                    response.setDateHeader("Expires", 0); // Proxies.
                }

                chain.doFilter(request, response); // So, just continue request.
            }
            else if (ajaxRequest) {
                response.setContentType("text/xml");
                response.setCharacterEncoding("UTF-8");
                response.getWriter().printf(AJAX_REDIRECT_XML, loginURL); // So, return special XML response instructing JSF ajax to send a redirect.
            }
            else {
                response.sendRedirect(loginURL); // So, just perform standard synchronous redirect.
            }
        }

        @Override
        public void destroy() {
            // TODO Auto-generated method stub

        }

        // ...

}

谁能告诉我怎么能这样做

2 个答案:

答案 0 :(得分:0)

你应该看看这个:Securing a Web Application

  

保护Web应用程序

     

本指南将指导您完成创建简单网页的过程   使用受Spring Security保护的资源的应用程序。

     

您将构建什么

     

您将构建一个Spring MVC应用程序,用于保护页面   由固定用户列表支持的登录表单。

答案 1 :(得分:0)

Spring绝对是最好的解决方案,我真的建议使用它:它可以帮助你解决所有问题!如果您现在不想使用它并且您不太关心安全性,那么您可以粗略地使用会话令牌或简单的静态令牌(甚至是布尔值,字符或字符串)来检查如果用户来自某个页面:

如果执行某个servlet(或spring控制器)中的代码,则应将此boolean-whateverYouWant字段设置为某个值:加载页面时,可以检查该字段的值(spring mvc-angularJs或javascript)然后你可以显示正确的页面:&#34;不允许&#34;如果令牌为空或无效或您喜欢什么! 最好的和绝对的解决方案是在spring mvc控制器中暴露的spring security-angularJs和web服务。说真的......想想学习春天!