springmvc-210807-04---拦截器实现简单登录

springmvc-210807-04—拦截器实现简单登录


MyInterceptor01.java(拦截器)

package com.bgy.handler;

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

// 拦截器,拦截用户请求
public class MyInterceptor01 implements HandlerInterceptor {

    /**
     * 验证登录的用户信息,正确返回true,其他返回false。
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("111111这是MyInterceptor01的preHandler()方法111111");

        String loginName = "";
        // 从session中获取name值
        Object attr = request.getSession().getAttribute("name");
        if (attr != null) {
            loginName = (String) attr;
        }

        // 判断用户登录账号,是否符合要求
        if (!"bgy".equals(loginName)){
            request.getRequestDispatcher("/tips.jsp").forward(request,response);
            return false;
        }

        return true;
    }

}

MyController.java(中央处理器)

package com.bgy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {

    /**
     * 测试拦截器
     */
    @RequestMapping("/some.do")
    public ModelAndView doSome(String name,Integer age) {

        System.out.println("这是MyController的doSome()方法");

        ModelAndView mv = new ModelAndView();

        mv.addObject("uname",name);
        mv.addObject("uage",age);
        mv.addObject("msg","拦截器");

        mv.setViewName("show");

        return mv;
    }
}

dispatchServlet.xml(springmvc配置文件)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- springmvc配置文件,声明controller和其他web相关的对象 -->

    <context:component-scan base-package="com.bgy.controller"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 声明拦截器,拦截器可以有0个或多个 -->
    <mvc:interceptors>
        <!-- 第一个拦截器 -->
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.bgy.handler.MyInterceptor01"/>
        </mvc:interceptor>
    </mvc:interceptors>

</beans>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String basePath = request.getScheme() + "://" +
            request.getServerName() + ":" +
            request.getServerPort() +
            request.getContextPath() + "/";
%>
<html>
<head>
    <title>拦截器</title>
    <base href="<%=basePath%>"/>
</head>
<body>
    <form action="some.do" method="post">
        <span>拦截器</span>
        <br/>
        <input type="submit" value="用户登录"/>
    </form>
</body>
</html>

login.jsp(登录页面,先运行这个jsp页面,让session有缓存)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录系统</title>
</head>
<body>
    <span>登录系统</span>
    <%
        session.setAttribute("name","bgy");
    %>
</body>
</html>

logout.jsp(退出页面,清除session缓存)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>退出系统</title>
</head>
<body>
    <span>退出系统</span>
    <%
        session.removeAttribute("name");
    %>
</body>
</html>

show.jsp(登录成功页面)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>show.jsp</title>
</head>
<body>
    WEB-INF/view/show.jsp
    <br/>
    ${msg}
    <br/>
    姓名:白光一
    <br/>
    年龄:22
</body>
</html>

tips.jsp(登录失败页面)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>tips.jsp</title>
</head>
<body>
    WEB-INF/view/tips.jsp
    <br/>
    非bgy用户不得使用
</body>
</html>

我的工程目录


运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值