springboot+springmvc实现登录拦截
2020-02-13 13:12
926 查看
这篇文章主要介绍了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
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- spring boot 使用拦截器 实现 用户登录拦截
- spring boot MVC 三 用户注册和登录的实现
- Spring Boot / Spring MVC 入门实践 (三) : 入门项目介绍与用户注册登录的实现
- Spring Boot / Spring MVC 入门实践 (三) : 入门项目介绍与用户注册登录的实现
- springboot+jwt+shiro集成实现前后端分离的登录认证和拦截
- 如何在SpringBoot中配置拦截器,实现登录拦截?
- SpringBoot/SpringMVC整合Shiro(一):实现登录与注册(MD5加盐加密)
- spring springmvc shiro 实现单点登录demo
- 关于Spring+SpringMVC+MyBatis+Mysql+Maven实现简易的登录操作(详细)
- Spring Boot + Security + JWT 实现Token验证+多Provider——登录系统
- springboot 实现拦截的五种姿势
- 使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- 使用SSM(Spring+SpringMVC+Mybatis)框架搭建服务器实现登录验证
- springboot之配置springmvc(登录拦截和设置默认首页)
- (看了之后保证学会超仔细)Springboot整合Spring security实现用户登录验证入门项目
- 10.1更 spring boot 登录拦截(附上代码)
- SpringBoot 并发登录人数控制的实现方法
- SpringBoot使用自定义注解实现权限拦截的示例
- SSH网站开发实录(4)(实现登录功能)SpringMVC+Spring+Hibernate
- SpringBoot2.0整合SpringSecurity实现自定义表单登录