Skip to content
Advertisement

How to solve the @Bean required error while implementing JWT token

I am trying to do an authentication web API using spring boot. I have used spring security and JWT token methods to build this API. But at some Extent I get this error:

Field jwtUtils in com.example.demo.services.JwtFilterRequest required a bean of type 'com.example.demo.utils.JwtUtils' 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 'com.example.demo.utils.JwtUtils' in your configuration.

Here is my JwtFilterRequest file:

package com.example.demo.services;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import com.example.demo.utils.JwtUtils;

@Component
public class JwtFilterRequest extends OncePerRequestFilter {
    
    @Autowired
    private JwtUtils jwtUtils;
    
    @Autowired
    private UserService userService;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        String authorizationHeader=request.getHeader("Authorization");
        String username=null;
        String jwtToken=null;
        if(authorizationHeader != null && authorizationHeader.startsWith("Bearer")) {
            jwtToken = authorizationHeader.substring(7);
            username=jwtUtils.extractUsername(jwtToken);
        }
        if(username != null && SecurityContextHolder.getContext().getAuthentication()==null) {
            UserDetails currentUserDetails=userService.loadUserByUsername(username);
            Boolean tokenValidated=jwtUtils.validateToken( jwtToken,currentUserDetails);
            if(tokenValidated) {
                 UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken=new UsernamePasswordAuthenticationToken (currentUserDetails,null);
                 usernamePasswordAuthenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                 SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
            }
        }
        filterChain.doFilter(request, response);
    }

}

Here is my JwtUtils file:

package com.example.demo.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.security.core.userdetails.UserDetails;

import com.mongodb.Function;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

public class JwtUtils {
    
    private static final String Secret_Key="dsfbhzksbnvrjaehglxkj";
    
    public String generateToken(UserDetails userdetails) {
        Map<String,Object>claims=new HashMap<>();
        return createToken(claims,userdetails.getUsername());
    }
    
    public Boolean validateToken(String token, UserDetails userdetails) {
        String userName=extractUsername(token);
        return userName.equals(userdetails.getUsername()) && !isTokenExpired(token);
        
    }
    
    private String createToken(Map<String, Object>claims,String subject) {
        
        Date now=new Date(System.currentTimeMillis());
        Date util=new Date(System.currentTimeMillis()+1000*60*60*10);
        return Jwts.builder().setClaims(claims).setSubject(subject).setIssuedAt(now).setExpiration(util).signWith(SignatureAlgorithm.HS256, Secret_Key).compact() ;
        
    }
    public <T> T extractClaim(String token, Function <Claims,T> claimsResolver) {
        final Claims claims = extractAllClaims(token);
        return claimsResolver.apply(claims);
    }
    private Claims extractAllClaims(String token) {
        return Jwts.parser().setSigningKey(Secret_Key).parseClaimsJws(token).getBody();
    }
    public String extractUsername(String token) {
        return extractClaim(token,Claims::getSubject);
    }
    public Date extractExpiration (String token) {
        return extractClaim(token,Claims::getExpiration);       
    }
    
    public Boolean isTokenExpired(String token) {
        return extractExpiration(token).before(new Date());
    }
    

}

I tried adding @Bean annotation at the area and it throwed an error again that it is not allowed there.

Advertisement

Answer

Add @Component annotation to JwtUtils so that Spring creates its bean at runtime which can be injected in JwtFilterRequest.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement