您好,登錄后才能下訂單哦!
小編給大家分享一下如何使用SpringBoot攔截器實現登錄攔截,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
可以對URL路徑進行攔截,可以用于權限驗證、解決亂碼、操作日志記錄、性能監控、異常處理等
新建 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邏輯執行之前 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle...."); String uri = request.getRequestURI(); System.out.println("當前路徑"+uri); /** * HandlerMethod=>Controller中標注@RequestMapping的方法 * 需要配置靜態資源不攔截時,添加這塊邏輯 => 前后端分離項目 */ if (!(handler instanceof HandlerMethod)) { return true; } if (httpSession.getAttribute("username") == null) { // 未登錄跳轉到登錄界面 throw new RuntimeException("no login!"); } else { return true; } } //Controller邏輯執行完畢但是視圖解析器還未進行解析之前 @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("postHandle...."); } //Controller邏輯和視圖解析器執行完畢 @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(); } }
實現類
@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; } }
當我們未登錄時我們不能進入攔截的頁面
登錄
登錄之后我們就能進入hello方法了
以上是“如何使用SpringBoot攔截器實現登錄攔截”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。