您好,登錄后才能下訂單哦!
這篇文章主要介紹了SpringBoot登錄用戶權限攔截器的實現方法,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
1. 創建自定義攔截器類并實現 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 登錄權限攔截器攔截"); User user = (User) request.getSession().getAttribute("loginUser"); //未登錄才判斷,登錄了直接放行 if(user == null){ //獲取訪問路徑 String address = request.getRequestURI(); log.info("======== 攔截,訪問路徑 address : {}", address); response.sendRedirect(request.getContextPath() + "/login.html"); return false; /*String address = request.getRequestURI(); log.info("======== 攔截,訪問路徑 address : {}", address); //不是登錄或者注冊頁面,就直接跳轉登錄界面 if(!address.contains("login") && !address.contains("register")){ //強制到登錄頁面 response.sendRedirect(request.getContextPath() + "/login.html"); //設置為false,不訪問controller return false; }*/ } //其它模塊或者已經登錄,就直接放行 // 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"); //記錄日志 向文件里面寫日志 //獲取服務器記錄日志log文件所存放的目錄位置 -- tomcat下的真實路徑+log目錄 String logdir = request.getServletContext().getRealPath("log"); //路徑不存在就創建 Path logdirPath = Paths.get(logdir); if(Files.notExists(logdirPath)){ Files.createDirectories(logdirPath); } //目錄存在就將數據[字符]寫入 //存放日志的路徑+文件名 Path logfile = Paths.get(logdir,"userlog.log"); //logfile.toFile() paths轉換為File類型 true以追加的方式寫入 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. 創建WebMvcConfigurer接口實現類,注冊并生效自定義的攔截器
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方法設置攔截路徑 * addPathPatterns:需要攔截的訪問路徑 * excludePathPatterns:不需要攔截的路徑, * String數組類型可以寫多個用","分割 * @param registry */ @Override public void addInterceptors(InterceptorRegistry registry){ log.info(" ======== LoginConfig.addInterceptors"); //添加對用戶未登錄的攔截器,并添加排除項 //error路徑,excludePathPatterns排除訪問的路徑在項目中不存在的時候, //springboot會將路徑變成 /error, 導致無法進行排除。 registry.addInterceptor(userLoginAuthInterceptor) .addPathPatterns("/**") .excludePathPatterns("/js/**", "/css/**", "/img/**", "/plugins/**") .excludePathPatterns("/login.html", "/register.html", "/system/user/login", "/system/user/login", "/index") .excludePathPatterns("/error"); } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“SpringBoot登錄用戶權限攔截器的實現方法”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。