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

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

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringMVC攔截器——實現登錄驗證攔截器的示例代碼

SpringMVC攔截器——實現登錄驗證攔截器的示例代碼

2020-08-16 14:56UniqueColor Java教程

本篇文章主要介紹了SpringMVC攔截器——實現登錄驗證攔截器的示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

本例實現登陸時的驗證攔截,采用SpringMVC攔截器來實現

當用戶點擊到網站主頁時要進行攔截,用戶登錄了才能進入網站主頁,否則進入登陸頁面

核心代碼

首先是index.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
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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%>" rel="external nofollow" >
  
  <title>首頁</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" rel="external nofollow" >
  -->
 </head>
 
 <body>
   <div style="margin:0 auto;padding-top:100px;font-size:18px;" align="center">
     <p><a href="loginpage.html" rel="external nofollow" >登陸</a></p>
     <p><a href="user/home.html" rel="external nofollow" >用戶中心</a></p>
     <p><a href="exception.html" rel="external nofollow" >觸發異常</a></p>
   </div>
 </body>
</html>

controller類

?
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
55
56
57
58
59
60
61
62
63
package com.jikexueyuan.demo.springmvc.lesson4.controller;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
 
import com.jikexueyuan.demo.springmvc.lesson4.constant.Global;
import com.jikexueyuan.demo.springmvc.lesson4.exception.MyException;
import com.jikexueyuan.demo.springmvc.lesson4.model.User;
import com.jikexueyuan.demo.springmvc.lesson4.service.LoginService;
 
/**
 * 這個例子講解了如何定義MVC三層注解,使用@Resource進行注入,以及使用@RequestMapping、@RequestParam 、@SessionAttributes
 */
 
@Controller
public class LoginController extends BaseController {
 
  @Resource
  LoginService service;
  
  @Resource
  HttpServletRequest request;
  
  @RequestMapping("/exception")
  public void exception() throws MyException{
    throw new MyException("測試springmvc中的異常捕獲");
  }
  
  @RequestMapping("/loginpage")
  public String toLoginPage(){
    return "/WEB-INF/jsp/login.jsp";
  }
  
  @RequestMapping("/user/home")
  public String toUserHome(){
    return "/WEB-INF/jsp/userhome.jsp";
  }
  
  @RequestMapping("/logout")
  public String logout(){
    request.getSession().removeAttribute(Global.USER_SESSION_KEY);
    return "redirect:/";
  }
  
  @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
  public String doLogin(@RequestParam String userName, @RequestParam String password){
    
    try {
      User user = service.doLogin(userName, password);
      request.getSession().setAttribute(Global.USER_SESSION_KEY, user);
      return "redirect:/user/home.html";
    } catch (Exception e) {
      return "/WEB-INF/jsp/login.jsp";
    }
    
  }
  
}

當點擊用戶中心時,觸發攔截,相關配置如下

在spring-mvc.xml中加上攔截配置,攔截所有URL中包含/user/的請求,當然請求用戶中心時就會觸發這個攔截器了

?
1
2
3
4
5
6
7
<mvc:interceptors>
    <mvc:interceptor>
      <!-- 攔截所有URL中包含/user/的請求 -->
      <mvc:mapping path="/user/**"/>
      <bean class="com.jikexueyuan.demo.springmvc.lesson4.interceptor.LoginInterceptor"></bean>
    </mvc:interceptor>
  </mvc:interceptors>

然后是bean指向的具體的interceptor類,如果session保存的用戶信息為null,則跳到login頁面,postHandle和afterCompletion方法都不執行,反之都執行。

?
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
package com.jikexueyuan.demo.springmvc.lesson4.interceptor;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
 
import com.jikexueyuan.demo.springmvc.lesson4.constant.Global;
 
public class LoginInterceptor implements HandlerInterceptor {
 
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    Object user = request.getSession().getAttribute(Global.USER_SESSION_KEY);
    if (user == null) {
      System.out.println("尚未登錄,調到登錄頁面");
      response.sendRedirect("/loginpage.html");
      return false;
    }
    
    return true;
  }
 
  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    System.out.println("postHandle");
  }
 
  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    System.out.println("afterCompletion");
  }
 
}

至此,簡單的springmvc攔截器就完成了。

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

原文鏈接:http://www.cnblogs.com/UniqueColor/p/5778199.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 花唇肿胀无法合拢双性 | 91调教| 免费观看全集 | 精品久久久久免费极品大片 | 国产自精品 | 欧美精品久久一区二区三区 | 15同性同志18 | 九九九九九九精品免费 | 国产女主播福利在线 | 男人晚上看的 | 好爽好深好猛好舒服视频上 | 日本草草视频在线观看 | 4438成人网| 男男视频18免费网站 | 含羞草国产亚洲精品岁国产精品 | 99久久精彩视频 | 爸爸的宝贝小说全文在线阅读 | 四虎新网站 | 男女18一级大黄毛片免 | 日韩成人在线免费视频 | 蜜桃成人影院 | 亚洲精品在线播放 | 我将她侵犯1~6樱花动漫在线看 | 九二淫黄大片看片 | 女人和拘做受全过程免费 | 欧美日韩国产精品综合 | 91制片在线观看 | 日本肉体xxxx69xxxx | 肉宠文很肉到处做1v1 | 538免费精品视频搬运工 | 九九免费高清在线观看视频 | 四虎www| 精品国产福利在线 | 99久久伊人精品波多野结衣 | 羞羞答答免费人成黄页在线观看国产 | 欧美坐爱 | 24adc年龄18岁欢迎大驾光临 | 久久人妻少妇嫩草AV無碼 | 九九精品免视频国产成人 | caoporn国产 | 欧美一级特黄特色大片免费 |