I don’t know why, but my @Autowired
component (jwtService) returns a NullPointerException.
Here is my code:
This is my JwtTokenValidator class:
@Component
@AllArgsConstructor
@NoArgsConstructor
public class JwtTokenValidator extends OncePerRequestFilter {
@Autowired
private JwtService jwtService;
@Override
protected void doFilterInternal(@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response,
@NonNull FilterChain filterChain) throws ServletException, IOException {
final String authHeader = request.getHeader("Authorization");
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
filterChain.doFilter(request, response);
return;
}
String token = authHeader.substring(7);
try {
String email = jwtService.extractEmail(token);
String authorities = jwtService.extractAuthorities(token);
List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList(authorities);
Authentication authentication = new UsernamePasswordAuthenticationToken(email, null, auth);
SecurityContextHolder.getContext().setAuthentication(authentication);
filterChain.doFilter(request, response);
} catch (Exception e) {
SecurityContextHolder.clearContext();
throw e;
}
}
}
and this is my JwtService class:
@Service
public class JwtService {
private SecretKey key = Keys.hmacShaKeyFor(JwtConstant.SECRET_KEY.getBytes());
public String extractEmail(String token) {
return String.valueOf(extractAllClaims(token).get("email"));
}
public String extractAuthorities(String token) {
return String.valueOf(extractAllClaims(token).get("authorities"));
}
public Claims extractAllClaims(String token){
return Jwts.parserBuilder()
.setSigningKey(key)
.build()
.parseClaimsJws(token)
.getBody();
}
public String generateToken(Authentication authentication){
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
String roles = populateAuthorities(authorities);
return Jwts.builder()
.setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() + (1000 * 60 * 60 * 24)))
.claim("email", authentication.getName())
.claim("authorities", roles)
.signWith(key, SignatureAlgorithm.HS256)
.compact();
}
public String populateAuthorities(Collection <? extends GrantedAuthority> authorities){
Set<String> auths = new HashSet<>();
for(GrantedAuthority authority : authorities){
auths.add(authority.getAuthority());
}
return String.join(",",auths);
}
}
I got this error message:
java.lang.NullPointerException: Cannot invoke "com.Abdelouadoud.MoroccoCraftsAPI.config.JwtService.extractEmail(String)" because "this.jwtService" is null
at com.Abdelouadoud.MoroccoCraftsAPI.config.JwtTokenValidator.doFilterInternal(JwtTokenValidator.java:55) ~[classes/:na]
I verified that JwtService is a bean (it contains a @Service annotation).
I also verified that I had never initiated JwtService manually in the other classes.
I really need your help, thank you!
New contributor
abdelouadoud laadimi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.