SpringMVC拦截器:解决项目中接口(url)访问权限的问题(通过url不能随意访问controller层)

层次关系

SpringMVC拦截器:解决项目中接口(url)访问权限的问题(通过url不能随意访问controller层)_第1张图片
拦截器实现HandlerInterceptor接口

package com.bybo.aca.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class Login implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest httpRequest,
            HttpServletResponse httpResponse, Object arg2, Exception arg3)
            throws Exception {
         
    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception {
        

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
            Object object) throws Exception {
        /*HttpServletRequest httpRequest = (HttpServletRequest) request;  
         HttpServletResponse httpResponse = (HttpServletResponse) response;*/
        String urlString = request.getRequestURI();
            
        ///olForum/forumList.html模拟登录页
        if(urlString.endsWith("forumList.html")){
            return true;
        }
        //请求的路径
        String contextPath=request.getContextPath();
       
        /*httpRequest.getRequestDispatcher("/olForum/forumList").forward(httpRequest, httpResponse);*/
        /*response.sendRedirect(contextPath+"/olForum/forumList.html");*/
        response.sendRedirect(contextPath + "/olForum/forumList.html?login=aaa");  
        return false;
        /*httpResponse.sendRedirect(httpRequest.getContextPath()+"/olForum/forumList.html");
        return;*/
        

    }

}

spring.xml

<mvc:interceptors>  
   
       
    <mvc:interceptor>  
        
        <mvc:mapping path="/**" />
       
        <mvc:exclude-mapping path="/index.html"/>
     
        <bean class="com.bybo.aca.web.interceptor.Login"/>  
    mvc:interceptor>  
mvc:interceptors>   

你可能感兴趣的:(SpringMVC拦截器:解决项目中接口(url)访问权限的问题(通过url不能随意访问controller层))