Here, in the SecurityConfig.java , jwt Encoder и JWT Декодер создается.
Код: Выделить всё
@Bean
public JwtEncoder jwtEncoder() {
return new NimbusJwtEncoder(new ImmutableSecret(jwtKey.getBytes()));
}
//Decoder
@Bean
public JwtDecoder jwtDecoder() {
byte[] bytes = jwtKey.getBytes();
SecretKeySpec originalKey = new SecretKeySpec(bytes, 0, bytes.length, "RSA");
logger.info("Original Key: {}", originalKey.getEncoded());
return NimbusJwtDecoder.withSecretKey(originalKey).macAlgorithm(MacAlgorithm.HS512).build();
}
Код: Выделить всё
package com.ayushsingh.jwtsymmetric_keydemo.service;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.jose.jws.MacAlgorithm;
import org.springframework.security.oauth2.jwt.JwsHeader;
import org.springframework.security.oauth2.jwt.JwtClaimsSet;
import org.springframework.security.oauth2.jwt.JwtEncoder;
import org.springframework.security.oauth2.jwt.JwtEncoderParameters;
import org.springframework.stereotype.Service;
@Service
public class TokenService {
@Autowired
private JwtEncoder jwtEncoder;
// Method to generate the token
public String generateToken(Authentication authentication) {
Instant now = Instant.now();
String scope = authentication.getAuthorities().stream()
.map(GrantedAuthority::getAuthority)
.filter(authority -> !authority.startsWith("ROLE"))
.collect(Collectors.joining(" "));
JwtClaimsSet claims = JwtClaimsSet.builder()
.issuer("self")
.issuedAt(now)
.expiresAt(now.plus(1, ChronoUnit.HOURS))
.subject(authentication.getName())
.claim("scope", scope)
.build();
var encodedParameters = JwtEncoderParameters.from(JwsHeader.with(MacAlgorithm.HS512).build(), claims);
return this.jwtEncoder.encode(encodedParameters).getTokenValue();
}
}
< /code>
[b] мои запросы-< /strong>
Я понимаю, что мы используем тот же симметричный ключ для шифрования здесь, как это объяснено в видео и здесь- https://www.pingidentity.com/en/resources/blog/post/jwt-security-nobody-talks-about.html#:~:text=using%20Asymmetric%20Signatures.-, асимметричный%20JWT%20Signatures, ревер %20 с%20thet%20public%20key.
Но я не могу понять, почему нам требуется secretkeyspec [/b] в [b] декодере-[/b]
@Bean
public JwtDecoder jwtDecoder() {
byte[] bytes = jwtKey.getBytes();
SecretKeySpec originalKey = new SecretKeySpec(bytes, 0, bytes.length, "RSA");
logger.info("Original Key: {}", originalKey.getEncoded());
return NimbusJwtDecoder.withSecretKey(originalKey).macAlgorithm(MacAlgorithm.HS512).build();
}
< /code>
Что именно делает SecretKeySpec и почему мы используем RSA здесь? Здесь, [b] jwtkey [/b] 9FAA372517AC1D389758D3750FC07ACF00F542277F26FEC1CE4593E93F64E338. Вывод для < /p>
logger.info("Original Key: {}", originalKey.getEncoded());
< /code>
is-< /p>
Original Key: [57, 102, 97, 97, 51, 55, 50, 53, 49, 55, 97, 99, 49, 100, 51, 56, 57, 55, 53, 56, 100, 51, 55, 53, 48, 102, 99, 48, 55, 97, 99, 102, 48, 48, 102, 53, 52, 50, 50, 55, 55, 102, 50, 54, 102, 101, 99, 49, 99, 101, 52, 53, 57, 51, 101, 57, 51, 102, 54, 52, 101, 51, 51, 56]
Подробнее здесь: https://stackoverflow.com/questions/763 ... ty-and-jwt