springboot+springmvc实现登录拦截
这篇文章主要介绍了springboot+springmvc实现登录拦截,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
- LoginInterceptor实现HandlerInterceptor接口,自定义拦截器处理方法
- LoginConfiguration实现WebMvcConfigurer接口,注册拦截器
- ResourceBundle加载properties文件数据,配置不进行拦截的路径
LoginInterceptor
packagecom.ytkj.smart_sand.system.interceptor; importcom.alibaba.fastjson.JSONObject; importcom.ytkj.smart_sand.base.DataResponse; importcom.ytkj.smart_sand.dict.user.Dic_sysuser_sessionkey; importcom.ytkj.smart_sand.pojo.user.SysUser; importorg.springframework.web.servlet.HandlerInterceptor; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; /** *@description: *@author:changzhou.xie@yuantiaokj.com *@date:2019/10/2117:04 */ publicclassLoginInterceptorimplementsHandlerInterceptor{ @Override publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{ System.out.println("requestURI:"+request.getRequestURI()); SysUsersysUser=(SysUser)request.getSession().getAttribute(Dic_sysuser_sessionkey.CURRENT_USER); if(sysUser==null){ DataResponseresult=DataResponse.failure("0100","用户没有登录"); response.setContentType("application/json;charset=UTF-8"); response.getWriter().write(JSONObject.toJSONString(result)); returnfalse; } returntrue; } }
LoginConfiguration
packagecom.ytkj.smart_sand.config; importcom.ytkj.smart_sand.dict.system.Dict_decollator; importcom.ytkj.smart_sand.system.interceptor.LoginInterceptor; importcom.ytkj.smart_sand.system.properties.LoginInfoProperties; importorg.apache.commons.lang3.StringUtils; importorg.springframework.context.annotation.Configuration; importorg.springframework.web.servlet.config.annotation.InterceptorRegistry; importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** *@description: *@author:changzhou.xie@yuantiaokj.com *@date:2019/10/2117:11 */ @Configuration publicclassLoginConfigurationimplementsWebMvcConfigurer{ /* 注意拦截路径的写法: /**/*.html表示所有的html文件。 /img/**表示img目录下的所有文件。 */ @Override publicvoidaddInterceptors(InterceptorRegistryregistry){ Stringpaths=LoginInfoProperties.getValue("loginReleasePaths"); String[]loginReleasePaths; if(StringUtils.isNotBlank(paths)){ loginReleasePaths=paths.split(Dict_decollator.ENG_COMMA); }else{ loginReleasePaths=newString[0]; } registry.addInterceptor(newLoginInterceptor()) .addPathPatterns("/**")//拦截路径 .excludePathPatterns(loginReleasePaths);//不进行拦截路径 } }
LoginInfoProperties
packagecom.ytkj.smart_sand.system.properties; importjava.util.MissingResourceException; importjava.util.ResourceBundle; /** *@description: *@author:changzhou.xie@yuantiaokj.com *@date:2019/10/2116:59 */ publicclassLoginInfoProperties{ privatestaticfinalStringLOGIN="login"; privatestaticResourceBundleLOGIN_BUNDLE=ResourceBundle.getBundle(LOGIN); publicstaticStringgetValue(Stringkey){ try{ returnLOGIN_BUNDLE.getString(key); }catch(MissingResourceExceptione){ e.printStackTrace(); } return""; } }
login.properties
#main/resources/login.properties #/**/*.html表示所有的html文件。 #/img/**表示img目录下的所有文件。 loginReleasePaths=/img/**,\ /**/*.html,\ /user/login/pc
ResourceBundle
是一个加载properties文件的工具类。支持国际化。从classpath中加载配置文件。
文件命名方式baseName_国别_语言.properties
ResourceBundlebundle=ResourceBundle.getBundle("res",newLocale("zh","CN"));
newLocale("zh","CN")这个对象就告诉了程序你的本地化信息。如果不指定则使用系统默认的Locale。
- classpath下寻找res_zh_CN.properties若不存在
- 那么会去找res_zh.properties,若还是不存在
- 则会去寻找res.properties,要还是找不到的话,那么就该抛异常了:MissingResourceException.
//login是资源文件的名称。 ResourceBundlelogin=ResourceBundle.getBundle("login");//不指定locale会使用系统默认的。 Exceptioninthread"main"java.util.MissingResourceException:Can'tfindbundleforbasenamelogin,localezh_CN
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。