2014-04-17 49 views
1

我正在开发Spring MVC控制器项目。我在7-8个jsp页面有不同的JSP页面。如何通过在Spring MVC中拦截JSP页面上的请求来显示不同的错误jsp页面?

人们将从不同的IP地址(这意味着来自不同的机器)调用这些不同的JSP页面。

现在我想要做的是 - 我将拦截呼叫并从中提取IP地址,如果IP地址在我们的有效列表中,那么只有我将显示他们正在请求的jsp页面,否则我会显示错误的jsp页面。

现在还有另一个棘手的情况。我不想在上面检查所有的JSP页面,我只想在两个JSP页面上执行此操作。这意味着如果人们要求这两个JSP页面,那么只有我会执行上述检查,否则我不想执行上述IP地址检查。

下面是我的两个jsp代码,其中我需要上面的IP地址检查。这意味着如果人们正在从不在我们列表中的IP地址的jsp页面下面请求,我想显示一个错误的jsp页面。

下面是我testWorkflow.jsp -

@RequestMapping(value = "testWorkflow", method = RequestMethod.GET) 
public @ResponseBody 
ZookeeperResponse testWorkflow(@RequestParam("workflow") final String workflow, 
    @RequestParam("conf") final String value, @RequestParam("dc") final String dc) { 

    // if ipAddress is not in the valid list 
    // then show error jsp page. 

    // some code here related to my JSP page 


    } 

下面是我testOperation.jsp -

@RequestMapping(value = "testOperation", method = RequestMethod.GET) 
public Map<String, String> testOperation() { 

    // if ipAddress is not in the valid list 
    // then show different error jsp page. 

    // some code here related to my JSP page 

    } 

而下面是我可以用它来提取头中的IP地址码 -

//is client behind something? 
String ipAddress = request.getHeader("X-FORWARDED-FOR"); 
if (ipAddress == null) { 
    ipAddress = request.getRemoteAddr(); 
}  

现在我不知道我该怎么做才能限制无效的IP地址在进行调用并显示错误的jsp页面(如果它们无效)。我需要为上述两个jsp页面调用显示不同的错误jsp页面。

这可能吗?什么是实现这一目标的最佳方式?我刚开始使用Spring MVC控制器工作,所以不确定我有什么选项,什么是我的问题的最佳方式?

任何建议和示例都会有很大的帮助。

注: -

我已经在代码本身有效的IP地址列表。所以我不需要在某个文件或xml文件中定义这些有效的IP地址。

回答

1

有一些事情很简单,可以解决您的问题。

你可以使用拦截器,它不需要任何外部模块。

代码:

public class LoginInterceptor extends HandlerInterceptorAdapter { 

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 
     String requestUri = request.getRequestURI(); 
     //Only check on several pages not all 
     //Check if user is requesting the two JSP pages 
     if (requestUri.startsWith(...)) { 
     //Yes, user is requesting the two JSP 
      if(/*Check if ip address is on the list*/) { 
       //Yes, it is. 
       if (/*check sth else maybe?*/) { 
         //test passes 
         return true; 
       } else { 
         //test fails. 
         response.sendRedirect(request.getContextPath() + "error_1"; 
         return false; 
       } 
      } else { 
       //oops, not on the list, redirect to error url 
       response.sendRedirect(request.getContextPath() + "error_2"; 
       return false; 
      } 

     } else { 
     //no need to check, just grant access 
     return true; 
     } 
    } 
} 

配置也很简单:

<mvc:interceptors> 
    <bean id="loginInterceptor" class="....LoginInterceptor" /> 
</mvc:interceptors> 

现在,假设用户的IP地址不在名单上,他或她将被重定向到错误页的URL(比如abc.com/error_denied )。在这里,我假设你没有映射到该URL的控制器,那么<mvc:view-controller path="/error_denied" view-name="error.jsp"/>将完成这项工作:)

+0

这就是我正在寻找的。我不知道如何阅读是否调用哪个jsp页面。所以你说,我可以从'getRequestURI'方法中获取?酷..还有我将如何显示'error1.jsp'页面为'testWorkflow.jsp'页面和'error2.jsp'的无效请求来为'testWorkflow.jsp'页面的无效请求进来..这是可能的无论如何呢?我已经知道有效的IP地址,所以如果检查但我不知道如何针对不同的调用显示不同的错误JSP页面,我可以做。 – john

+0

另外,我们可以代替'requestUri'检查在拦截器中添加一些排除列表吗?只是问问。 – john

+0

说,我有网址“'http:// localhost:8080/hello'”'getRequestURI'会给/ hello和'getRequestURL'会给出整个URL,我通常更喜欢'getRequestURI'。因此,您可以截取该通话并进行窥视,如果该通话要发送到您要检查的网址,请检查代码,否则只需返回true,无需进行检查。如果用户测试失败,返回false将拒绝访问(重定向到403)。 – Bowen