SpringBoot實(shí)現(xiàn)登錄攔截器的方法詳解
在項(xiàng)目目錄下建立兩個(gè)包:inter 與contsfig
在inter新建層中實(shí)現(xiàn)HandlerInterceptor的繼承類(lèi)
package com.example.gameboxadminserver.inter; import com.example.gameboxadminserver.entity.User; 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 MyInterceptor implements HandlerInterceptor { //在preHandle方法中進(jìn)行登錄判斷 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session = request.getSession(); //session.setAttribute("adminName","o"); String adminName = (String)session.getAttribute("adminName");//獲取儲(chǔ)存的session //System.out.println(adminName); if(adminName==null){ System.out.println("請(qǐng)先登陸!"); return false; } return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { //System.out.println("執(zhí)行了TestInterceptor的postHandle方法"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
在conrsfig中新增WebMvcConfiguer的繼承類(lèi)LoginConfig
實(shí)現(xiàn)addInterceptors方法
package com.example.gameboxadminserver.contsfig; import com.example.gameboxadminserver.inter.MyInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class LoginConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { //注冊(cè)攔截器 InterceptorRegistration registration = registry.addInterceptor(new MyInterceptor()); registration.addPathPatterns("/**"); //所有路徑都被攔截 registration.excludePathPatterns( //添加不攔截路徑 "/admin/adminLogin", ); } }
在serviceImpl層
實(shí)現(xiàn)登錄邏輯并保存session
Httpsession session
session.setAttribute(“name”,value);
package com.example.gameboxadminserver.service.impl; import com.example.gameboxadminserver.entity.Admin; import com.example.gameboxadminserver.entity.Result; import com.example.gameboxadminserver.entity.ResultUtil; import com.example.gameboxadminserver.mapper.AdminMapper; import com.example.gameboxadminserver.service.AdminService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; /** * <p> * 服務(wù)實(shí)現(xiàn)類(lèi) * </p> * * @author firstGroup * @since 2020-10-28 */ @Service public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService { @Autowired AdminMapper adminMapper; @Override public Result adminLogin(HttpSession session,String adminName, String adminPwd) { Admin admin = adminMapper.adminLogin(adminName,adminPwd); if(admin!=null){ session.setAttribute("adminName",adminName); return ResultUtil.success("登陸成功!"); } return ResultUtil.error(2000,"登陸失敗"); } }
這樣就寫(xiě)完啦
功能測(cè)試
登陸失敗
無(wú)法訪問(wèn)其他接口
登錄成功
成功訪問(wèn)其他接口
到此這篇關(guān)于SpringBoot實(shí)現(xiàn)登錄攔截器的方法詳解的文章就介紹到這了,更多相關(guān)SpringBoot登錄攔截器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+ java 實(shí)現(xiàn)多級(jí)菜單遞歸效果
這篇文章主要介紹了vue+ java 實(shí)現(xiàn)多級(jí)菜單遞歸效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12SpringBoot項(xiàng)目如何將Bean注入到普通類(lèi)中
這篇文章主要介紹了SpringBoot項(xiàng)目如何將Bean注入到普通類(lèi)中,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11SpringBoot?實(shí)現(xiàn)CAS?Server統(tǒng)一登錄認(rèn)證的詳細(xì)步驟
??CAS(Central?Authentication?Service)中心授權(quán)服務(wù),是一個(gè)開(kāi)源項(xiàng)目,目的在于為Web應(yīng)用系統(tǒng)提供一種可靠的單點(diǎn)登錄,這篇文章主要介紹了SpringBoot?實(shí)現(xiàn)CAS?Server統(tǒng)一登錄認(rèn)證,需要的朋友可以參考下2024-02-02maven項(xiàng)目打jar包并包含所有依賴(lài)詳細(xì)教程
maven打包生成的普通jar包,只包含該工程下源碼編譯結(jié)果,不包含依賴(lài)內(nèi)容,下面這篇文章主要給大家介紹了關(guān)于maven項(xiàng)目打jar包并包含所有依賴(lài)的相關(guān)資料,需要的朋友可以參考下2023-05-05Java阻塞隊(duì)列必看類(lèi):BlockingQueue快速了解大體框架和實(shí)現(xiàn)思路
這篇文章主要介紹了Java阻塞隊(duì)列必看類(lèi):BlockingQueue快速了解大體框架和實(shí)現(xiàn)思路,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10