I’m new to Springboot and i hope you can help me. I’m doing a back application in SpringBoot for a react native app and i’m stuck. I want to secure my app with a jwt but when i try to launch my application i have this message andi don’t understand why i got this.
*************************** APPLICATION FAILED TO START *************************** Description: Field jwtTokenProvider in asrouen.projet2.security.JwtAuthenticationFilter required a bean of type 'asrouen.projet2.security.JwtTokenProvider' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'asrouen.projet2.security.JwtTokenProvider' in your configuration.
JwtTokenProvider Class
@Component public class JwtTokenProvider { private static final Logger LOGGER = LoggerFactory.getLogger(JwtTokenProvider.class); @Value(value = "${app.jwtSecret}") private String jwtSecret; @Value(value = "${app.jwtExpirationInMs}") private int jwtExpirationInMs; public String generateToken(Authentication authentication) { UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal(); Date now = new Date(); Date expiryDate = new Date(now.getTime() + jwtExpirationInMs); return Jwts.builder().setSubject(Long.toString(userPrincipal.getId())).setIssuedAt(new Date()) .setExpiration(expiryDate).signWith(SignatureAlgorithm.HS512, jwtSecret).compact(); } public Long getUserIdFromJWT(String token) { Claims claims = Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token).getBody(); return Long.valueOf(claims.getSubject()); } public boolean validateToken(String authToken) { try { Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(authToken); return true; } catch (SignatureException ex) { LOGGER.error("Invalid JWT signature"); } catch (MalformedJwtException ex) { LOGGER.error("Invalid JWT token"); } catch (ExpiredJwtException ex) { LOGGER.error("Expired JWT token"); } catch (UnsupportedJwtException ex) { LOGGER.error("Unsupported JWT token"); } catch (IllegalArgumentException ex) { LOGGER.error("JWT claims string is empty"); } return false; } }
and JwtAuthenticationFilter Class
public class JwtAuthenticationFilter extends OncePerRequestFilter { private static final Logger LOGGER = LoggerFactory.getLogger(JwtAuthenticationFilter.class); @Autowired private JwtTokenProvider jwtTokenProvider; @Autowired private CustomUserDetailsServiceImpl customUserDetailsService; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { try { String jwt = getJwtFromRequest(request); if (StringUtils.hasText(jwt) && jwtTokenProvider.validateToken(jwt)) { Long userId = jwtTokenProvider.getUserIdFromJWT(jwt); UserDetails userDetails = customUserDetailsService.loadUserById(userId); UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken( userDetails, null, userDetails.getAuthorities()); authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); SecurityContextHolder.getContext().setAuthentication(authenticationToken); } } catch (Exception ex) { LOGGER.error("Could not set user authentication in security context", ex); } filterChain.doFilter(request, response); } private String getJwtFromRequest(HttpServletRequest request) { String bearerToken = request.getHeader("Authorization"); if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) { return bearerToken.substring(7, bearerToken.length()); } return null; } }
I hope to read from you soon
Advertisement
Answer
In which package is your application class? (annotated with @SpringBootApplication)?
Spring boot will normally only search for @Component in the same package or any of its sub-packages.