spring rest API 的所有查询的自定义验证

     2023-02-27     90

关键词:

【中文标题】spring rest API 的所有查询的自定义验证【英文标题】:Custom validation for all the query of spring rest API 【发布时间】:2020-02-21 19:15:03 【问题描述】:

我正在使用 Spring Rest API,并且我想在进入 rest api 查询之前应用自定义验证(更具体地说,我想检查用户是否经过身份验证,因此我还需要 HttpServletRequest 对象)。

例如,我有 3 个 API。 1.RestAPI/test1 2.RestAPI/test2 3.RestAPI/test3

所以在进行查询之前,我想检查用户是否经过身份验证。

我可以使用ConstraintValidator吗?

我怎样才能做到这一点?

我没有使用弹簧靴...

谢谢!

【问题讨论】:

【参考方案1】:

有以下几种方法:

1) Spring 安全@PreAuthorize("isAuthenticated()")@Secured("ROLE_ADMIN")

查看thread了解更多信息

2)您可以创建自定义注释,添加带有SecurityContextHolder的方面:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Authenticated 


import org.aspectj.lang.annotation.Aspect;
@Aspect
@Component
public class AuthenticatedAspect 

    @Around("@annotation(Authenticated)")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable 
         if (!SecurityContextHolder.getContext().getAuthentication().isAuthenticated()) 
             throw your exeption
         
         return joinPoint.proceed();
    

对于第二种方法,您可能需要添加proxy-target-class="true" <aop:aspectj-autoproxy proxy-target-class="true"/>

【讨论】:

在具有多个连接的 Spring Boot Rest API 中公开自定义查询

】在具有多个连接的SpringBootRestAPI中公开自定义查询【英文标题】:ExposecustomqueryinSpringBootRestAPIwithmultiplejoins【发布时间】:2020-12-2921:11:45【问题描述】:我有一个SpringRESTApi和一个MySQL数据库,现在我想公开具有多个连接的自定... 查看详情

Spring Boot REST API 版本控制的自定义标头方法

】SpringBootRESTAPI版本控制的自定义标头方法【英文标题】:CustomHeaderApproachforSpringBootRESTAPIversioning【发布时间】:2020-04-2905:47:02【问题描述】:我希望实现使用自定义标头使用SpringBoot和Swagger对RESTAPI进行版本控制。我浏览了许多... 查看详情

如何处理 grails spring-security-rest 插件中的自定义身份验证异常?

】如何处理grailsspring-security-rest插件中的自定义身份验证异常?【英文标题】:HowdoIhandleacustomauthenticationexceptioningrailsspring-security-restplugin?【发布时间】:2017-03-0620:24:19【问题描述】:我正在使用带有spring-security-rest插件的Grails。... 查看详情

无法显示来自 Wordpress REST API 自定义端点的自定义字段

】无法显示来自WordpressRESTAPI自定义端点的自定义字段【英文标题】:Can\'tdisplaycustomfieldfromWordpressRESTAPIcustomendpoint【发布时间】:2020-09-0113:46:21【问题描述】:我正在使用WordpressRestAPI将内容从Wordpress网站导入PHP应用程序。这并不... 查看详情

如何让 Laravel 返回 JSON REST API 的自定义错误

】如何让Laravel返回JSONRESTAPI的自定义错误【英文标题】:HowcanImakeLaravelreturnacustomerrorforaJSONRESTAPI【发布时间】:2014-04-2007:21:20【问题描述】:我正在开发某种RESTfulAPI。当发生一些错误时,我会抛出一个App::abort($code,$message)错误。... 查看详情

使用 Keycloak 中的自定义 Admin REST API 获取用户的 ID 令牌

】使用Keycloak中的自定义AdminRESTAPI获取用户的ID令牌【英文标题】:GetIDtokenofauserusingcustomAdminRESTAPIinKeycloak【发布时间】:2021-01-2007:50:43【问题描述】:在我现在正在开发的系统中,我想使用Keycloak的adminRESTAPI在我的Keycloak服务器... 查看详情

使用 Spring Data Rest 时具有注入值的自定义逻辑

