Page 1 of 1

Spring Boot - Jwts.builder kann die Methode 'Signwith () nicht auflösen' nicht auflösen.

Posted: 22 Mar 2025, 04:09
by Anonymous
Ich habe dieses Spring Boot + Angular Tutorial auf YouTube verfolgt, das Spring Security und JSON Web Token (JWT) zur Authentifizierung eines Benutzers verwendet. Ich bin jedoch zu einem Punkt im Video gekommen, an dem ich einen Fehler in meiner JWTUTIL2 -Klasse habe. Aus irgendeinem Grund, wenn ich versuche, meine Frühlingsanwendung auszuführen, erhalte ich diesen Fehler: < /p>
Error:(41, 17) java: no suitable method found for signWith(java.security.PrivateKey)
method io.jsonwebtoken.JwtBuilder.signWith(io.jsonwebtoken.SignatureAlgorithm,byte[]) is not applicable
(actual and formal argument lists differ in length)
method io.jsonwebtoken.JwtBuilder.signWith(io.jsonwebtoken.SignatureAlgorithm,java.lang.String) is not applicable
(actual and formal argument lists differ in length)
method io.jsonwebtoken.JwtBuilder.signWith(io.jsonwebtoken.SignatureAlgorithm,java.security.Key) is not applicable
(actual and formal argument lists differ in length)
< /code>
Wenn ich mir die JWTUTIL2 -Klasse anschaue, hat sie eine rote Linie unter dem Parameter, die an die jwts.builder () -Methode geliefert wird.@Service
public class JwtUtilTwo
{
private KeyStore keyStore;

@PostConstruct
public void init() {
try
{
keyStore = KeyStore.getInstance("JKS");
InputStream resourceAsStream = getClass().getResourceAsStream("/springblog.jks");
keyStore.load(resourceAsStream, "secret".toCharArray());
}
catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException e)
{
throw new SpringRubbishRewardsException("Exception occurred while loading keystore");
}
}

public String generateToken(Authentication authentication) {
User principal = (User) authentication.getPrincipal();
return Jwts.builder()
.setSubject(principal.getUsername())
.signWith(getPrivateKey()) //ERROR HERE
.compact();
}

private PrivateKey getPrivateKey()
{
try
{
return (PrivateKey) keyStore.getKey("springblog", "secret".toCharArray());
}
catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException e)
{
throw new SpringRubbishRewardsException("Exception occurred while retrieving public key from keystore");
}
}
}
< /code>
Jetzt habe ich tatsächlich einen Weg gefunden, den Fehler zu beheben, indem ich die Maven -Abhängigkeiten in der POM -Datei geändert hat. Ich habe mir das Github -Repository des YouTube -Tutorials für das Video angesehen und die folgenden JWT -Abhängigkeiten kopiert und meine ursprüngliche JWT -Abhängigkeit kommentiert: < /p>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0

rubbish-rewards
rubbish-rewards-api
1.0-SNAPSHOT


org.springframework.boot
spring-boot-starter-parent
2.3.0.RELEASE




org.springframework.boot
spring-boot-starter-web



org.springframework.boot
spring-boot-starter-data-jpa



mysql
mysql-connector-java
runtime



org.springframework.boot
spring-boot-starter-security







io.jsonwebtoken
jjwt-api
0.10.5


io.jsonwebtoken
jjwt-impl
runtime
0.10.5


io.jsonwebtoken
jjwt-jackson
runtime
0.10.5





javax.xml.bind
jaxb-api
2.3.0




< /code>
Obwohl dies das erste Problem behebt, wird ein neues Problem eingeführt, wenn ich versuche, meine Anwendung auszuführen. Diesmal hat es einen Fehler, der lautet: < /p>
2020-07-14 11:52:35.833 WARN 14616 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authenticateController': Unsatisfied dependency expressed through field 'authenticateService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authenticateService': Unsatisfied dependency expressed through field 'jwtUtil2'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jwtUtilTwo': Invocation of init method failed; nested exception is rubbishrewards.exceptions.SpringRubbishRewardsException: Exception occurred while loading keystore
< /code>
Ich würde den Code in meinen Authentifizierungscontroller- und Authentifizierungsservice -Klassen veröffentlichen, aber ich habe das Gefühl, dass diese Frage zu überfüllt werden wird. Stattdessen finden Sie hier ein Link zu meinem Anwendungs ​​-GitHub -Repository. Wenn jemand helfen könnte, würde ich es schätzen.