springboot中怎么利用springmvc实现登录拦截

发布时间:2021-08-07 14:21:08 作者:Leah
来源:亿速云 阅读:113

springboot中怎么利用springmvc实现登录拦截,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

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 */@Configurationpublic 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

看完上述内容,你们掌握springboot中怎么利用springmvc实现登录拦截的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. 探索SpringBoot中的SpringMVC
  2. SpringBoot拦截器如何实现登录拦截

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot springmvc

上一篇:java中怎么实现一个商品管理系统

下一篇:如何解决某些HTML字符打不出来的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》