拦截器:

  

package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException; @Configuration
public class WebConfiguration implements WebMvcConfigurer { @Bean
public SecurityInterceptor getSecurityInterceptor() {
return new SecurityInterceptor();
} @Override
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration addInterceptor = registry.addInterceptor(getSecurityInterceptor());
//排除配置
addInterceptor.excludePathPatterns("/login/**");//排除登录
// 拦截配置
addInterceptor.addPathPatterns("/**/**");
} private class SecurityInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
HttpSession session = request.getSession();
//判断是否已有该用户登录的session
if (session.getAttribute("account") != null) {
return true;
}
response.getWriter().print("not login");
return false;
}
}
}

  控制器:

package com.example.demo;

import com.example.demo.bean.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpSession; @RestController
public class MailController { @GetMapping("/user")
public User send() { User user = new User();
user.setName("Li Hua");
user.setAge(25);
user.setSex(1);
return user; } @GetMapping("/login")
public String login(HttpSession session) {
session.setAttribute("account", "ggband");
return "登录成功"; }
}

  

springboot之拦截器的更多相关文章

  1. SpringBoot自定义拦截器实现IP白名单功能

    SpringBoot自定义拦截器实现IP白名单功能 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8993331.html 首先,相关功能已经上线了,且先让我先 ...

  2. SpringBoot使用拦截器

    SpringBoot的拦截器只能拦截流经DispatcherServlet的请求,对于自定义的Servlet无法进行拦截. SpringMVC中的拦截器有两种:HandlerInterceptor和W ...

  3. SpringBoot 注册拦截器方式及拦截器如何获取spring bean实例

    SpringBoot 注册拦截器时,如果用New对象的方式的话,如下: private void addTokenForMallInterceptor(InterceptorRegistry regi ...

  4. springboot+springmvc拦截器做登录拦截

    springboot+springmvc拦截器做登录拦截 LoginInterceptor 实现 HandlerInterceptor 接口,自定义拦截器处理方法 LoginConfiguration ...

  5. SpringMVC拦截器与SpringBoot自定义拦截器

    首先我们先回顾一下传统拦截器的写法: 第一步创建一个类实现HandlerInterceptor接口,重写接口的方法. 第二步在XML中进行如下配置,就可以实现自定义拦截器了 SpringBoot实现自 ...

  6. springboot + 注解 + 拦截器 + JWT 实现角色权限控制

    1.关于JWT,参考: (1)10分钟了解JSON Web令牌(JWT) (2)认识JWT (3)基于jwt的token验证 2.JWT的JAVA实现 Java中对JWT的支持可以考虑使用JJWT开源 ...

  7. 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法

    最近使用SpringBoot的自定义拦截器,在拦截器中注入了一个DAO,准备下面作相应操作,拦截器代码: public class TokenInterceptor implements Handle ...

  8. springBoot 使用拦截器 入坑

    近期使用SpringBoot 其中用到了拦截器 结果我的静态资源被全部拦截了,让我导致了好久才搞好: 看下图项目结构: 问题描述:上图划红框的资源都被拦截器给拦截了,搞得项目中不能访问:解决问题就是在 ...

  9. springboot的拦截器Interceptor的性质

    Interceptor在springboot2.x版本的快速入门 实现HandlerInterceptor的接口,并重载它的三个方法:preHandle.postHandle.afterComplet ...

  10. SpringBoot MVC 拦截器

    SpringBoot MVC 环境搭建 在pom.xml添加spring-boot-starter-web   <dependency>   <groupId>org.spri ...

随机推荐

  1. webpack打包和gulp打包工具详细教程

    30分钟手把手教你学webpack实战 阅读目录 一:什么是webpack? 他有什么优点? 二:如何安装和配置 三:理解webpack加载器 四:理解less-loader加载器的使用 五:理解ba ...

  2. hive最全学习线路和实践练习

    非原创,源自:https://www.cnblogs.com/snowbook/p/5723767.html hive被大多数企业使用,学习它,利于自己掌握企业所使用的技术,这里从安装使用到概念.原理 ...

  3. 解决win7无法运行bat批处理文件的方法

    在win7系统中我们可以将一些命令制作为bat批处理文件,只需双击打开即可运行命令,方便使用. 那么,要怎么运行bat批处理呢?最近有用户反馈,遇到无法运行bat批处理的现象,该怎么办呢? 修复方法一 ...

  4. centos的nginx如何访问本地共享文件夹的文件 nginx访问404,403问题

    关键挂载 sudo vmhgfs-fuse .host:/musings /home/xxx -o allow_other,uid=0,gid=0

  5. Linux安装JDK.8

    E&T: CentOS_7.4 64位; jdk-8u191-linux-x64; Xftp5; Xshell5; P1.下载Linux环境下的jdk1.8 下载链接: https://www ...

  6. html基础学习笔记1

    <!DOCTYPE html> 声明为 HTML5 文档  <html> 元素是 HTML 页面的根元素 <head> 元素包含了文档的元(meta)数据,如 &l ...

  7. Salesforce Lightning Builder Flows (Salesforce Lightning 构建Flows)

    本文构建Salesforce Lightning Flows 只是一个简单的实现步骤,原文地址如下: https://trailhead.salesforce.com/en/content/learn ...

  8. Rogue 罗格

    发售年份 1980 平台 多平台 开发商 Michael Toy, Glenn Wichman, Ken Arnold 类型 策略/角色扮演 https://www.youtube.com/watch ...

  9. innobackupex一些常用备份选项

    指定数据库备份[root@node1]# innobackupex --defaults-file=/data/3306/my.cnf --user=root --password=password ...

  10. c# 程序只能运行一次(多次运行只能打开同一个程序)

    转自:https://social.msdn.microsoft.com/Forums/zh-CN/6398fb10-ecc2-4c03-ab25-d03544f5fcc9/2291420309357 ...