java - Autowired field is null in filter that extends GenericFilterBean -


this question has answer here:

i'm trying autowire service request filter. gives me null pointer exception , i'm not quite sure why. i've used exact same autowire class in controller , works expected!

@component(value="jwtfilter") public class jwtfilter extends genericfilterbean {     redisclient redisclient = new redisclient();      @autowired     jwttokenservice jwtserv; // null reason??!!      @override     public void dofilter(final servletrequest req,                          final servletresponse res,                          final filterchain chain) throws ioexception, servletexception { //        springbeanautowiringsupport.processinjectionbasedoncurrentcontext(this); // doesnt fix issue         system.out.println("\nmaking request...");         final httpservletrequest request = (httpservletrequest) req;           final string authheader = request.getheader("authorization");         if (authheader == null || !authheader.startswith("bearer ")) {             throw new servletexception("missing or invalid authorization header.");         }          final string token = authheader.substring(7); // part after "bearer "          try {             final claims claims = jwts.parser().setsigningkey("${userapi.secret}")                     .parseclaimsjws(token).getbody();             request.setattribute("claims", claims);             request.setattribute("token", token);             system.out.println(claims.getsubject() + " <~~ subject jwt");             jwttoken foundt = jwtserv.findbyuser(claims.getsubject()); // fails here because of jwtserv being null             system.out.println (foundt + " <~~ found jwt redis");          }         catch (final signatureexception e) {             throw new servletexception("invalid token.");         }           chain.dofilter(req, res);     } 

is there i've missed out on? i'm not sure why it'd work in spring controller , not filter?

here main class @beans:

@springbootapplication public class userserviceapplication { @bean     public filterregistrationbean jwtfilter() {         filterregistrationbean registrationbean = new filterregistrationbean();         registrationbean.setfilter(new jwtfilter());         registrationbean.addurlpatterns("/api/v1/*");         registrationbean.addurlpatterns("/user/api/v1/*");         return registrationbean;     }      private @value("${redis.host}") string redishost;     private @value("${redis.port}") int redisport;      public static void main(string[] args) {         springapplication.run(userserviceapplication.class, args);     }      @bean     public hibernatejpasessionfactorybean sessionfactory() {         return new hibernatejpasessionfactorybean();     }       @bean     jedisconnectionfactory jedisconnectionfactory() {         jedisconnectionfactory jedisconfactory = new jedisconnectionfactory();         jedisconfactory.sethostname("localhost");         jedisconfactory.setport(6379);         return jedisconfactory;     }      @bean     public redistemplate<string, object> redistemplate() {         redistemplate<string, object> template = new redistemplate<string, object>();         template.setconnectionfactory(jedisconnectionfactory());         return template;     }        @bean     channeltopic topic() {         return new channeltopic("pubsub:queue");     } 

since manually instantiate jwtfilter

registrationbean.setfilter(new jwtfilter()); 

spring doesn't manage therefore doesn't inject jwtserv.

fix inject jwtfilter userserviceapplication , set injected jwtfilter registrationbean.

@springbootapplication public class userserviceapplication {      @autowired     private jwtfilter jwtfilter;      @bean     public filterregistrationbean jwtfilter() {         filterregistrationbean registrationbean = new filterregistrationbean();         registrationbean.setfilter(jwtfilter);         ...     }     ... 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -