利用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.mzph.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&#…