0

Consider kotlin controller class:

@RestController
@RequestMapping("/myPath/")
open class MyController {
    private val s3AsyncClient: S3AsyncClient = S3AsyncClient.builder().build()
    //...
    @PostMapping("/indexing")
    @Secured("ROLE_USER")
    fun someFunction() {
        return s3AsyncClient.toString();
    }
}

Which leads to NullPointerException.

Here is what I saws in debugger:

enter image description here

But when @Secured is removed everything works. So it seems that spring security proxing breaks kotlin val initialization. is there a way to make them works all together?

Cherry
  • 31,309
  • 66
  • 224
  • 364

1 Answers1

0

It turns out that by default kotlin mark all methods as final. So the methods can not be overrided and that's why it is called from object itself, not from proxies. So to make proxying work just add open key word in method definition:

@PostMapping("/indexing")
@Secured("ROLE_USER")
/* -> */ open fun someFunction() {
    return s3AsyncClient.toString();
}
Cherry
  • 31,309
  • 66
  • 224
  • 364