亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot登錄用戶權(quán)限攔截器

 更新時(shí)間:2021年03月21日 14:23:24   作者:strive_day  
這篇文章主要介紹了SpringBoot登錄用戶權(quán)限攔截器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

1. 創(chuàng)建自定義攔截器類并實(shí)現(xiàn) HandlerInterceptor 接口

package com.xgf.online_mall.interceptor;

import com.xgf.online_mall.system.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;

@Slf4j
@Component
public class UserLoginAuthInterceptor implements HandlerInterceptor {
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    log.info(" ======== UserAuthInterceptor preHandle 登錄權(quán)限攔截器攔截");
    User user = (User) request.getSession().getAttribute("loginUser");
    //未登錄才判斷,登錄了直接放行
    if(user == null){
      //獲取訪問(wèn)路徑
      String address = request.getRequestURI();
      log.info("======== 攔截,訪問(wèn)路徑 address : {}", address);
      response.sendRedirect(request.getContextPath() + "/login.html");
      return false;

      /*String address = request.getRequestURI();
      log.info("======== 攔截,訪問(wèn)路徑 address : {}", address);
      //不是登錄或者注冊(cè)頁(yè)面,就直接跳轉(zhuǎn)登錄界面
      if(!address.contains("login") && !address.contains("register")){
        //強(qiáng)制到登錄頁(yè)面
        response.sendRedirect(request.getContextPath() + "/login.html");
        //設(shè)置為false,不訪問(wèn)controller
        return false;
      }*/
    }
    //其它模塊或者已經(jīng)登錄,就直接放行
//    log.info("======== 已登錄 user = {}", user);
    return true;
  }


  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    log.info(" ===== UserAuthInterceptor postHandle");
  }

  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    log.info("==== UserAuthInterceptor afterCompletion");

    //記錄日志 向文件里面寫(xiě)日志
    //獲取服務(wù)器記錄日志log文件所存放的目錄位置 -- tomcat下的真實(shí)路徑+log目錄
    String logdir = request.getServletContext().getRealPath("log");
    //路徑不存在就創(chuàng)建
    Path logdirPath = Paths.get(logdir);
    if(Files.notExists(logdirPath)){
      Files.createDirectories(logdirPath);
    }
    //目錄存在就將數(shù)據(jù)[字符]寫(xiě)入 //存放日志的路徑+文件名
    Path logfile = Paths.get(logdir,"userlog.log");
    //logfile.toFile() paths轉(zhuǎn)換為File類型 true以追加的方式寫(xiě)入
    BufferedWriter writer = new BufferedWriter(new FileWriter(logfile.toFile(),true));

    //獲取登錄用戶信息
    User user = (User)request.getSession().getAttribute("loginUser");
    //記錄user信息,存入日志
    writer.write(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " >> " + user +"\r\n");
    writer.flush();
    writer.close();
  }
}

2. 創(chuàng)建WebMvcConfigurer接口實(shí)現(xiàn)類,注冊(cè)并生效自定義的攔截器

import com.xgf.online_mall.constant.PathConstantParam;
import com.xgf.online_mall.interceptor.UserLoginAuthInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
@Slf4j
public class LoginConfig implements WebMvcConfigurer {
  @Autowired
  private UserLoginAuthInterceptor userLoginAuthInterceptor;

  /**
   * addInterceptors方法設(shè)置攔截路徑
   *   addPathPatterns:需要攔截的訪問(wèn)路徑
   *   excludePathPatterns:不需要攔截的路徑,
   *   String數(shù)組類型可以寫(xiě)多個(gè)用","分割
   * @param registry
   */
  @Override
  public void addInterceptors(InterceptorRegistry registry){
    log.info(" ======== LoginConfig.addInterceptors");
    //添加對(duì)用戶未登錄的攔截器,并添加排除項(xiàng)
    //error路徑,excludePathPatterns排除訪問(wèn)的路徑在項(xiàng)目中不存在的時(shí)候,
    //springboot會(huì)將路徑變成 /error, 導(dǎo)致無(wú)法進(jìn)行排除。
    registry.addInterceptor(userLoginAuthInterceptor)
        .addPathPatterns("/**")
        .excludePathPatterns("/js/**", "/css/**", "/img/**", "/plugins/**")
        .excludePathPatterns("/login.html", "/register.html", "/system/user/login", "/system/user/login", "/index")
        .excludePathPatterns("/error");
  }
 }

到此這篇關(guān)于SpringBoot登錄用戶權(quán)限攔截器的文章就介紹到這了,更多相關(guān)SpringBoot 用戶權(quán)限攔截器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論