简体   繁体   中英

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.

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.

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 . To take an example from 'Securing Spring Data REST PreAuthorize' , you could have a CrudRepository like:

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM