0

I have a Jersey authorization filter intended to get executed before an API call. But spring beans are not getting initialised, thus throwing NullPointerException.

@Provider
@Autherization
public class AuthorizationFilter implements ContainerRequestFilter, ContainerResponseFilter {

    @Autowired
    private EntityDAO<SessionDO> sessionDAO;

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        String requestSessionId = requestContext.getHeaders().getFirst("SESSION_ID");
        String username = requestContext.getHeaders().getFirst("USERNAME");
        Optional<SessionDO> session = sessionDAO.get(requestSessionId);
        if (session.isEmpty() || session.get().getExpiryTimestamp() < System.currentTimeMillis()) {
            throw new SessionExpiredException("User not logged in or session has expired");
        }
        if (!username.equals(session.get().getStudentDO().getUid())) {
            throw new InvalidUsernameException("Username is didn't match for given session id");
        }
    }

    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {

    }
}

Bean is annotated with @Component as following

@Component
public class SessionDAOImpl extends EntityDAO<SessionDO> {

    @Autowired
    private DynamoDBMapper dynamoDBMapper;

    @Override
    public Optional<SessionDO> get(String id) {
        return Optional.ofNullable(dynamoDBMapper.load(SessionDO.class, id));
    }
}

Why are beans not loaded into filter?

Kuldeep Yadav
  • 1,664
  • 5
  • 23
  • 41
  • Please also show how you instantiate AuthorizationFilter – Nobody Feb 03 '20 at 06:20
  • Is it a problem with just this filter or is the Spring to Jersey injection not working at all? – Paul Samsotha Feb 03 '20 at 06:21
  • Yes it is, I am injecting it like this https://bitbucket.org/snippets/kuldeepiitg/EbGjkG/springapplication https://bitbucket.org/snippets/kuldeepiitg/qnz6jB/authenticationservice – Kuldeep Yadav Feb 03 '20 at 06:22
  • @Nobody filters are automatically scanned by Jersey, I need not/have not to initialise explicitly. – Kuldeep Yadav Feb 03 '20 at 06:23
  • In order to autowire, you need to have the class in which you want to get bean autowired, to be within spring context. To be completely honest, never worked with jersey, but my bet is on the fact that you do need to declare filter as @Component / hook desired dao in a different manner – Nobody Feb 03 '20 at 06:26
  • 2
    What you have linked to, this is not the correct way to integrate Spring with Jersey. What I see you doing is manually retrieving beans from the ApplicationContext yourself. You are not using injection. If you want to see how to integrate Spring injection into a Jersey project, have a look at [this example project](https://github.com/psamsotha/jersey-spring-example) – Paul Samsotha Feb 03 '20 at 06:29
  • @PaulSamsotha I have to do the manual work only at first level i.e. service layer, subsequent layers are automatically injected by @ Autowire and @ Component facility. Nevertheless, I will look into the example you provide. Thank you. – Kuldeep Yadav Feb 03 '20 at 06:34
  • You shouldn't have to do _any_ manual work. Take a look at the project I linked to. Pay attention to the dependencies and the Config classes. – Paul Samsotha Feb 03 '20 at 06:39

0 Answers0