SpringMVC攔截器實現(xiàn)登錄認證
博客以Demo的形式講訴攔截器的使用
項目結(jié)構(gòu)如圖:

需要的jar:有springMVC配置需要的jar和jstl需要的jar

SpringMVC包的作用說明:
aopalliance.jar:這個包是AOP聯(lián)盟的API包,里面包含了針對面向切面的接口。通常spring等其它具備動態(tài)織入功能的框架依賴這個jar
spring-core.jar:這個jar 文件包含Spring 框架基本的核心工具類。Spring 其它組件要都要使用到這個包里的類,是其它組件的基本核心,當然你也可以在自己的應用系統(tǒng)中使用這些工具類。
外部依賴Commons Logging, (Log4J)。
spring-beans.jar:這個jar 文件是所有應用都要用到的,它包含訪問配置文件、創(chuàng)建和管理bean 以及進行Inversion of Control /
Dependency Injection(IoC/DI)操作相關的所有類。如果應用只需基本的IoC/DI 支持,引入spring-core.jar 及spring-beans.jar 文件
就可以了。
spring-aop.jar:這個jar 文件包含在應用中使用Spring 的AOP 特性時所需的類和源碼級元數(shù)據(jù)支持。使用基于AOP 的Spring特性,如聲明型事務管理(Declarative Transaction Management),也要在應用里包含這個jar包。
外部依賴spring-core, (spring-beans,AOP Alliance, CGLIB,Commons Attributes)。
spring-context.jar:這個jar 文件為Spring 核心提供了大量擴展??梢哉业绞褂肧pring ApplicationContext特性時所需的全部類,JDNI
所需的全部類,instrumentation組件以及校驗Validation 方面的相關類。
外部依賴spring-beans, (spring-aop)。
spring-context-support:Spring-context的擴展支持,用于MVC方面
spring-web.jar:這個jar 文件包含Web 應用開發(fā)時,用到Spring 框架時所需的核心類,包括自動載入Web Application Context 特性的類、Struts 與JSF集成類、文件上傳的支持類、Filter 類和大量工具輔助類。
外部依賴spring-context, Servlet API, (JSP API, JSTL, Commons FileUpload, COS)。
spring-webmvc.jar:這個jar 文件包含Spring MVC 框架相關的所有類。包括框架的Servlets,Web MVC框架,控制器和視圖支持。當然,如果你的應用使用了獨立的MVC 框架,則無需這個JAR 文件里的任何類。
外部依賴spring-web, (spring-support,Tiles,iText,POI)。
spring-aspects.jar:提供對AspectJ的支持,以便可以方便的將面向方面的功能集成進IDE中,比如Eclipse AJDT。
外部依賴。
spring-jdbc.jar:這個jar 文件包含對Spring 對JDBC 數(shù)據(jù)訪問進行封裝的所有類。
外部依賴spring-beans,spring-dao。
spring-test.jar:對Junit等測試框架的簡單封裝
spring-tx.jar:Spring的tx事務處理的jar
spring-expression.jar:Spring表達式語言
編寫控制器:
package com.mvc.action;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 登錄認證的控制器
*/
@Controller
public class LoginControl {
/**
* 登錄
* @param session
* HttpSession
* @param username
* 用戶名
* @param password
* 密碼
* @return
*/
@RequestMapping(value="/login")
public String login(HttpSession session,String username,String password) throws Exception{
//在Session里保存信息
session.setAttribute("username", username);
//重定向
return "redirect:hello.action";
}
/**
* 退出系統(tǒng)
* @param session
* Session
* @return
* @throws Exception
*/
@RequestMapping(value="/logout")
public String logout(HttpSession session) throws Exception{
//清除Session
session.invalidate();
return "redirect:hello.action";
}
}
編寫攔截器:
package com.mvc.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{
/**
* Handler執(zhí)行完成之后調(diào)用這個方法
*/
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception exc)
throws Exception {
}
/**
* Handler執(zhí)行之后,ModelAndView返回之前調(diào)用這個方法
*/
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView modelAndView) throws Exception {
}
/**
* Handler執(zhí)行之前調(diào)用這個方法
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
//獲取請求的URL
String url = request.getRequestURI();
//URL:login.jsp是公開的;這個demo是除了login.jsp是可以公開訪問的,其它的URL都進行攔截控制
if(url.indexOf("login.action")>=0){
return true;
}
//獲取Session
HttpSession session = request.getSession();
String username = (String)session.getAttribute("username");
if(username != null){
return true;
}
//不符合條件的,跳轉(zhuǎn)到登錄界面
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
return false;
}
}
SpringMVC的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 使用組件掃描 -->
<!-- 將action掃描出來,在spring容器中進行注冊,自動對action在spring容器中進行配置 -->
<context:component-scan base-package="com.mvc.action" />
<!-- 項目的Handler
<bean name="/hello.action" class="com.mvc.action.HelloAction"></bean>
-->
<!-- 處理器映射器HandlerMapping -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 處理器設配器HandlerAdapter -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</list>
</property>
</bean>
<!-- 視圖解析器ViewResolver -->
<!-- 解析jsp,默認支持jstl -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 在實際開發(fā)中通常都需配置 mvc:annotation-driven標簽,這個標簽是開啟注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 攔截器 -->
<mvc:interceptors>
<!-- 多個攔截器,順序執(zhí)行 -->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.mvc.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
登錄界面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${pageContext.request.contextPath}/login.action" method="post">
用戶名:<input type="text" name="username" /><br>
密碼:<input type="text" name="password" /><br>
<input type="submit" value="登錄" />
</form>
</body>
</html>
登錄成功后,跳轉(zhuǎn)的界面
hello.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'hello.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
當前用戶:${username}
<c:if test="${username!=null}">
<a href="${pageContext.request.contextPath }/logout.action">退出</a>
</c:if>
${message}
</body>
</html>
HelloControl.java,我寫成HelloWorld形式的,自己要根據(jù)項目去改哦
package com.mvc.action;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
//標記這個類是一個Handler處理器
@Controller
public class HelloAction{
@RequestMapping("/hello")//制定這個控制類對應的url
public String hello(Model model){
String message = "SpringMVC";
//為model添加Attribute
model.addAttribute("message",message);
return "hello";
}
// public ModelAndView handleRequest(HttpServletRequest request,
// HttpServletResponse response) throws Exception {
//
// //在頁面上提示一行信息
// String message = "hello world!";
//
// //通過request對象將信息在頁面上展示
// //request.setAttribute("message", message);
//
// ModelAndView modelAndView = new ModelAndView();
// // 相當于request.setAttribute(), 將數(shù)據(jù)傳到頁面展示
// //model數(shù)據(jù)
// modelAndView.addObject("message", message);
// //設置視圖
// modelAndView.setViewName("hello");
//
// return modelAndView;
// }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 詳解springmvc控制登錄用戶session失效后跳轉(zhuǎn)登錄頁面
- Spring MVC+mybatis實現(xiàn)注冊登錄功能
- SpringMVC 實現(xiàn)用戶登錄實例代碼
- SpringMVC攔截器實現(xiàn)單點登錄
- Java編程實現(xiàn)springMVC簡單登錄實例
- springmvc+spring+mybatis實現(xiàn)用戶登錄功能(上)
- springmvc下實現(xiàn)登錄驗證碼功能示例
- Spring mvc 實現(xiàn)用戶登錄的方法(攔截器)
- spring mvc實現(xiàn)登錄賬號單瀏覽器登錄
- spring?MVC實現(xiàn)簡單登錄功能
相關文章
2020macOS Big Sur配置Java開發(fā)環(huán)境之jdk安裝過程
這篇文章主要介紹了2020macOS Big Sur配置Java開發(fā)環(huán)境之jdk安裝,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
Java技巧分享之利用RxJava打造可觀測數(shù)據(jù)RxLiveData
這篇文章主要來和大家分享一個Java技巧,那就是利用RxJava打造可觀測數(shù)據(jù)RxLiveData,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-06-06
Spring攔截器實現(xiàn)鑒權(quán)的示例代碼
本文主要介紹了Spring攔截器實現(xiàn)鑒權(quán)的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07

