๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽจ Projects/์—๋Ÿฌ๋ชจ์Œ

[์—๋Ÿฌ๋ชจ์Œ] com.auth0.jwt.exceptions.AlgorithmMismatchException: The provided Algorithm doesn't match the one defined in the JWT's Header

by HelloRabbit 2023. 3. 21.
728x90

์—๋Ÿฌ ์„ค๋ช…

์ž๋ฐ” ํ”„๋กœ์ ํŠธ์—์„œ ๋กœ๊ทธ์ธ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‹œํ๋ฆฌํ‹ฐ ํ•„ํ„ฐ๋ฅผ ํ†ตํ•ด JWT ํ† ํฐ์„ ์ƒ์„ฑํ•˜๊ณ  ํ™•์ธํ•˜๋Š” ๊ณผ์ •์—์„œ ์ƒ๊ธด ์—๋Ÿฌ

์—๋Ÿฌ ๋ฉ”์„ธ์ง€

 

JwtAuthenticationFilter.java

@RequiredArgsConstructor
public class JwtAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
	private final AuthenticationManager authenticationManager;
	
	// /login ์š”์ฒญ์„ ํ•˜๋ฉด ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์ธ์ฆํ•˜๊ธฐ ์œ„ํ•ด ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜
	@Override
	public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
			throws AuthenticationException {
		...์ฝ”๋“œ ์ƒ๋žต...
	}
	
	// attemptAuthentication()์—์„œ ์ธ์ฆ์ด ๋๋‚˜๋ฉด ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜
	@Override
	protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
			Authentication authResult) throws IOException, ServletException {
		
		PrincipalDetails principalDetails = (PrincipalDetails) authResult.getPrincipal();
		
		String jwtToken = JWT.create()
        				.withSubject(principalDetails.getUsername())
                    			.withExpiresAt(new Date(System.currentTimeMillis() + (JwtProperties.EXPIRATION_TIME)))
                            		.withClaim("id", principalDetails.getUser().getId())
                            		.withClaim("username", principalDetails.getUser().getUsername())
                            		.sign(Algorithm.HMAC256(JwtProperties.SECRET));
		
		response.addHeader(JwtProperties.HEADER_STRING, JwtProperties.TOKEN_PREFIX + jwtToken);
	}
}

 

JwtAuthorizationFilter.java

public class JwtAuthorizationFilter extends BasicAuthenticationFilter {
	
	private UserRepository userRepository;
	
	public JwtAuthorizationFilter(AuthenticationManager authenticationManager, UserRepository userRepository) {
		super(authenticationManager);
		this.userRepository = userRepository;
	}
	
	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		
		String jwtHeader = request.getHeader(JwtProperties.HEADER_STRING);
		
		if(jwtHeader == null || !jwtHeader.startsWith(JwtProperties.TOKEN_PREFIX)) {
			chain.doFilter(request, response);
			return;
		}
		
		String jwtToken = request.getHeader(JwtProperties.HEADER_STRING)
								 .replace(JwtProperties.TOKEN_PREFIX, "");
		
		// ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์‹œ์ 
		String username = JWT.require(Algorithm.HMAC512(JwtProperties.SECRET))
				   .build()
				   .verify(jwtToken)
				   .getClaim("username").asString();
		
		...์ฝ”๋“œ ์ƒ๋žต...
	}
}

 

 

ํ•ด๊ฒฐ

๊ฒฐ๋ก ์€ ๋งค์šฐ ๊ฐ„๋‹จํ–ˆ๋‹ค.

 

JWT ํ† ํฐ์„ ์ฒ˜์Œ ์ƒ์„ฑํ•˜๊ณ (JwtAuthenticationFilter.java) ๊ฒ€์ฆํ•˜๋Š”(JwtAuthorizationFilter.java) ๋‘ ๊ณผ์ •์—์„œ JWT ํ† ํฐ ์„œ๋ช…์— ์ ‘๊ทผํ•  ๋•Œ ๊ฐ™์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค. 

 

์œ„์— JwtAuthenticationFilter.java ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด Algorithm.HMAC512๋ฅผ ์ด์šฉํ•ด์•ผ ํ•˜๋Š”๋ฐ ์‹ค์ˆ˜๋กœ ๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜(Algorithm.HMAC256)์„ ๋ถˆ๋Ÿฌ์™€ ํ† ํฐ ์„œ๋ช…ํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ์„œ๋ช…์€ HMAC256์œผ๋กœ ๋˜์–ด ์žˆ๋Š”๋ฐ ํ† ํฐ ๊ฒ€์ฆ ๊ณผ์ •์—์„œ๋Š” HMAC512๋กœ ํ™•์ธํ•˜๋‹ˆ๊นŒ ์ž˜๋ชป๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ผ๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

 

๋. :)

 

 

 

๋Œ“๊ธ€