一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - SpringMVC攔截器實現登錄認證

SpringMVC攔截器實現登錄認證

2020-07-06 14:24u014427391 JAVA教程

這篇文章主要介紹了SpringMVC攔截器實現登錄認證的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

博客以Demo的形式講訴攔截器的使用

項目結構如圖:

SpringMVC攔截器實現登錄認證

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

SpringMVC攔截器實現登錄認證

SpringMVC包的作用說明:

aopalliance.jar:這個包是AOP聯盟的API包,里面包含了針對面向切面的接口。通常spring等其它具備動態織入功能的框架依賴這個jar

spring-core.jar:這個jar 文件包含Spring 框架基本的核心工具類。Spring 其它組件要都要使用到這個包里的類,是其它組件的基本核心,當然你也可以在自己的應用系統中使用這些工具類。
外部依賴Commons Logging, (Log4J)。

spring-beans.jar:這個jar 文件是所有應用都要用到的,它包含訪問配置文件、創建和管理bean 以及進行Inversion of Control /
Dependency Injection(IoC/DI)操作相關的所有類。如果應用只需基本的IoC/DI 支持,引入spring-core.jar 及spring-beans.jar 文件
就可以了。

spring-aop.jar:這個jar 文件包含在應用中使用Spring 的AOP 特性時所需的類和源碼級元數據支持。使用基于AOP 的Spring特性,如聲明型事務管理(Declarative Transaction Management),也要在應用里包含這個jar包。

外部依賴spring-core, (spring-beans,AOP Alliance, CGLIB,Commons Attributes)。

spring-context.jar:這個jar 文件為Spring 核心提供了大量擴展。可以找到使用Spring ApplicationContext特性時所需的全部類,JDNI
所需的全部類,instrumentation組件以及校驗Validation 方面的相關類。
外部依賴spring-beans, (spring-aop)。
spring-context-support:Spring-context的擴展支持,用于MVC方面

spring-web.jar:這個jar 文件包含Web 應用開發時,用到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 數據訪問進行封裝的所有類。
外部依賴spring-beans,spring-dao。

spring-test.jar:對Junit等測試框架的簡單封裝

spring-tx.jar:Spring的tx事務處理的jar

spring-expression.jar:Spring表達式語言

編寫控制器:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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";
 }
  
 /**
  * 退出系統
  * @param session
  *   Session
  * @return
  * @throws Exception
  */
 @RequestMapping(value="/logout")
 public String logout(HttpSession session) throws Exception{
  //清除Session
  session.invalidate();
   
  return "redirect:hello.action";
 }
  
  
  
}

編寫攔截器:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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執行完成之后調用這個方法
  */
 public void afterCompletion(HttpServletRequest request,
   HttpServletResponse response, Object handler, Exception exc)
   throws Exception {
   
 }
 
 /**
  * Handler執行之后,ModelAndView返回之前調用這個方法
  */
 public void postHandle(HttpServletRequest request, HttpServletResponse response,
   Object handler, ModelAndView modelAndView) throws Exception {
 }
 
 /**
  * Handler執行之前調用這個方法
  */
 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;
  }
  //不符合條件的,跳轉到登錄界面
  request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
   
  return false;
 }
 
}

SpringMVC的配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?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>
   
  <!-- 在實際開發中通常都需配置 mvc:annotation-driven標簽,這個標簽是開啟注解 -->
  <mvc:annotation-driven></mvc:annotation-driven>
  <!-- 攔截器 -->
  <mvc:interceptors>
   <!-- 多個攔截器,順序執行 -->
   <mvc:interceptor>
    <mvc:mapping path="/**"/>
    <bean class="com.mvc.interceptor.LoginInterceptor"></bean>
   </mvc:interceptor>
  </mvc:interceptors>
  
</beans>

登錄界面:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<%@ 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>

登錄成功后,跳轉的界面
hello.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<%@ 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形式的,自己要根據項目去改哦

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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(), 將數據傳到頁面展示
//  //model數據
//  modelAndView.addObject("message", message);
//  //設置視圖
//  modelAndView.setViewName("hello");
// 
//  return modelAndView;
// }
  
  
 
  
}

SpringMVC攔截器實現登錄認證

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜勾魂曲| 亚洲日韩精品欧美一区二区一 | a∨79成人网 | 国产精品久久久久久久福利院 | 国产自拍偷拍自拍 | 黄 色 成 年人在线 幻女free性俄罗斯第一次摘花 | 视频一本大道香蕉久在线播放 | 天堂a视频| 处女呦呦 | 国产日韩成人 | 亚洲一区二区精品推荐 | 欧美日本一区视频免费 | 国产精品久久久久久岛国 | 日本大尺度激情做爰叫床 | 青柠网在线观看视频 | 亚洲人成网站在线观看90影院 | 精品一区二区三区免费毛片 | 视频免费观看在线播放高清 | 99视频在线免费观看 | 大ji吧快给我别停受不了视频 | 亚洲欧美日韩天堂在线观看 | 日韩欧美色 | www.av网站| 欧美精品国产一区二区三区 | 成人精品视频 成人影院 | 寡妇一级毛片 | 国产剧情一区 | 欧美日韩中文字幕久久伊人 | 九9热这里只有真品 | 久久中文骚妇内射 | 欧洲一级黑寡妇 | tkvk视频| 日本大片免aaa费观看视频 | 国产免费成人在线视频 | 国产一区二区三区免费在线视频 | 四虎影院新网址 | 动漫美丽妇人1~2在线看 | 欧美日韩高清观看一区二区 | 日本高清va不卡视频在线观看 | 办公室的秘密在线观看 | 日韩欧美一卡二区 |