Java Web檢查用戶登錄狀態(tài)(防止用戶訪問到非法頁面)
使用攔截器
- 在方法前標(biāo)注自定義注解
- 攔截所有請求,只處理帶有該注解的方法
自定義注解:
- 常用元注解:
@Target,@Rentention,@Document,@Inherited - 如何讀取注解:
-Method.getDeclaredAnnotations()-Method.getAnnotaion(Class<T> annotationClass)
業(yè)務(wù)場景:未登陸狀態(tài)下,用戶不能訪問需要登陸才能訪問的頁面,例如修改個(gè)人信息頁面等。
1. 自定義注解
package com.nowcoder.community.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginRequired {
}2. 在方法前加上該注解
@LoginRequired
@RequestMapping(path = "/setting",method = RequestMethod.GET)
public String getSettingPage(){
return "/site/setting";
}3. 定義攔截器
package com.nowcoder.community.controller.Interceptor;
import com.nowcoder.community.annotation.LoginRequired;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
@Component
public class LoginRequireInterception implements HandlerInterceptor {
@Autowired
private HostHolder hostHolder;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if(handler instanceof HandlerMethod) { // 攔截到類型為方法
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod(); // 獲取方法
LoginRequired loginRequired = method.getAnnotation(LoginRequired.class); // 獲取方法的注解
if (loginRequired != null && hostHolder.getUser() == null) { // 方法是loginRequired且user沒登陸,需要攔截
response.sendRedirect(request.getContextPath() + "/login");
return false;
}
}
return true;
}
}4. 配置攔截器
package com.nowcoder.community.config;
import com.nowcoder.community.controller.Interceptor.AlphaInterceptor;
import com.nowcoder.community.controller.Interceptor.LoginRequireInterception;
import com.nowcoder.community.controller.Interceptor.LoginTicketInterceptor;
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;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private AlphaInterceptor alphaInterceptor;
@Autowired
private LoginTicketInterceptor loginTicketInterceptor;
@Autowired
private LoginRequireInterception loginRequireInterception;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 通過重寫addInterceptors()方法,可以配置攔截器,對(duì)請求進(jìn)行預(yù)處理或后處理。
registry.addInterceptor(loginRequireInterception)
.excludePathPatterns("/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg");
}
}到此這篇關(guān)于Java Web檢查用戶登錄狀態(tài)(防止用戶訪問到非法頁面)的文章就介紹到這了,更多相關(guān)Java 檢查用戶登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中實(shí)現(xiàn)線程的超時(shí)中斷方法實(shí)例
之前在使用Java實(shí)現(xiàn)熔斷降級(jí)組件的時(shí)候,需要實(shí)現(xiàn)接口請求的超時(shí)中斷,通過查找相關(guān)資料了解了相關(guān)的方法,下面這篇文章主要給大家介紹了關(guān)于Java中實(shí)現(xiàn)線程的超時(shí)中斷的相關(guān)資料,需要的朋友可以參考下2018-06-06
Springboot通過請求頭獲取當(dāng)前用戶信息方法詳細(xì)示范
這篇文章主要介紹了Springboot通過請求頭獲取當(dāng)前用戶信息的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11
Zuul實(shí)現(xiàn)動(dòng)態(tài)路由與權(quán)限過濾器方式
文章介紹如何通過Zuul實(shí)現(xiàn)動(dòng)態(tài)路由與權(quán)限驗(yàn)證,利用自定義過濾器和配置刷新機(jī)制,提升系統(tǒng)靈活性與安全性,確保接口訪問可控且無需重啟2025-07-07
基于spring-security 401 403錯(cuò)誤自定義處理方案
這篇文章主要介紹了基于spring-security 401 403錯(cuò)誤自定義處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
使用springboot通過spi機(jī)制加載mysql驅(qū)動(dòng)的過程
這篇文章主要介紹了使用springboot通過spi機(jī)制加載mysql驅(qū)動(dòng)的過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot利用自定義注解實(shí)現(xiàn)隱私數(shù)據(jù)脫敏(加密顯示)的解決方案
這兩天在整改等保測出的問題,里面有一個(gè)“用戶信息泄露”的風(fēng)險(xiǎn)項(xiàng)(就是后臺(tái)系統(tǒng)里用戶的一些隱私數(shù)據(jù)直接明文顯示了),其實(shí)指的就是要做數(shù)據(jù)脫敏,本文給大家介紹了SpringBoot利用自定義注解實(shí)現(xiàn)隱私數(shù)據(jù)脫敏(加密顯示)的解決方案,需要的朋友可以參考下2023-11-11
SpringBoot?快速實(shí)現(xiàn)?api?接口加解密功能
在項(xiàng)目中,為了保證數(shù)據(jù)的安全,我們常常會(huì)對(duì)傳遞的數(shù)據(jù)進(jìn)行加密,Spring?Boot接口加密,可以對(duì)返回值、參數(shù)值通過注解的方式自動(dòng)加解密,這篇文章主要介紹了SpringBoot?快速實(shí)現(xiàn)?api?接口加解密功能,感興趣的朋友一起看看吧2023-10-10

