溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

springboot+springmvc如何實現登錄攔截

發(fā)布時間:2021-05-17 13:44:55 來源:億速云 閱讀:184 作者:小新 欄目:編程語言

這篇文章主要介紹了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
 */
@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")這個對象就告訴了程序你的本地化信息。如果不指定則使用系統(tǒng)默認的Locale。

  • classpath下尋找res_zh_CN.properties 若不存在

  • 那么會去找res_zh.properties,若還是不存在

  • 則會去尋找res.properties,要還是找不到的話,那么就該拋異常了:MissingResourceException.

// login是資源文件的名稱。
ResourceBundle login = ResourceBundle.getBundle("login");//不指定locale會使用系統(tǒng)默認的。

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name login, locale zh_CN

springboot是什么

springboot一種全新的編程規(guī)范,其設計目的是用來簡化新Spring應用的初始搭建以及開發(fā)過程,SpringBoot也是一個服務于框架的框架,服務范圍是簡化配置文件。

感謝你能夠認真閱讀完這篇文章,希望小編分享的“springboot+springmvc如何實現登錄攔截”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI