Springboot2.0实现URL拦截
1、创建一个登陆拦截器SecurityInterceptor,它继承HandlerInterceptorAdapter类
package com.cn.commodity.config; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; public class SecurityInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
HttpSession session = request.getSession();
if (session.getAttribute(WebSecurityConfig.SESSION_KEY) != null) {
return true;
} // 跳转登录-------controller类中的登陆方法
String url = "/login";
response.sendRedirect(url);
return false;
}
}
2、创建一个URL拦截器WebSecurityConfig类,主要在这个类中实现URL拦截。注意:具体的拦截依据项目而定。
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.WebMvcConfigurerAdapter;
@Configuration
public class WebSecurityConfig extends WebMvcConfigurerAdapter { public static final String SESSION_KEY = "user"; @Bean
public SecurityInterceptor getSecurityInterceptor() {
return new SecurityInterceptor();
} @Override
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration addInterceptor = registry.addInterceptor(getSecurityInterceptor());
// 排除配置--对下面的不进行拦截
addInterceptor.excludePathPatterns("/index");
addInterceptor.excludePathPatterns("/login");
// addInterceptor.excludePathPatterns("/user/**");
addInterceptor.excludePathPatterns("/start/**");
// 拦截配置
addInterceptor.addPathPatterns("/**");
}
}
3、自己写一个controller和一些jsp,就可以实现了。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class JspController {
@RequestMapping("/index")
public String index(){
return "index";
} @RequestMapping("/login")
public String login(){
return "login";
}
}
4、jsp目录和配置图

Springboot2.0实现URL拦截的更多相关文章
- SpringBoot2.0针对请求参数@RequestBody验证统一拦截
title: "SpringBoot2.0针对请求参数@RequestBody验证的统一拦截"categories: SpringBoot2.0 Shirotags: Spring ...
- SpringBoot2.0 基础案例(05):多个拦截器配置和使用场景
一.拦截器简介 1.拦截器定义 拦截器,请求的接口被访问之前,进行拦截然后在之前或之后加入某些操作.拦截是AOP的一种实现策略. 拦截器主要用来按照指定规则拒绝请求. 2.拦截器中应用 Token令牌 ...
- SpringBoot2.0初识
核心特性 组件自动装配: Web MVC , Web Flux , JDBC 等 激活: @EnableAutoConfiguration 配置: /META_INF/spring.factories ...
- SpringBoot2.0+Mybatis-Plus3.0+Druid1.1.10 一站式整合
SpringBoot2.0+Mybatis-Plus3.0+Druid1.1.10 一站式整合 一.先快速创建一个springboot项目,其中pom.xml加入mybatis-plus 和druid ...
- SpringBoot2.0+Shiro+JWT 整合
SpringBoot2.0+Shiro+JWT 整合 JSON Web Token(JWT)是一个非常轻巧的规范.这个规范允许我们使用 JWT 在用户和服务器之间传递安全可靠的信息. 我们利用一定的编 ...
- spring-boot-2.0.3应用篇 - shiro集成
前言 上一篇:spring-boot-2.0.3源码篇 - 国际化,讲了如何实现国际化,实际上我工作用的模版引擎是freemaker,而不是thymeleaf,不过原理都是相通的. 接着上一篇,这一篇 ...
- spring-boot-2.0.3源码篇 - 国际化
前言 针对spring boot,网上已有很多优质的系列教程,我就不再班门弄斧了(实际上是担心没别人写的好,哈哈哈!).但是还是想蹭蹭spring boot的热度,即使不考虑微服务,spring bo ...
- SpringBoot2.0集成Shiro
1.shiro的三个核心概念: 1)Subject:代表当前正在执行操作的用户,但Subject代表的可以是人,也可以是任何第三方系统帐号.当然每个subject实例都会被绑定到SercurityMa ...
- springboot2.0+websocket集成【群发消息+单对单】(二)
https://blog.csdn.net/qq_21019419/article/details/82804921 版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上 ...
随机推荐
- 利用openssl完成自签发证书步骤--精华版
#CentOS 7 CA目录 cd /etc/pki/CA #建立 demoCA 目录结构mkdir -p ./demoCA/{private,newcerts}touch ./demoCA/inde ...
- deep_learning_Function_ lambda函数详解
这里总结了关于 Python 中的 lambda 函数的“一个语法,三个特性,四个用法”. 一个语法: 在 Python 中,lambda 函数的语法是唯一的.其形式如下: lambda argume ...
- three.js之创建一个几何体
<html> <head> <title>My first three.js app</title> <style> body { marg ...
- xampp下载和使用
XAMPP 下载地址: XAMPP HTML存放目录,也就是根目录,可以在这个目录进行添加HTML文件和PHP文件. C:\xampp\htdocs 访问web,localhost:80或者直接访问l ...
- 【转载】softmax的log似然代价函数(求导过程)
全文转载自:softmax的log似然代价函数(公式求导) 在人工神经网络(ANN)中,Softmax通常被用作输出层的激活函数.这不仅是因为它的效果好,而且因为它使得ANN的输出值更易于理解.同时, ...
- java8学习之BiFunction函数式接口实例演示&Predicate函数式接口详解
BiFunction函数式接口: 在上次中已经对BiFunction接口进行了初步的认识,这里对它进一步学习,这里打算新建一个Person实体,然后新建若干个Person的实例存放在集合中,最后再根据 ...
- 个人项目WordCount基础功能
码云地址:https://gitee.com/stedylan/WordCount 1.PSP表格: PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning 计划 10 1 ...
- 使用Sendinput以及GetAsyncKeyState来模拟按键延时
Code: #include <windows.h> #include <tchar.h> #include <iostream> BOOL flag = TRUE ...
- FutureWarning: get_value is deprecated and will be removed in a future release. Please use .at[] or .iat[] accessors instead print(labels_df.get_value(patients,col=1))
这是因为pandas的版本高了,0.21之后就已经将这个方法干掉了.直接装成0.20之前的就好
- sysbench简易使用
sysbench简易使用 由于测试需要,需要用到sysbench这个工具.推荐简便使用. # yum 安装 yum install sysbench 创建数据库 CREATE DATABASE `sb ...