欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springmvc的登录拦截器配置

程序员文章站 2022-07-09 18:19:16
...

登录界面
springmvc的登录拦截器配置
login.jsp

<%--
  Created by IntelliJ IDEA.
  User: zxh
  Date: 2020/1/8
  Time: 14:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录页面</title>
</head>
<body>
<form id="loginForm" action="${pageContext.request.contextPath}/login.do" method="post">
    <table>
        <tr>
            <td>用户名</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="password" name="password"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="登录"></td>
        </tr>
    </table>
</form>
</body>
</html>

登录成功后页面
main.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>主页面</title>
</head>
<body>
...........主页面
</body>
</html>

登录控制器
springmvc的登录拦截器配置
LoginController.java

@Controller
public class LoginController {

    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session){

        if ("zxh".equals(username) && "123".equals(password)){
            //登录成功
            //1.保存session
            session.setAttribute("username",username);
            //2.进入到主页面
            return "main";
        }
        return "login";
    }
}

登录拦截器
springmvc的登录拦截器配置
LoginInterceptor.java

package com.zxh.backoffice.interceptor;

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

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

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //拦截
        //1.排除不需要拦截路径
        System.out.println("客户端访问资源的路径:"+request.getRequestURL());
        if(request.getRequestURI().endsWith("login.do")){
            return true; //放行
        }
        //2.如果已经登录,也要放行
        if (request.getSession().getAttribute("user")!=null){
            return true;
        }
        //3.进入到登录页面
        request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request,response);
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("未返回视图前   后处理");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("返回视图前   后处理");
    }
}

在springmvc.xml中进行登录拦截器的配置,加上如下配置代码即可

  
    <!--配置登录拦截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>   <!--拦截所有映射路径-->
            <bean class="com.zxh.backoffice.interceptor.LoginInterceptor"/>
            <!--<mvc:exclude-mapping path=""/>-->  <!--排除哪些路径-->
        </mvc:interceptor>
    </mvc:interceptors>
相关标签: spring mvc