简体   繁体   English

在 SpringDataRest 中,如何在收到 http 请求时添加自定义逻辑

[英]In SpringDataRest, How can i add custom logic when http request received

As we know, In SpringDataRest, the Repository files are only used (not controllers) and we can use build-in methods for It.众所周知,在 SpringDataRest 中,仅使用 Repository 文件(而不是控制器),我们可以为其使用内置方法。

My repository code is:我的存储库代码是:

public interface StudentRepository extends JpaRepository<Student, Integer> {
}

I don't want to add custom methods and add my request processing logic there.我不想添加自定义方法并在那里添加我的请求处理逻辑。 I want some configuration or events overriding where i can process the HttpRequest handler, parse the token and check some data in token and based on that token i'll decide to either process that request or discard it with some error.我想要一些配置或事件覆盖我可以处理 HttpRequest 处理程序的位置,解析令牌并检查令牌中的一些数据,并基于该令牌我将决定处理该请求或丢弃它并出现一些错误。

Thanks.谢谢。

If you want to restrict access to specific endpoints or operations with spring data rest and you are using spring security then you can use the @PreAuthorize annotation with hasRole .如果您想使用 spring 数据休息来限制对特定端点或操作的访问,并且您正在使用 spring 安全性,那么您可以将@PreAuthorize 注释hasRole一起hasRole To take an example from 'Securing Spring Data REST PreAuthorize' , you could have a CrudRepository like:'Securing Spring Data REST PreAuthorize' 为例,您可以拥有一个 CrudRepository,如:

@PreAuthorize("hasRole('ROLE_USER')")
public interface ParkrunCourseRepository extends CrudRepository<ParkrunCourse, Long> {
    @Override
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    ParkrunCourse save(ParkrunCourse parkrunCourse);
}

Then only users with the admin role will be able to do posts to save these entities.然后只有具有管理员角色的用户才能发布帖子以保存这些实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在HTTP状态代码后添加自定义消息 - How can I add custom message after HTTP status code Spring SAML - 如何在SP HTTP请求中添加自定义字段? - Spring SAML - how to add custom fields on the SP HTTP request? 我如何在HTTP请求中发送对象 - How can i send object in HTTP request 如何发出以Enum作为请求参数的Jmeter HTTP请求? - How can I make a Jmeter HTTP request with an Enum as Request Parameter? Spring 安全性 (OAuth):如何在未公开 restTemplate 时添加请求 header? - Spring Security (OAuth): How can I add a request header when restTemplate is not exposed? Spring Cloud配置刷新后如何执行自定义逻辑? - How can I execute custom logic after Spring cloud configuration refresh? Spring Boot Actuator - 如何向/ shutdown端点添加自定义逻辑 - Spring Boot Actuator - how to add custom logic to /shutdown endpoint 将我的自定义 http 标头添加到 Spring RestTemplate 请求/扩展 RestTemplate - Add my custom http header to Spring RestTemplate request / extend RestTemplate 如果http请求的内容类型为urlencoded,如何让我的spring启动控制器读取我对某个对象的http请求的主体? - How can I get my spring boot controller to read the body of my http request to an object if the http request has content type urlencoded? 如何为@Column(nullable = false) 添加自定义消息? - How can I add a custom message for @Column(nullable = false)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM