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?