SSM-攔截器 demo

摘要:整理一個簡單的攔截器模板,放上源碼

攔截器邏輯
1.攔截想跳過登陸的所有請求
2,不攔截要登陸的請求
3.登陸失敗後會跳到自己所在的頁面

實現效果

1網站首頁

在這裏插入圖片描述


2登陸界面

在這裏插入圖片描述


3登陸成功

在這裏插入圖片描述


4登陸失敗.

會彈出登陸失敗的信息,然後重新刷新該頁面

在這裏插入圖片描述


2相關的文件有:

com.how2java.interceptor.LoginInterceptor

package com.how2java.interceptor; //當前文件所在目錄文件目錄

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

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

public class LoginInterceptor implements HandlerInterceptor{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		
		HttpSession httpSession = request.getSession();
		String url = request.getRequestURI();
		//除了login.jsp是可以公開訪問的,其餘的URL都進行攔截

        Object object = httpSession.getAttribute("userName");//獲取session中userName
        if(object != null){ //判斷userName是否爲非空,如果爲非空,則表示已登錄,可繼續操作 即將這個請求傳給相應的控制器

            return true;
        }else{ //如果爲空,則表示未登錄,返回登錄界面 進行登陸
			request.setAttribute("msg", "你還有沒有登錄,請先登錄!");
			//轉發至登錄頁面
            request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
        }

        return false;

	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

}

com.how2java.controller.LoginController

package com.how2java.controller;

import javax.servlet.http.HttpSession;
import javax.swing.*;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.awt.*;

@Controller
public class LoginController {
	
	@RequestMapping("login.action")
	public String login(String userName,String userPass,HttpSession session) {
		System.out.println(userName);
		System.out.println(userPass);
		System.err.println(1);

		session.setAttribute("userName",userName);

		if(userName.equals("admin") && userPass.equals("123"))
		{
			return "index";
		}

		//如果登陸失敗就重新返回登陸頁面

		JOptionPane.showMessageDialog(null, "登陸失敗", "alert", JOptionPane.ERROR_MESSAGE);
		return "login";
	}



}

springMVC.xml


    <mvc:interceptors>
        <mvc:interceptor >
            <mvc:mapping path="/login2"/>
            <mvc:exclude-mapping path="/login"/>
            <bean class="com.how2java.interceptor.LoginInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

WEB-INF/jsp/index.jsp 登陸成功後的index

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<span>你已成功進入本系統</span>
</body>
</html>

WEB-INF/jsp/login.jsp

<%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2019/4/21
  Time: 13:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <a href="http://localhost:8080/listCategory"> 測試路徑 </a><br>
    <a href="http://localhost:8080/index"> 測試攔截器 </a>
</head>
<body>

</body>
</html>

index.jsp 網站的index

<%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2019/4/21
  Time: 13:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <a href="http://localhost:8080/listCategory"> 測試路徑 </a><br>
    <a href="http://localhost:8080/index"> 測試攔截器 </a>
</head>
<body>

</body>
</html>

源碼

https://download.csdn.net/download/qq_26769591/11146992

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章