Java-SpringBoot登陆拦截器

前言

在很多javaweb项目中都会写拦截器,而这拦截器只写一次,大多时间和精力都花在业务逻辑,数据安全等方面,从而忽略掉拦截器的写法

就比如我,每次写新项目时,在写到后台登陆等情况时,我总是想不起来如何对拦截器下手,不知该如何写起

为此写了篇文章,巩固一下

下面我就只贴代码,不做过多解释,我觉得注释写的也是很详细的

pom.xml

COPY
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
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

创建页面

创建项目:具体详细步骤,此处不做过多解释

在项目的src\resource\templates下新建三个页面

  1. index.html

    COPY
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <!DOCTYPE html>
    <html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    首页
    <a href="/admin">直接访问后台</a>
    <a href="/toadmin?succeed=true">成功进入后台</a>
    <!-- 判断拦截器返回的参数是否为null -->
    <th:block th:if="${message}!=null">
    <script>
    // 获取拦截器返回的错误参数
    alert("[[${message}]]")
    </script>
    </th:block>
    </body>
    </html>
  2. admin.html

    COPY
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <!DOCTYPE html>
    <html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    后台
    <a th:href="index">返回首页</a>
    <a th:href="adminpwd">修改密码</a>
    <script>
    alert("你已进入后台!")
    </script>
    </body>
    </html>
  3. password.html

    COPY
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <!DOCTYPE html>
    <html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    修改密码
    <a th:href="index">返回首页</a>
    </body>
    </html>

编辑java代码

创建两个包controllerconfig

  1. controller下新建User.java

    COPY
    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
    package top.lete114.test.controller;

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

    import javax.servlet.http.HttpSession;

    /**
    * @author Lete乐特
    * @createDate 2021- 01-24 11:57
    */
    @Controller
    public class User {
    // 首页
    @GetMapping({"/","/index","/index.html"})
    public String index(){
    System.out.println("这是首页");
    return "/index";
    }

    /*
    * 通过/toadmin?succeed=true 访问
    * 并且succeed参数为true才能进入,否则拦截
    */
    // 进入后台
    @GetMapping({"/admin","/admin.html"})
    public String admin(){
    System.out.println("这是后台");
    return "/admin";
    }
    // 后台功能其中的一部分 修改密码
    @GetMapping("/adminpwd")
    public String adminPassword(){
    System.out.println("这是后台修改密码");
    return "/password";
    }

    // 进入后台处理器
    @GetMapping("/toadmin")
    public String ToAdmin(boolean succeed, HttpSession session){
    // 通过/toadmin?succeed=true 访问并且传入值为true
    if(succeed){
    // 存储登陆用户的session
    // 这个session拦截器需要用到
    session.setAttribute("succeed","登陆成功");
    return "/admin";
    }
    // 未满足条件则踢回首页
    return "/index";
    }
    }
  2. controller下新建MvcConfig.javaLoginHandlerInterceptor.java

    COPY
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    package top.lete114.test.config;

    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

    /**
    * @author Lete乐特
    * @createDate 2021- 01-24 12:02
    */
    @Configuration
    public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    // 添加一个拦截器,拦截以/admin为前缀的url路径
    registry.addInterceptor(new LoginHandlerInterceptor())
    .addPathPatterns("/admin**");// 拦截已admin开头的请求
    // .excludePathPatterns("/login");// 释放不要拦截的请求 比如login,静态资源等
    }
    }
    COPY
    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
    package top.lete114.test.config;

    import org.springframework.web.servlet.HandlerInterceptor;

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

    /**
    * @author Lete乐特
    * @createDate 2020- 11-11 15:42
    */
    public class LoginHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
    // 获取控制层的设置的session
    Object admin = request.getSession().getAttribute("succeed");
    // 如果为获取,则拦截,并且重定向到首页

    if (null == admin) {
    request.setAttribute("message", "已拦截!");
    request.getRequestDispatcher("/").forward(request,response);
    return false;
    } else {
    request.removeAttribute("message");
    return true;
    }
    }
    }
Authorship: Lete乐特
Article Link: https://blog.imlete.cn/article/Java-SpringBoot-Interceptor.html
Copyright: All posts on this blog are licensed under the CC BY-NC-SA 4.0 license unless otherwise stated. Please cite Lete乐特 's Blog !