ali-bouali / spring-boot-3-jwt-security

Sample project on how to implement JWT security based using Spring boot 3 and Spring security 6

Home Page:https://aliboucoding.com/p/securing-your-spring-boot-3-0-applications-with-json-web-tokens-jwt

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Factory method 'securityFilterChain' threw exception with message

SukeshU opened this issue · comments

When I try to start the application, I am getting this issue. Can you please help me?

Error: - Factory method 'securityFilterChain' threw exception with message: Cannot invoke "Object.getClass()" because "filter" is null

@component
@requiredargsconstructor
public class JwtAuthenticationFilter extends OncePerRequestFilter {

private final JwtService jwtService = null;
private final UserDetailsService userDetailsService = null;

@OverRide
protected void doFilterInternal(
@nonnull HttpServletRequest request,
@nonnull HttpServletResponse response,
@nonnull FilterChain filterChain
) throws ServletException, IOException {
final String authHeader = request.getHeader("Authorization");
final String jwt;
final String userEmail;
if (authHeader == null ||!authHeader.startsWith("Bearer ")) {
filterChain.doFilter(request, response);
return;
}
jwt = authHeader.substring(7);
userEmail = jwtService.extractUsername(jwt);
if (userEmail != null && SecurityContextHolder.getContext().getAuthentication() == null) {
UserDetails userDetails = this.userDetailsService.loadUserByUsername(userEmail);
if (jwtService.isTokenValid(jwt, userDetails)) {
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
userDetails,
null,
userDetails.getAuthorities()
);
authToken.setDetails(
new WebAuthenticationDetailsSource().buildDetails(request)
);
SecurityContextHolder.getContext().setAuthentication(authToken);
}
}
filterChain.doFilter(request, response);
}
}

I got the solution. The issue was I initialized the final keyword as "=null", which is not required.