Autowired 字段在扩展 GenericFilterBean 的过滤器中为 null
Autowired field is null in filter that extends GenericFilterBean
我正在尝试 autowire service 到我的请求过滤器。它给了我一个空指针异常,我不太清楚为什么。我在我的 控制器 中使用了完全相同的 autowire class 并且它按预期工作!
@Component(value="jwtFilter")
public class JwtFilter extends GenericFilterBean {
RedisClient redisClient = new RedisClient();
@Autowired
JwtTokenService jwtServ; // Is null for some 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); // The 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 from jwt");
JwtToken foundT = jwtServ.findByUser(claims.getSubject()); // Fails here because of jwtServ being null
System.out.println (foundT + " <~~ Found JWT from redis");
}
catch (final SignatureException e) {
throw new ServletException("Invalid token.");
}
chain.doFilter(req, res);
}
我错过了什么吗?我不确定为什么它会在我的 spring 控制器 而不是 过滤器?
这是我的主要 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");
}
由于您手动实例化JwtFilter
registrationBean.setFilter(new JwtFilter());
Spring 无法管理它,因此不会注入 jwtServ
。
要修复它,将 jwtFilter
注入 UserServiceApplication
并将 jwtFilter
注入 registrationBean
.
@SpringBootApplication
public class UserServiceApplication {
@Autowired
private JwtFilter jwtFilter;
@Bean
public FilterRegistrationBean jwtFilter() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(jwtFilter);
...
}
...
我正在尝试 autowire service 到我的请求过滤器。它给了我一个空指针异常,我不太清楚为什么。我在我的 控制器 中使用了完全相同的 autowire class 并且它按预期工作!
@Component(value="jwtFilter")
public class JwtFilter extends GenericFilterBean {
RedisClient redisClient = new RedisClient();
@Autowired
JwtTokenService jwtServ; // Is null for some 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); // The 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 from jwt");
JwtToken foundT = jwtServ.findByUser(claims.getSubject()); // Fails here because of jwtServ being null
System.out.println (foundT + " <~~ Found JWT from redis");
}
catch (final SignatureException e) {
throw new ServletException("Invalid token.");
}
chain.doFilter(req, res);
}
我错过了什么吗?我不确定为什么它会在我的 spring 控制器 而不是 过滤器?
这是我的主要 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");
}
由于您手动实例化JwtFilter
registrationBean.setFilter(new JwtFilter());
Spring 无法管理它,因此不会注入 jwtServ
。
要修复它,将 jwtFilter
注入 UserServiceApplication
并将 jwtFilter
注入 registrationBean
.
@SpringBootApplication
public class UserServiceApplication {
@Autowired
private JwtFilter jwtFilter;
@Bean
public FilterRegistrationBean jwtFilter() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(jwtFilter);
...
}
...