SpringBoot攔截器實現(xiàn)登錄攔截的示例代碼
可以對URL路徑進(jìn)行攔截,可以用于權(quán)限驗證、解決亂碼、操作日志記錄、性能監(jiān)控、異常處理等

實現(xiàn)代碼
新建 interceptor包

添加攔截器代碼
package com.qcby.interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginInterceptor implements HandlerInterceptor {
@Autowired
private HttpSession httpSession;
//Controller邏輯執(zhí)行之前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle....");
String uri = request.getRequestURI();
System.out.println("當(dāng)前路徑"+uri);
/**
* HandlerMethod=>Controller中標(biāo)注@RequestMapping的方法
* 需要配置靜態(tài)資源不攔截時,添加這塊邏輯 => 前后端分離項目
*/
if (!(handler instanceof HandlerMethod)) {
return true;
}
if (httpSession.getAttribute("username") == null) {
// 未登錄跳轉(zhuǎn)到登錄界面
throw new RuntimeException("no login!");
} else {
return true;
}
}
//Controller邏輯執(zhí)行完畢但是視圖解析器還未進(jìn)行解析之前
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle....");
}
//Controller邏輯和視圖解析器執(zhí)行完畢
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("afterCompletion....");
}
}注冊,配置攔截路徑和排除登錄需訪問路徑

package com.qcby.config;
import com.qcby.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor())
.addPathPatterns("/**")
// 那些路徑不攔截
.excludePathPatterns("/user/login","/error");
}
@Bean
public LoginInterceptor loginInterceptor(){
return new LoginInterceptor();
}
}實現(xiàn)類
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private HttpSession session;
@ApiOperation("用戶登錄接口")
@RequestMapping(value="login",method = {RequestMethod.GET,RequestMethod.POST})
public Map<String,Object>login(User user){
Map<String,Object> map=new HashMap<>();
map.put("code",0);
if(StringUtils.isEmpty(user.getUsername())||StringUtils.isEmpty(user.getPassword())){
map.put("msg","用戶或密碼為空!");
return map;
}
QueryWrapper<User> queryWrapper=new QueryWrapper<>();
queryWrapper.eq("username",user.getUsername())
.eq("password",user.getPassword());
User user1=userService.getOne(queryWrapper);
if(user1!=null){
map.put("cod",1);
map.put("data",user1);
session.setAttribute("username",user1.getUsername());
}else {
map.put("msg","用戶名或密碼錯誤!");
}
return map;
}
}當(dāng)我們未登錄時我們不能進(jìn)入攔截的頁面

登錄

登錄之后我們就能進(jìn)入hello方法了

到此這篇關(guān)于SpringBoot攔截器實現(xiàn)登錄攔截的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot攔截器實現(xiàn)登錄攔截內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ArrayList和LinkedList區(qū)別及使用場景代碼解析
這篇文章主要介紹了ArrayList和LinkedList區(qū)別及使用場景代碼解析,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
遠(yuǎn)程連接Jedis和整合SpringBoot的詳細(xì)過程
這篇文章主要介紹了遠(yuǎn)程連接Jedis和整合SpringBoot的詳細(xì)過程,本文通過圖文實例相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
SpringBoot條件注解@Conditional詳細(xì)解析
這篇文章主要介紹了SpringBoot條件注解@Conditional詳細(xì)解析,@Conditional是Spring4.0提供的一個用于條件裝配的注解,其定義了一個Condition的數(shù)組,只有當(dāng)數(shù)組所有的條件都滿足的時候,組件才會被導(dǎo)入容器,需要的朋友可以參考下2023-11-11
全面解析Spring Security 內(nèi)置 Filter
這篇文章主要介紹了Spring Security 內(nèi)置 Filter的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Elasticsearch開發(fā)AtomicArray使用示例探究
這篇文章主要為大家介紹了Elasticsearch AtomicArray使用示例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
springboot?vue項目管理后端實現(xiàn)接口新增
這篇文章主要為大家介紹了springboot?vue項目管理后端實現(xiàn)接口新增,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
取消idea雙擊shift鍵時出現(xiàn)的全局搜索的問題分析
這篇文章主要介紹了取消idea雙擊shift鍵時出現(xiàn)的全局搜索的問題分析,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-10-10

