欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于SpringMVC實(shí)現(xiàn)網(wǎng)頁登錄攔截

 更新時(shí)間:2021年12月16日 16:35:02   作者:奔走的王木木Sir  
SpringMVC的處理器攔截器類似于Servlet開發(fā)中的過濾器Filter,用于對處理器進(jìn)行預(yù)處理和后處理。因此,本文將為大家介紹如何通過SpringMVC實(shí)現(xiàn)網(wǎng)頁登錄攔截功能,需要的小伙伴可以了解一下

1.簡介

SpringMVC的處理器攔截器類似于Servlet開發(fā)中的過濾器Filter,用于對處理器進(jìn)行預(yù)處理和后處理。

攔截器和過濾器的區(qū)別在于攔截器使AOP思想的具體應(yīng)用

過濾器

  • servlet規(guī)范中的一部分,任何java web工程都可以使用
  • 在url-pattern中配置了/*之后,可以對所有要訪問的資源進(jìn)行攔截
  • 需要重寫方法

攔截器

  • SpringMVC框架自己的,只有使用了SpringMVC框架的工程才能使用
  • 攔截器只會攔截訪問的控制器方法, 如果訪問的是jsp/html/css/image/js是不會進(jìn)行攔截的
  • 不需要重寫方法

2.自定義攔截器

??>新建一個(gè)Module,添加web支持

??>配置web.xml,applicationContext.xml,添加一個(gè)controller的包

??>編寫測試?

@RestController
public class TestController {
    @GetMapping("/t1")
    public String test(){
        System.out.println("TestController-->test()執(zhí)行了");
        return "ok";
    }
}

添加Artifact中的lib,以及配置Tomcat,啟動(dòng)測試出現(xiàn),證明Spring配置好了

??>編寫攔截器

package com.hxl.config;

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

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

public class MyInterceptor implements HandlerInterceptor {
    //在請求處理的方法之前執(zhí)行
    //return true;執(zhí)行下一個(gè)攔截器
    //如果返回false就不執(zhí)行下一個(gè)攔截器
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("------------處理前------------");
        return true;
    }
    //在請求處理方法執(zhí)行之后執(zhí)行
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("------------處理后------------");
    }
    //在dispatcherServlet處理后執(zhí)行,做清理工作.
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("------------清理------------");
    }
}

??>在applicationContext.xml中配置攔截器

<!--關(guān)于攔截器的配置-->
<mvc:interceptors>
    <mvc:interceptor>
        <!--/** 包括路徑及其子路徑-->
        <!--/admin/* 攔截的是/admin/add等等這種 , /admin/add/user不會被攔截-->
        <!--/admin/** 攔截的是/admin/下的所有-->
        <mvc:mapping path="/**"/>
        <!--bean配置的就是攔截器-->
        <bean class="com.hxl.config.MyInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

前面的我們都不動(dòng),運(yùn)行,我們可以看到效果

那么接下來就用一個(gè)實(shí)例來體驗(yàn)一下攔截器(登錄)

在WEB-INF下的所有頁面或者資源,只能通過controller或者servlet進(jìn)行訪問

3. 登錄攔截

3.1 先做一個(gè)頁面

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>

  <a href="${pageContext.request.contextPath}/goLogin" rel="external nofollow" >登錄</a>
  <a href="${pageContext.request.contextPath}/goMain" rel="external nofollow" >首頁</a>

  </body>
</html>

main.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<h1>首頁</h1>

</body>
</html>

login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登錄</title>
</head>
<body>

<h1>登錄頁面</h1>

<form action="${pageContext.request.contextPath}/login" method="post">
    用戶名:<input type="text" name="username">
    密碼:<input type="text" name="password">
    <input type="submit" value="登錄">
</form>
</body>
</html>

LoginController

package com.hxl.controller;

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

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @RequestMapping("/goMain")
    public String goMain(){
        return "main";
    }

    @RequestMapping("/goLogin")
    public String goLogin(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(HttpSession session,String username,String password){
        // 向session記錄用戶身份信息
        System.out.println("接收前端==="+username);
        session.setAttribute("user", username);
        return "main";
    }
}

測試

因?yàn)樵赪EB-INF下的 頁面我們不能直接訪問,所以在index中進(jìn)行跳轉(zhuǎn),然后請求到login.jsp。此時(shí)我們在這里輸入,就會在session中攜帶賬號密碼。

但此時(shí)是不符合的,因?yàn)槲覀冞€沒有登錄就可以去首頁,所以我們需要寫一個(gè)攔截器的功能

3.2 登錄攔截

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>

  <a href="${pageContext.request.contextPath}/user/goLogin" rel="external nofollow" >登錄</a>
  <a href="${pageContext.request.contextPath}/user/goMain" rel="external nofollow" >首頁</a>

  </body>
</html>

login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登錄</title>
</head>
<body>

<h1>登錄頁面</h1>

<form action="${pageContext.request.contextPath}/user/login" method="post">
    用戶名:<input type="text" name="username">
    密碼:<input type="text" name="password">
    <input type="submit" value="登錄">
</form>
</body>
</html>

main.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<h1>首頁</h1>
<p>${username}</p>
<p>
    <a href="${pageContext.request.contextPath}/user/goOut" rel="external nofollow" >注銷</a>
</p>
</body>
</html>

LoginInterceptor

此時(shí)我們?nèi)懸粋€(gè)登錄的攔截器,來判斷它到底什么時(shí)候進(jìn)行攔截

package com.hxl.config;

import org.springframework.web.servlet.HandlerInterceptor;

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

public class LoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        //放行:判斷什么情況下沒有登錄
        //登錄頁面放行
        if(request.getRequestURI().contains("goLogin")){
            return true;
        }
        if(request.getRequestURI().contains("login")){
            return true;
        }
        //用戶已登錄,第一次登錄的時(shí)候也是沒有session的。
        if(session.getAttribute("user") != null){
            return true;
        }
        //判斷什么情況下沒有登錄
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
        return false;
    }
}

LoginController

這里面我們有一個(gè)類url,下面的請求都需要加上/user,在配置攔截器的時(shí)候可以加一個(gè),只攔截user請求下的。以及注銷功能

package com.hxl.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class LoginController {

    @RequestMapping("/goMain")
    public String goMain(){
        return "main";
    }

    @RequestMapping("/goLogin")
    public String goLogin(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(HttpSession session, String username, String password, Model model){
        // 向session記錄用戶身份信息
        System.out.println("接收前端==="+username);
        session.setAttribute("user", username);
        model.addAttribute("username",username);
        return "main";
    }

    @RequestMapping("/goOut")
    public String goOut(HttpSession session){
        /*//銷毀,下面的好
        session.invalidate();*/
        //移除
        session.removeAttribute("user");
        return "main";
    }
}

