spring-projects / spring-projects/spring-data-rest

java.lang.NullPointerException when using ResourceProcessor<RepositoryLinksResource> and SpringSecurity [DATAREST-702]

Open
#1,073 0 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

type: bug
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

Manuel Sousa opened DATAREST-702 and commented

I have a custom controller which implements ResourceProcessor<RepositoryLinksResource> in order to populate the root response as in: http://stackoverflow.com/questions/25783487/custom-response-for-root-request-int-the-spring-rest-hateoas-with-both-repositor/26118405#26118405

If one of the controller methods is annotated with springsecurity Pre/PostAuthorize I get a null pointer exception when booting the application:
...
Caused by: java.lang.NullPointerException
at org.springframework.data.rest.webmvc.ResourceProcessorHandlerMethodReturnValueHandler.<init>(ResourceProcessorHandlerMethodReturnValueHandler.java:89)
at org.springframework.data.rest.webmvc.ResourceProcessorInvokingHandlerAdapter.afterPropertiesSet(ResourceProcessorInvokingHandlerAdapter.java:75)
at org.springframework.data.rest.webmvc.RepositoryRestHandlerAdapter.afterPropertiesSet(RepositoryRestHandlerAdapter.java:60)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
...

I think the issue lies in the processor being Proxy$... instead of the ControllerClass.
Moving the method to another class, for instance the DAO fixes the issue as an workaround.

Not sure if this is a bug, a design choice or if i'm missing something.

@RestController
@ExposesResourceFor(Device.class)
@RequestMapping(value = "/devices", produces = "application/hal+json")
public class DeviceController implements ResourceProcessor<RepositoryLinksResource> {
...
    @RequestMapping(path = "/{id}")
    @PreAuthorize("isAuthenticated()")
    public Resource<Device> getDevice(@PathVariable("id") String id){
    ...
    }

    public RepositoryLinksResource process(RepositoryLinksResource resource) {
        resource.add(ControllerLinkBuilder.linkTo(DeviceController.class).withRel("devices"));
        return resource;
    }
}

Removing the @PreAuthorize or moving it to the dao is enough for it to work as expected


Affects: 2.4 GA (Gosling)

Referenced from: commits https://github.com/spring-projects/spring-data-rest/commit/e08dff846e1589dcae147be6714c0ace97620795

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.