利用HandlerMethodArgumentResolver和注解解析封裝用戶信息和Http參數

獲取用戶身份信息詳情注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 獲取用戶身份信息詳情注解*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface CurrentUserPrincipal {/*** 是否允許匿名用戶* @return*/boolean Anonymous() default false;
}

獲取HTTP上下文注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 獲取HTTP上下文注解** @author linpx* @date 2022/3/25 下午 6:27*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface CurrentHttpContext {}

接口HandlerMethodArgumentResolver

/** Copyright 2002-2014 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      https://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.springframework.web.method.support;import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;/*** Strategy interface for resolving method parameters into argument values in* the context of a given request.** @author Arjen Poutsma* @since 3.1* @see HandlerMethodReturnValueHandler*/
public interface HandlerMethodArgumentResolver {/*** Whether the given {@linkplain MethodParameter method parameter} is* supported by this resolver.* @param parameter the method parameter to check* @return {@code true} if this resolver supports the supplied parameter;* {@code false} otherwise*/boolean supportsParameter(MethodParameter parameter);/*** Resolves a method parameter into an argument value from a given request.* A {@link ModelAndViewContainer} provides access to the model for the* request. A {@link WebDataBinderFactory} provides a way to create* a {@link WebDataBinder} instance when needed for data binding and* type conversion purposes.* @param parameter the method parameter to resolve. This parameter must* have previously been passed to {@link #supportsParameter} which must* have returned {@code true}.* @param mavContainer the ModelAndViewContainer for the current request* @param webRequest the current request* @param binderFactory a factory for creating {@link WebDataBinder} instances* @return the resolved argument value, or {@code null} if not resolvable* @throws Exception in case of errors with the preparation of argument values*/@NullableObject resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception;}

HTTP上下文參數解析器


import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;import javax.servlet.http.HttpServletRequest;/*** HTTP上下文參數解析器*/
public class HttpContextArgumentResolver implements HandlerMethodArgumentResolver {@Overridepublic boolean supportsParameter(MethodParameter methodParameter) {return HttpContext.class.isAssignableFrom(methodParameter.getParameterType()) &&methodParameter.hasParameterAnnotation(CurrentHttpContext.class);}@Overridepublic Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {return new HttpContext(nativeWebRequest.getNativeRequest(HttpServletRequest.class));}
}

用戶身份信息參數解析器


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;/*** 用戶身份信息參數解析器*/
public class UserPrincipalArgumentResolver implements HandlerMethodArgumentResolver {@Autowiredprivate UserPrincipalResolverService userPrincipalResolverService;@Overridepublic boolean supportsParameter(MethodParameter methodParameter) {return BaseUserDetail.class.isAssignableFrom(methodParameter.getParameterType()) &&methodParameter.hasParameterAnnotation(CurrentUserPrincipal.class);}@Overridepublic Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {CurrentUserPrincipal annotation = methodParameter.getParameterAnnotation(CurrentUserPrincipal.class);return userPrincipalResolverService.resolve(methodParameter.getParameterType(), annotation.Anonymous());}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/80785.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/80785.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/80785.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

OpenCV 圖形API(52)顏色空間轉換-----將 NV12 格式的圖像數據轉換為 RGB 格式的圖像

操作系統:ubuntu22.04 OpenCV版本:OpenCV4.9 IDE:Visual Studio Code 編程語言:C11 算法描述 將圖像從 NV12 (YUV420p) 色彩空間轉換為 RGB。該函數將輸入圖像從 NV12 色彩空間轉換到 RGB。Y、U 和 V 通道值的常規范圍是 0 到 255。 輸出圖…

哈工大李治軍《操作系統》進程同步與信號量筆記

1.什么是信號量? 定義:記錄一些信息(即量),并根據這個信息決定睡眠還是喚醒(即信號)。睡眠和喚醒只是一個信號(相當于0和1)。 2.問題:一種資源的數量是8&am…

MySQL 的索引類型有哪些?

MySQL 中的索引是提高查詢性能的重要工具,它通過構建數據結構來加速數據檢索。MySQL 支持多種索引類型,每種類型適用于不同的場景。以下是 MySQL 中主要的索引類型及其特點: 1. B-Tree 索引(默認類型) 結構&#xff1…

基于Qt5的藍牙打印開發實戰:從掃描到小票打印的全流程

文章目錄 前言一、應用案例演示二、開發環境搭建2.1 硬件準備2.2 軟件配置 三、藍牙通信原理剖析3.1 實現原理3.2 通信流程3.3 流程詳解3.4 關鍵技術點 四、Qt藍牙核心類深度解析4.1 QBluetoothDeviceDiscoveryAgent4.2 QBluetoothDeviceInfo4.3 QBluetoothSocket 五、功能實現…

高可靠性厚銅板制造的關鍵設備與工藝投入

隨著科技的不斷發展,電子設備越來越普及,對電路板的需求也越來越大。厚銅板電路板作為一種高性能、高可靠性的電路板,受到了廣泛的關注和應用。那么,作為一家厚銅板電路板供應商,如何投入線路板生產呢?本文…

【如何使用solidwork編輯結構導入到simscope】

這里寫自定義目錄標題 嘗試將solidrwork的模型導入到matlab中,以下是官方給出的設計步驟,沖啊 To use Simscape Multibody Link, you must install MATLAB and the CAD applications on the same computer. To ensure the successful installation of Si…

Linux 在個人家目錄下添加環境變量 如FLINK_PROPERTIES=“jobmanager.rpc.address: jobmanager“

問題: Docker Flink Application Mode 命令行形式部署前,需要在Linux執行以下: $ FLINK_PROPERTIES"jobmanager.rpc.address: jobmanager" $ docker network create flink-network 臨時變量只在當前session會話窗口生效&#xf…

spring項目rabbitmq es項目啟動命令

應該很多開發者遇到過需要啟動中間件的情況,什么測試服務器掛了,服務連不上nacos了巴拉巴拉的,雖然是測試環境,但也會手忙腳亂,瘋狂百度。 這里介紹一些實用方法 有各種不同的場景,一是重啟,服…

語音合成之七語音克隆技術突破:從VALL-E到SparkTTS,如何解決音色保真與清晰度的矛盾?

從VALL-E到SparkTTS,如何解決音色保真與清晰度的矛盾? 引言語音克隆技術發展史YourTTS:深入剖析架構與技術VALL-E:揭秘神經編解碼語言模型MaskGCTSparkTTS:利用 LLM 實現高效且可控的語音合成特征解耦生成式模型特征解…

run code執行ts配置

1、全局安裝typescript npm install –g typescript 執行tsc –v,可輸出版本號,代表安裝成功 2、創建tsConfig文件 npx tsc –init 創建成功目錄下會出現tsconfig.json文件 3、安裝ts-node,支持執行運行ts文件 npm install –g ts-node 控制…

splitchunk(如何將指定文件從主包拆分為單獨的js文件)

1. 說明 webpack打包會默認將入口文件引入依賴js打包為一個入口文件,導致這個文件會比較大,頁面首次加載時造成加載時間較長 可通過splitchunk配置相應的規則,對匹配的規則打包為單獨的js,減小入口js的體積 2. 示例 通過正則匹配&#xff…

postgres 導出導入(基于數據庫,模式,表)

在 PostgreSQL 中,導出和導入數據庫、模式(schema)或表的數據可以使用多種工具和方法。以下是常用的命令和步驟,分別介紹如何導出和導入整個數據庫、特定的模式以及單個表的數據。 一、導出數據 1. 使用 pg_dump 導出整個數據庫…

第十一天 主菜單/設置界面 過場動畫(Timeline) 成就系統(Steam/本地) 多語言支持

前言 對于剛接觸Unity的新手開發者來說,構建完整的游戲系統往往充滿挑戰。本文將手把手教你實現游戲開發中最常見的四大核心系統:主菜單界面、過場動畫、成就系統和多語言支持。每個模塊都將結合完整代碼示例,使用Unity 2022 LTS版本進行演示…

深入探索Python Pandas:解鎖數據分析的無限可能

放在前頭 深入探索Python Pandas:解鎖數據分析的無限可能 深入探索Python Pandas:解鎖數據分析的無限可能 在當今數據驅動的時代,高效且準確地處理和分析數據成為了各個領域的關鍵需求。而Python作為一門強大且靈活的編程語言,…

小集合 VS 大集合:MySQL 去重計數性能優化

小集合 VS 大集合:MySQL 去重計數性能優化 前言一、場景與問題 🔎二、通俗執行流程對比三、MySQL 執行計劃解析 📊四、性能瓶頸深度剖析 🔍五、終極優化方案 🏆六、總結 前言 📈 測試結果: 在…

3、Linux操作系統下,linux的技術手冊使用(man)

linux系統內置技術手冊,方便開發人員查閱Linux相關指令,提升開發效率 man即是manual的前三個字母,有時候遇事不決,問個人(man) 其在線網址為:man 還有man網站的作者寫的書,可以下…

京東商品詳情數據爬取難度分析與解決方案

在當今數字化商業時代,電商數據對于市場分析、競品研究、價格監控等諸多領域有著不可估量的價值。京東,作為國內首屈一指的電商巨頭,其商品詳情頁蘊含著海量且極具價值的數據,涵蓋商品價格、庫存、規格、用戶評價等關鍵信息。然而…

正確應對監管部門的數據安全審查

首席數據官高鵬律師團隊編著 在當今數字化時代,數據安全已成為企業及各類組織面臨的重要議題,而監管部門的數據安全審查更是關乎其生存與發展的關鍵挑戰。隨著法律法規的不斷完善與監管力度的加強,如何妥善應對這一審查,避免潛在…

三星One UI安全漏洞:剪貼板數據明文存儲且永不過期

三星One UI系統曝出重大安全漏洞,通過剪貼板功能導致數百萬用戶的敏感信息面臨泄露風險。 剪貼板數據永久存儲 安全研究人員發現,運行Android 9及以上系統的三星設備會將所有剪貼板內容——包括密碼、銀行賬戶詳情和個人消息——以明文形式永久存儲&am…

動態規劃求解leetcode300.最長遞增子序列(LIS)詳解

給你一個整數數組 nums ,找到其中最長嚴格遞增子序列的長度。 子序列 是由數組派生而來的序列,刪除(或不刪除)數組中的元素而不改變其余元素的順序。例如,[3,6,2,7] 是數組 [0,3,1,6,2,2,7] 的子序列。 示例 1&#…