】使用SpringDataRest时具有注入值的自定义逻辑【英文标题】:CustomLogicwithInjectedValueswhenusingSpringDataRest【发布时间】:2015-04-1808:54:19【问题描述】:我想转换现有的Web服务以利用spring-data-rest。如何在SpringDataRest之上使用注入值(特... 查看详情

列出所有已部署的 REST 端点(spring-boot、jersey)

】列出所有已部署的REST端点(spring-boot、jersey)【英文标题】:Listingalldeployedrestendpoints(spring-boot,jersey)【发布时间】:2015-12-0803:31:13【问题描述】:是否可以使用springboot列出我所有配置的rest-endpoints?执行器在启动时列出了所有... 查看详情

ejabberd - 基于外部 REST API 的自定义花名册模块

】ejabberd-基于外部RESTAPI的自定义花名册模块【英文标题】:ejabberd-CustomrostermodulebasedonexternalRESTAPI【发布时间】:2020-08-1222:23:47【问题描述】:我正在浏览https://docs.ejabberd.im/developer/extending-ejabberd/architecture/,它讨论了通过RESTAPI... 查看详情

您将如何使用 Spring Boot 处理仅具有可选查询参数的 REST API?

】您将如何使用SpringBoot处理仅具有可选查询参数的RESTAPI?【英文标题】:HowwouldyouhandleaRESTAPIwithonlyoptionalqueryparamswithSpringBoot?【发布时间】:2020-06-0423:58:40【问题描述】:我想构建一个返回Order对象的简单端点,我可以在其中通... 查看详情

带有 RepositoryRestResource-s 和常规控制器的 Spring REST HATEOAS 中的根请求的自定义响应

】带有RepositoryRestResource-s和常规控制器的SpringRESTHATEOAS中的根请求的自定义响应【英文标题】:CustomresponseforrootrequestinttheSpringRESTHATEOASwithbothRepositoryRestResource-sandregularcontrollers【发布时间】:2014-11-0504:03:12【问题描述】:假设我... 查看详情

@RepositoryRestResource 中的自定义实现

...1【问题描述】:我正在开发一个使用@RepositoryRestResource的spring-boot项目。有2个实体,Product和Domain,它们具有多对多关系。我想实现一个运行复杂查询的自定义REST调用。为了实现 查看详情

用于包装外部 REST API 的自定义 grafana 数据源插件

】用于包装外部RESTAPI的自定义grafana数据源插件【英文标题】:CustomgrafanadatasourceplugintowrapexternalRESTAPI【发布时间】:2017-01-2905:17:34【问题描述】:我正在尝试找到一种方法来创建一个数据源插件,该插件可以与外部RESTAPI通信并... 查看详情

使用 Spring Data MongoRepository 进行更新查询的自定义方法

】使用SpringDataMongoRepository进行更新查询的自定义方法【英文标题】:CustommethodforupdatequerywithspringdataMongoRepository【发布时间】:2018-10-0519:10:42【问题描述】:我正在使用org.springframework.data.mongodb.repository.MongoRepository。我写了一些... 查看详情

自定义查询 Spring Data JPA + REST

】自定义查询SpringDataJPA+REST【英文标题】:CustomquerySpringDataJPA+REST【发布时间】:2015-03-2522:41:29【问题描述】:在我的数据库中,我有一个表“CITA”,其中包含以下属性:id、fecha_hora、descripcion、id_empleado、id_cliente。我也有一个S... 查看详情

Django Rest 框架中的自定义用户模型注册

】DjangoRest框架中的自定义用户模型注册【英文标题】:CustomUserModelRegistrationInDjangoRestFramework【发布时间】:2020-08-0920:00:16【问题描述】:我是djangorest框架的新手。我有我用(models.Model)创建的用户模型,现在我想为这个模型中的... 查看详情

用于在 Spring Data Jpa 中从多个表中获取数据的自定义查询

】用于在SpringDataJpa中从多个表中获取数据的自定义查询【英文标题】:CustomQueryforfetchingdatafrommultipletablesinspringDataJpa【发布时间】:2017-08-1200:18:15【问题描述】:实体正在关注产品表@EntitypublicclassProductimplementsSerializable/*@Id@Generat... 查看详情

带有分页的 Spring JPA 存储库中的自定义查询

】带有分页的SpringJPA存储库中的自定义查询【英文标题】:CustomQueryinSpringJPARepositorywithPagination【发布时间】:2015-10-2916:53:32【问题描述】:我已经尝试使用SpringBoot实现JPA存储库,它工作正常。现在,如果我尝试在使用@QueryAnnotat... 查看详情