springboot+springmvc实现登录拦截

作者:魔有追求 时间:2023-04-26 19:23:26 

这篇文章主要介绍了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

标签:spring,boot,mvc,登录,拦截
0
投稿

猜你喜欢

  • C#获取项目指定目录下文件的方法

    2023-04-19 07:15:26
  • java实现静默安装apk

    2023-08-31 08:11:34
  • GC算法实现垃圾优先算法

    2023-11-07 17:25:15
  • Springboot基于websocket实现简单在线聊天功能

    2023-12-21 08:58:18
  • Android实现跑马灯效果的方法

    2021-09-06 13:56:38
  • Mybatis结果生成键值对的实例代码

    2023-11-28 15:50:58
  • 浅谈c# 浮点数计算

    2023-08-04 22:10:09
  • Java定位问题线程解析

    2023-08-09 22:04:27
  • MyBatis-Plus 自定义sql语句的实现

    2022-12-24 23:16:16
  • spring boot整合netty的实现方法

    2021-11-17 04:28:03
  • Maven项目修改JDK版本全过程

    2021-07-19 12:13:29
  • java实现mongodb的数据库连接池

    2023-11-23 14:23:09
  • C#全局热键设置与窗体热键设置实例

    2022-09-30 05:38:52
  • SSM项目使用拦截器实现登录验证功能

    2023-06-17 16:12:38
  • Java JVM字节码指令集总结整理与介绍

    2021-09-18 17:10:20
  • java实现捕鱼达人游戏

    2023-11-23 21:28:59
  • springboot注解Aspect实现方案

    2022-12-17 19:32:06
  • 详解SpringMVC和MyBatis框架开发环境搭建和简单实用

    2022-03-11 13:54:51
  • java poi导出图片到excel示例代码

    2023-10-30 00:13:17
  • Android自定义覆盖层控件 悬浮窗控件

    2021-10-21 01:14:40
  • asp之家 软件编程 m.aspxhome.com