最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot+springmvc實現(xiàn)登錄攔截

 更新時間:2019年10月22日 08:28:31   作者:魔有追求  
這篇文章主要介紹了springboot+springmvc實現(xiàn)登錄攔截,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

這篇文章主要介紹了springboot+springmvc實現(xiàn)登錄攔截,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

  • LoginInterceptor 實現(xiàn) HandlerInterceptor 接口,自定義攔截器處理方法
  • LoginConfiguration 實現(xiàn) WebMvcConfigurer 接口,注冊攔截器
  • ResourceBundle 加載 properties文件數(shù)據(jù),配置不進(jìn)行攔截的路徑

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);//不進(jìn)行攔截路徑
  }

}

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)默認(rèn)的Locale。

  • classpath下尋找res_zh_CN.properties 若不存在
  • 那么會去找res_zh.properties,若還是不存在
  • 則會去尋找res.properties,要還是找不到的話,那么就該拋異常了:MissingResourceException.
// login是資源文件的名稱。
ResourceBundle login = ResourceBundle.getBundle("login");//不指定locale會使用系統(tǒng)默認(rèn)的。

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java調(diào)用wsdl接口的兩種方法(axis和wsimport)

    Java調(diào)用wsdl接口的兩種方法(axis和wsimport)

    本文主要介紹了Java調(diào)用wsdl接口的兩種方法(axis和wsimport),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • 如何在Spring boot加入shiro支持

    如何在Spring boot加入shiro支持

    這篇文章主要介紹了如何在Spring boot加入shiro支持,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • Java IO中字節(jié)流復(fù)制圖片實現(xiàn)代碼

    Java IO中字節(jié)流復(fù)制圖片實現(xiàn)代碼

    這篇文章主要介紹了Java IO中字節(jié)流復(fù)制圖片實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • SpringBoot中集成日志的四種方式

    SpringBoot中集成日志的四種方式

    在開發(fā)中,日志記錄是保障應(yīng)用程序健壯性、可維護(hù)性的重要手段,通過日志,我們可以記錄系統(tǒng)的運行狀態(tài)、捕獲異常并進(jìn)行調(diào)試,Spring Boot 默認(rèn)使用的是 Logback,但你也可以根據(jù)需求選擇其他框架,以下是幾種常用的日志集成方法,需要的朋友可以參考下
    2024-10-10
  • springcloud下hibernate本地化方言配置方式

    springcloud下hibernate本地化方言配置方式

    這篇文章主要介紹了springcloud下hibernate本地化方言配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • java實現(xiàn)單鏈表之逆序

    java實現(xiàn)單鏈表之逆序

    這篇文章主要介紹了應(yīng)用java語言實現(xiàn)單鏈表逆序,,需要的朋友可以參考下
    2015-07-07
  • Spring Cloud Consul的服務(wù)注冊與發(fā)現(xiàn)

    Spring Cloud Consul的服務(wù)注冊與發(fā)現(xiàn)

    這篇文章主要介紹了Spring Cloud Consul服務(wù)注冊與發(fā)現(xiàn)的實現(xiàn)方法,幫助大家更好的理解和學(xué)習(xí)使用spring框架,感興趣的朋友可以了解下
    2021-02-02
  • java 非常好用的反射框架Reflections介紹

    java 非常好用的反射框架Reflections介紹

    這篇文章主要介紹了java 反射框架Reflections的使用,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 關(guān)于Controller層和Service層的類報錯問題及解決方案

    關(guān)于Controller層和Service層的類報錯問題及解決方案

    這篇文章主要介紹了關(guān)于Controller層和Service層的類報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java安全 ysoserial CommonsCollections2示例分析

    Java安全 ysoserial CommonsCollections2示例分析

    這篇文章主要為大家介紹了Java安全 ysoserial CommonsCollections2示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11

最新評論

秭归县| 时尚| 建湖县| 儋州市| 嘉鱼县| 静宁县| 漳州市| 吉林市| 墨竹工卡县| 伊宁县| 洛南县| 得荣县| 安溪县| 涿鹿县| 旺苍县| 惠安县| 富民县| 平山县| 璧山县| 北票市| 南安市| 大埔县| 东阿县| 三穗县| 金坛市| 莲花县| 波密县| 抚远县| 泗洪县| 克什克腾旗| 合川市| 琼结县| 中方县| 凤凰县| 汉寿县| 界首市| 彭州市| 茌平县| 柳州市| 从江县| 马龙县|