ssm配置拦截器

ssm配置拦截器

1.编写一个拦截器的类

ssm配置拦截器_第1张图片

LoginHandlerInterceptor.java

package com.demo.config;

import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class LoginHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取登录成功的session
        Object loginUser = request.getSession().getAttribute("user");
        if (loginUser==null){
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out=response.getWriter();
            out.print("");
           /* request.setAttribute("msg","请登录后再进行操作");
            request.getRequestDispatcher("/login.html").forward(request,response);  //转发跳转*/
            return false;
        }else {
            return true;
        }
    }

}

2.在spring-mvc中对拦截器进行配置

 <!--拦截器配置-->
    <mvc:interceptors>
        <!--后台访问拦截器-->
        <mvc:interceptor>
            <!--拦截所有资源-->
            <mvc:mapping path="/**"/>
            <!--对静态资源放行-->
            <mvc:exclude-mapping path="/css/**"/>
            <mvc:exclude-mapping path="/fonts/**"/>
            <mvc:exclude-mapping path="/js/**"/>
            <mvc:exclude-mapping path="/img/**"/>
            <mvc:exclude-mapping path="/layer/**"/>
            <mvc:exclude-mapping path="/lib/**"/>
            <mvc:exclude-mapping path="/plugin/**"/>
            <mvc:exclude-mapping path="/temp/**"/>
            <mvc:exclude-mapping path="/index"/>
            <mvc:exclude-mapping path="/about"/>
            <mvc:exclude-mapping path="/article"/>
            <mvc:exclude-mapping path="/article_detail"/>
            <mvc:exclude-mapping path="/board"/>
            <mvc:exclude-mapping path="/login"/>
            <mvc:exclude-mapping path="/register"/>
            <mvc:exclude-mapping path="/randoms"/>
            <mvc:exclude-mapping path="/adminMessage/**"/>
            <mvc:exclude-mapping path="/commentList/**"/>
            <mvc:exclude-mapping path="/indexArticle/**"/>
            <mvc:exclude-mapping path="/update_user/**"/>
            <mvc:exclude-mapping path="/user_login/**"/>

            <!--对index的页面进行放行-->
            <mvc:exclude-mapping path="/mood.html"/>
            <mvc:exclude-mapping path="/article.html"/>
            <mvc:exclude-mapping path="/article_detail.html"/>
            <mvc:exclude-mapping path="/board.html"/>
            <mvc:exclude-mapping path="/login.html"/>
            <mvc:exclude-mapping path="/register.html"/>
            <mvc:exclude-mapping path="/ops-index.html"/>
            <mvc:exclude-mapping path="/about.html"/>
            <mvc:exclude-mapping path="/index.html"/>

            <!--实现拦截功能的类-->
            <bean class="com.demo.config.LoginHandlerInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

你可能感兴趣的:(java,ajax,spring)