最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java Web檢查用戶登錄狀態(tài)(防止用戶訪問到非法頁面)

 更新時(shí)間:2023年09月01日 16:27:13   作者:Katniss的名字被占用  
一般javaweb網(wǎng)站都有用戶登錄,而有一些操作必須用戶登錄才能進(jìn)行,本文主要介紹了Java Web檢查用戶登錄狀態(tài),具有一定的參考價(jià)值,感興趣的可以了解一下

使用攔截器

  • 在方法前標(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)線程的超時(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
  • Java線上問題排查過程

    Java線上問題排查過程

    本文詳細(xì)介紹了如何通過命令行和可視化工具來診斷和解決CPU和內(nèi)存使用率過高的問題,包括找到高占用率的進(jìn)程和線程,分析堆內(nèi)存使用情況,以及在內(nèi)存溢出時(shí)自動(dòng)保存堆快照文件的方法
    2026-03-03
  • Springboot通過請求頭獲取當(dāng)前用戶信息方法詳細(xì)示范

    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)限過濾器方式

    文章介紹如何通過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ò)誤自定義處理方案

    這篇文章主要介紹了基于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)的過程

    這篇文章主要介紹了使用springboot通過spi機(jī)制加載mysql驅(qū)動(dòng)的過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Springboot事務(wù)失效的幾種情況解讀

    Springboot事務(wù)失效的幾種情況解讀

    這篇文章主要介紹了Springboot事務(wù)失效的幾種情況解讀,因?yàn)镾pring AOP默認(rèn)使用動(dòng)態(tài)代理,會(huì)給被代理的類生成一個(gè)代理類,事務(wù)相關(guān)的操作都通過代理來完成,使用內(nèi)部方法調(diào)用時(shí),使用的是實(shí)例調(diào)用,沒有通過代理類調(diào)用方法,因此事務(wù)不會(huì)檢測到失敗,需要的朋友可以參考下
    2023-10-10
  • SpringBoot利用自定義注解實(shí)現(xiàn)隱私數(shù)據(jù)脫敏(加密顯示)的解決方案

    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?接口加解密功能

    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
  • java實(shí)現(xiàn)順時(shí)針打印矩陣

    java實(shí)現(xiàn)順時(shí)針打印矩陣

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)順時(shí)針打印矩陣的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03

最新評(píng)論

紫金县| 闵行区| 睢宁县| 清原| 高雄县| 休宁县| 泗洪县| 厦门市| 汉寿县| 周口市| 鸡东县| 隆回县| 东山县| 上林县| 化州市| 清原| 大城县| 麦盖提县| 松阳县| 惠安县| 瑞安市| 汕头市| 乌鲁木齐市| 琼海市| 楚雄市| 紫云| 拜泉县| 岳阳县| 阿拉善盟| 那曲县| 西安市| 和静县| 峨边| 沂源县| 缙云县| 青浦区| 青铜峡市| 普兰店市| 泽库县| 水城县| 咸宁市|