您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關springboot如何使用攔截器判斷是否登錄,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
自定義攔截器實現HandlerInterceptor接口
創建一個配置類繼承WebMvcConfigurerAdapter類并重寫addInterceptors方法
代碼:
@Component public class AdminLoginInterceptor implements HandlerInterceptor { // 在請求處理之前調用,只有返回true才會執行請求 @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { // 得到session HttpSession session = httpServletRequest.getSession(true); // 得到對象 Object admin = session.getAttribute("admin"); // 判斷對象是否存在 if(admin!=null){ return true; }else{ // 不存在則跳轉到登錄頁 httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/login/adminLogin"); return false; } } // 試圖渲染之后執行 @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { } // 在請求處理之后,視圖渲染之前執行 @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { } }
@SpringBootConfiguration public class AdminLoginAdapter extends WebMvcConfigurerAdapter { @Autowired AdminLoginInterceptor adminLoginInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(adminLoginInterceptor).addPathPatterns("/admin/**").excludePathPatterns("/login/**"); super.addInterceptors(registry); } }
package com.example.demo.interceptor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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; /** * 〈一句話功能簡述〉<br> * 〈攔截器〉 * * @author 丶Zh2Guo * @create 2018/11/22 * @since 1.0.0 */ public class LoginInterceptor implements HandlerInterceptor { private Logger logger = LoggerFactory.getLogger(LoginInterceptor.class); // 在請求處理之前,只有返回true才會執行請求 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { logger.info("[攔截器]啟動登錄狀態攔截"); // 得到session HttpSession session = request.getSession(); logger.info("[攔截器]sessionID:" + session.getId()); // 得到用戶信息 Object userInfo = session.getAttribute("userInfo"); //判斷用戶是否登錄 if (userInfo != null) { logger.info("[攔截器]用戶已經登錄,用戶名,密碼:" + session.getAttribute("userInfo")); return true; } else { //不存在跳轉至登錄頁 response.sendRedirect(request.getContextPath() + "/"); // 跳轉到首頁登錄 logger.info("[攔截器]用戶沒有登錄,已跳轉到:" + request.getContextPath() + "/"); return false; } } // 視圖渲染后執行 @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } // 請求處理后,視圖渲染前 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
覆蓋其addInterceptors接口,注冊自定義的攔截器
@Configuration 注解一定要有
package com.example.demo.interceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * 〈一句話功能簡述〉<br> * 〈自定義配置類〉 * * @author 丶Zh2Guo * @create 2018/11/22 * @since 1.0.0 */ @Configuration public class LoginConfig implements WebMvcConfigurer { /** * 該方法用于注冊攔截器 * 可注冊多個攔截器,多個攔截器組成一個攔截器鏈 */ @Override public void addInterceptors(InterceptorRegistry registry) { // addPathPatterns 添加路徑 // excludePathPatterns 排除路徑 registry.addInterceptor(new LoginInterceptor()) .addPathPatterns("/sys/*"); // 攔截sys路徑下的url // .excludePathPatterns(""); } }
/** * Copyright (C), 2017-2018, XXX有限公司 * FileName: LoginController * Author: 丶Zh2Guo * Date: 2018/11/22 11:10 * Description: 登錄 * History: * <author> <time> <version> <desc> * 作者姓名 修改時間 版本號 描述 */ package com.example.demo.controller; import com.example.demo.dao.UserDAO; import com.example.demo.pojo.User; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; /** * 〈一句話功能簡述〉<br> * 〈登錄〉 * * @author 丶Zh2Guo * @create 2018/11/22 * @since 1.0.0 */ @Controller public class LoginController { // 日志 private Logger logger = LoggerFactory.getLogger(LoginController.class); @Autowired private UserDAO userDAO; // 啟動服務自動跳轉登錄 // @RequestMapping(value = {"/", "/login"}) @RequestMapping(value = "/") public String login() { return "login"; } // 登錄 @RequestMapping(value = "/loginCheck", method = RequestMethod.POST) @ResponseBody public String loginCheck(HttpServletRequest request) { // 獲取登錄信息 String userName = request.getParameter("userName"); String password = request.getParameter("password"); // 封裝成對象 User user = new User(); user.setUserName(userName); user.setPassword(password); // 校驗用戶信息 User info = userDAO.checkUser(user); if (info != null) { request.getSession().setAttribute("userInfo", userName + "-" + password); logger.info("登錄成功,用戶名:" + userName + "密碼:" + password); return "success"; } else { logger.info("登錄失敗,用戶名:" + userName + "密碼:" + password); return "fail"; } } }
上述就是小編為大家分享的springboot如何使用攔截器判斷是否登錄了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。