applicationContext.xml

<!--關(guān)于攔截器的配置-->
<mvc:interceptors>
    <mvc:interceptor>
        <!--/** 包括路徑及其子路徑-->
        <!--/admin/* 攔截的是/admin/add等等這種 , /admin/add/user不會被攔截-->
        <!--/admin/** 攔截的是/admin/下的所有-->
        <mvc:mapping path="/**"/>
        <!--bean配置的就是攔截器-->
        <bean class="com.hxl.config.MyInterceptor"/>
    </mvc:interceptor>

    <mvc:interceptor>
        <!--user下面的請求-->
        <mvc:mapping path="/user/**"/>
        <bean class="com.hxl.config.LoginInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

測試

此時(shí)我們啟動(dòng)之后,如果沒有登錄,那么會重定向到goLogin頁面,然后登錄,登錄之后跳轉(zhuǎn)到main頁面,其中有注銷功能,沒有注銷之前可以去index頁面點(diǎn)擊首頁正常跳轉(zhuǎn),如果注銷了,session沒有了,那么就會跳轉(zhuǎn)到登錄頁面。?

以上就是基于SpringMVC實(shí)現(xiàn)網(wǎng)頁登錄攔截的詳細(xì)內(nèi)容,更多關(guān)于SpringMVC網(wǎng)頁登錄攔截的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • IDEA編譯時(shí)報(bào)常量字符串過長的解決辦法

    IDEA編譯時(shí)報(bào)常量字符串過長的解決辦法

    本文主要介紹了IDEA編譯時(shí)報(bào)常量字符串過長的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Spring data jpa的使用與詳解(復(fù)雜動(dòng)態(tài)查詢及分頁,排序)

    Spring data jpa的使用與詳解(復(fù)雜動(dòng)態(tài)查詢及分頁,排序)

    這篇文章主要介紹了Spring data jpa的使用與詳解(復(fù)雜動(dòng)態(tài)查詢及分頁,排序),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • JDK14的新特性NullPointerExceptions的使用

    JDK14的新特性NullPointerExceptions的使用

    這篇文章主要介紹了JDK14的新特性NullPointerExceptions的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • SSM如何實(shí)現(xiàn)在Controller中添加事務(wù)管理

    SSM如何實(shí)現(xiàn)在Controller中添加事務(wù)管理

    這篇文章主要介紹了SSM如何實(shí)現(xiàn)在Controller中添加事務(wù)管理,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java Char的簡單工具類CharUtil分享

    Java Char的簡單工具類CharUtil分享

    下面小編就為大家分享一篇Java Char的簡單工具類CharUtil,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • SpringBoot整合RabbitMQ 手動(dòng)應(yīng)答(簡單demo)

    SpringBoot整合RabbitMQ 手動(dòng)應(yīng)答(簡單demo)

    這篇文章主要介紹了SpringBoot整合RabbitMQ 手動(dòng)應(yīng)答 簡單demo,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • SpringBoot中的自定義Starter解讀

    SpringBoot中的自定義Starter解讀

    這篇文章主要介紹了SpringBoot中的自定義Starter解讀,啟動(dòng)器模塊其實(shí)是一個(gè)空的jar文件,里面沒有什么類、接口,僅僅是提供輔助性依賴管理,這些依賴可能用于自動(dòng)裝配或者其他類庫,需要的朋友可以參考下
    2023-12-12
  • Java實(shí)現(xiàn)搜索功能代碼詳解

    Java實(shí)現(xiàn)搜索功能代碼詳解

    這篇文章主要介紹了Java實(shí)現(xiàn)搜索功能代碼詳解,實(shí)現(xiàn)思路小編給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2017-12-12
  • springboot冪等切片的實(shí)現(xiàn)

    springboot冪等切片的實(shí)現(xiàn)

    本文主要介紹了springboot冪等切片的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Spring AOP使用之多切面運(yùn)行順序

    Spring AOP使用之多切面運(yùn)行順序

    這篇文章主要介紹了Spring AOP使用之多切面運(yùn)行順序,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02

最新評論