最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringMVC攔截器實現(xiàn)登錄認證

 更新時間:2016年11月18日 10:53:44   作者:u014427391  
這篇文章主要介紹了SpringMVC攔截器實現(xiàn)登錄認證的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

項目結構如圖:


需要的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 核心提供了大量擴展。可以找到使用Spring 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í)行完成之后調用這個方法 
  */ 
 public void afterCompletion(HttpServletRequest request, 
   HttpServletResponse response, Object handler, Exception exc) 
   throws Exception { 
   
 } 
 
 /** 
  * Handler執(zhí)行之后,ModelAndView返回之前調用這個方法 
  */ 
 public void postHandle(HttpServletRequest request, HttpServletResponse response, 
   Object handler, ModelAndView modelAndView) throws Exception { 
 } 
 
 /** 
  * Handler執(zhí)行之前調用這個方法 
  */ 
 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的配置文件:

<?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> 

登錄成功后,跳轉的界面
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; 
// } 
  
  
 
  
} 

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

相關文章

  • JDBC中Statement的Sql注入問題詳解

    JDBC中Statement的Sql注入問題詳解

    這篇文章主要介紹了JDBC中Statement的Sql注入問題詳解,sql注入攻擊指的是通過構建特殊的輸入作為參數(shù)傳入web應用程序,而這些輸入大都是sql語法里的一些組合,通過執(zhí)行sql語句進而執(zhí)行攻擊者所要做的操作,需要的朋友可以參考下
    2023-10-10
  • 使用IDEA搭建Hadoop開發(fā)環(huán)境的操作步驟(Window10為例)

    使用IDEA搭建Hadoop開發(fā)環(huán)境的操作步驟(Window10為例)

    經(jīng)過三次重裝,查閱無數(shù)資料后成功完成hadoop在win10上實現(xiàn)偽分布式集群,以及IDEA開發(fā)環(huán)境的搭建。一步一步跟著本文操作可以避免無數(shù)天坑
    2021-07-07
  • 23種設計模式(8) java外觀模式

    23種設計模式(8) java外觀模式

    這篇文章主要為大家詳細介紹了23種設計模式之java外觀模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • 基于Bigdecimal科學計數(shù)問題

    基于Bigdecimal科學計數(shù)問題

    這篇文章主要介紹了基于Bigdecimal科學計數(shù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Java基于Swing實現(xiàn)的打獵射擊游戲代碼

    Java基于Swing實現(xiàn)的打獵射擊游戲代碼

    這篇文章主要介紹了Java基于Swing實現(xiàn)的打獵射擊游戲代碼,包含完整的游戲事件處理與邏輯流程控制,具有不錯的參考借鑒價值,需要的朋友可以參考下
    2014-11-11
  • Java實現(xiàn)多線程下載和斷點續(xù)傳

    Java實現(xiàn)多線程下載和斷點續(xù)傳

    這篇文章主要為大家詳細介紹了Java實現(xiàn)多線程下載和斷點續(xù)傳,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • SpringBoot集成內存數(shù)據(jù)庫Sqlite的實踐

    SpringBoot集成內存數(shù)據(jù)庫Sqlite的實踐

    sqlite這樣的內存數(shù)據(jù)庫,小巧可愛,做小型服務端演示程序,非常好用,本文主要介紹了SpringBoot集成Sqlite,具有一定的參考價值,感興趣的可以了解一下
    2021-09-09
  • ChatGPT-4.0未來已來 你來不來

    ChatGPT-4.0未來已來 你來不來

    最近聽說了一個非?;鸬募夹gChatGPT4.0,今天這篇文章就給大家介紹一下ChatGPT究竟是什么東東,不得不說ChatGPT是真的強,下面就讓我們一起了解究竟什么是ChatGPT吧
    2023-03-03
  • Springboot2.X集成redis集群(Lettuce)連接的方法

    Springboot2.X集成redis集群(Lettuce)連接的方法

    這篇文章主要介紹了Springboot2.X集成redis集群(Lettuce)連接的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • 微信公眾平臺(測試接口)準備工作

    微信公眾平臺(測試接口)準備工作

    想要微信開發(fā),首先要有個服務器,但是自己沒有。這時候可以用花生殼,將內網(wǎng)映射到公網(wǎng)上,這樣就可以在公網(wǎng)訪問自己的網(wǎng)站了。
    2016-05-05

最新評論

洛扎县| 北辰区| 泽普县| 太康县| 宜兴市| 渭源县| 长乐市| 西林县| 乐安县| 东乡县| 开封县| 镇安县| 南通市| 诸暨市| 霍林郭勒市| 九龙城区| 慈利县| 兴海县| 临朐县| 安福县| 南丹县| 黔江区| 南部县| 隆德县| 荔浦县| 金堂县| 呼玛县| 吴桥县| 紫金县| 乌拉特后旗| 沛县| 克拉玛依市| 漳州市| 桃园市| 丰城市| 融水| 宁强县| 靖宇县| 淳化县| 曲阜市| 沁水县|