I added Spring Security on a Rest API with JWT authentication. Now I need to retrieve some data from the token in every controller method - be it either the username or other information.
Since almost all of my controller methods would need a Principal
variable, is there a way to avoid declaring it as an argument to each method?
I once used ObjectProvider
to do a similar thing, like:
@RequestScope
@Component
public class MyObj // ...
Usage:
@Component
public class OtherObj {
@Autowired
private ObjectProvider<MyObj> provider;
// ...
@Override
public boolean meth() throws Exception {
MyObj o = provider.getIfAvailable();
// ...
But there I found that if no instance exists, it is created instead of being returned null
or an exception being thrown.