springboot+springmvc实现登录拦截
作者:魔有追求 发布时间:2023-04-26 19:23:26
标签:spring,boot,mvc,登录,拦截
这篇文章主要介绍了springboot+springmvc实现登录拦截,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
LoginInterceptor 实现 HandlerInterceptor 接口,自定义 * 处理方法
LoginConfiguration 实现 WebMvcConfigurer 接口,注册 *
ResourceBundle 加载 properties文件数据,配置不进行拦截的路径
LoginInterceptor
package com.ytkj.smart_sand.system.interceptor;
import com.alibaba.fastjson.JSONObject;
import com.ytkj.smart_sand.base.DataResponse;
import com.ytkj.smart_sand.dict.user.Dic_sysuser_sessionkey;
import com.ytkj.smart_sand.pojo.user.SysUser;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @description:
* @author: changzhou.xie@yuantiaokj.com
* @date: 2019/10/21 17:04
*/
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("requestURI:" + request.getRequestURI());
SysUser sysUser = (SysUser) request.getSession().getAttribute(Dic_sysuser_sessionkey.CURRENT_USER);
if(sysUser == null){
DataResponse result = DataResponse.failure("0100", "用户没有登录");
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(JSONObject.toJSONString(result));
return false;
}
return true;
}
}
LoginConfiguration
package com.ytkj.smart_sand.config;
import com.ytkj.smart_sand.dict.system.Dict_decollator;
import com.ytkj.smart_sand.system.interceptor.LoginInterceptor;
import com.ytkj.smart_sand.system.properties.LoginInfoProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @description:
* @author: changzhou.xie@yuantiaokj.com
* @date: 2019/10/21 17:11
*/
@Configuration
public class LoginConfiguration implements WebMvcConfigurer {
/*
注意拦截路径的写法:
/**/*.html 表示所有的html文件。
/img/** 表示img目录下的所有文件。
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
String paths = LoginInfoProperties.getValue("loginReleasePaths");
String[] loginReleasePaths;
if(StringUtils.isNotBlank(paths)){
loginReleasePaths = paths.split(Dict_decollator.ENG_COMMA);
}else{
loginReleasePaths = new String[0];
}
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**")//拦截路径
.excludePathPatterns(loginReleasePaths);//不进行拦截路径
}
}
LoginInfoProperties
package com.ytkj.smart_sand.system.properties;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
/**
* @description:
* @author: changzhou.xie@yuantiaokj.com
* @date: 2019/10/21 16:59
*/
public class LoginInfoProperties {
private static final String LOGIN = "login";
private static ResourceBundle LOGIN_BUNDLE = ResourceBundle.getBundle(LOGIN);
public static String getValue(String key){
try {
return LOGIN_BUNDLE.getString(key);
} catch (MissingResourceException e) {
e.printStackTrace();
}
return "";
}
}
login.properties
# main/resources/login.properties
# /**/*.html 表示所有的html文件。
# /img/** 表示img目录下的所有文件。
loginReleasePaths=/img/**,\
/**/*.html,\
/user/login/pc
ResourceBundle
是一个加载properties文件的工具类。支持国际化。从classpath中加载配置文件。
文件命名方式 baseName_国别_语言.properties
ResourceBundle bundle = ResourceBundle.getBundle("res", new Locale("zh", "CN"));
new Locale("zh", "CN")这个对象就告诉了程序你的本地化信息。如果不指定则使用系统默认的Locale。
classpath下寻找res_zh_CN.properties 若不存在
那么会去找res_zh.properties,若还是不存在
则会去寻找res.properties,要还是找不到的话,那么就该抛异常了:MissingResourceException.
// login是资源文件的名称。
ResourceBundle login = ResourceBundle.getBundle("login");//不指定locale会使用系统默认的。
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name login, locale zh_CN
来源:https://www.cnblogs.com/mozq/p/11715708.html
0
投稿
猜你喜欢
- springboot配置文件中属性变量引用@@这种属性应用方式是field_name=@field_value@。两个@符号是springb
- 翻看印象笔记发现自己整理过arraycopy()这样一个方法,码字放到这里:System.arraycopy()是一个静态方法,用来实现重置
- 1. 函数式接口的理解根据重构的思想,需要把容易变化的模块进行抽象并封装起来,从这个点来看,Java8新引入的函数式接口就是基于这个思想进行
- fifter、servlet、interceptorfifter用来处理请求头、请求参数、编码的一些设置,然后转交给servlet,处理业务
- Java8实现菜单树形数据当我们打开京东商城时,左侧的菜单依次分为 * 展示,这是如何实现的呢?本篇暂不讲述前端,只讲述如何使用java8 的
- 1.使用keySet进行遍历实现步骤:(1)使用Map集合中的方法keySet(),把Map集合所有的key取出来,存储到一个Set集合中(
- 前言在上一章节Spring和Mybatis整合的原理详解中有写到Spring和MyBatis整合时用到的Bean扫描是Spring本身提供的
- 我计划在后续的一段时间内,写一系列关于java 9的文章,虽然java 9 不像Java 8或者Java 11那样的核心java版本,但是还
- 引言用过Spring Cloud的同学都知道在使用动态配置刷新的我们要配置一个 @RefreshScope,在类上才可以实现对象属性的的动态
- wait(), notify(), notifyAll()等方法介绍在Object.java中,定义了wait(), notify()和no
- 一、前言今天实现一个时钟工具,其实在之前已经完成了一个简单的时钟工具:【Unity3D应用案例系列】时钟、钟表小组件开发。那么,今天的这个小
- ListBox控件的使用: 1)控件属性 Items SelectedItems SelectioModes 2)数据绑定 DataSour
- 项目介绍基于Layui的后台管理系统模板,扩展Layui原生UI样式,整合第三方开源组件,提供便捷快速的开发方式,延续LayuiAdmin的
- 一、前期准备提示:如果友友你没有看过系列一的文章点击这个链接:王者荣耀中一个英雄是怎么被产生的?(一)我们现在功能比较多,所有为了让程序运行
- 我就废话不多说了,大家还是直接看代码吧~package com.jalor;import java.util.ArrayList;impor
- 1,创建存储过程create proc Pro_Login(@UserName nvarchar(10),@PassWord nvarcha
- strftime函数主要用于时间格式化,它的函数原型如下:size_t __cdecl strftime(char * __restrict
- 如果还不知道DecorView,那也没有什么关系 ^_^先来看看实现的效果实现的大致思路首先需要明白什么是DecorView,他是andro
- 将DataGrid中上面这个表头变成下面的两行表头,你会怎么实现?很巧妙地截断和补充td tr来实现来源:http://www.cnsend
- 目录什么是异常?编译时还是运行时?“受检异常”究竟可不可取?我的观点什么是异常?要了解受检异常,首先要了解什么是异常。在Java中,异常是一