• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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("<script language='javascript'>alert('请登录后再进行操作');window.location.href='https://blog.csdn.net/wwy11123/article/details/login.html';</script>");
           /* 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>
学新通

这篇好文章是转载于:编程之路

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 编程之路
  • 本文地址: /boutique/detail/tanhhfechi
系列文章
更多 icon
同类精品
更多 icon
继续加载