I’m trying to implement the follow the Micronaut guide at https://guides.micronaut.io/latest/micronaut-security-jwt-maven-java.html
When I get to https://guides.micronaut.io/latest/micronaut-security-jwt-maven-java.html#use-the-micronaut-http-client-and-jwt, my tests all fail.
My client is
package eco.aem.elements.stationcatalog;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Consumes;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.security.authentication.UsernamePasswordCredentials;
import io.micronaut.security.token.render.BearerAccessRefreshToken;
import static io.micronaut.http.HttpHeaders.AUTHORIZATION;
import static io.micronaut.http.MediaType.TEXT_PLAIN;
@Client("/")
public interface AppClient {
@Post("/login")
BearerAccessRefreshToken login(@Body UsernamePasswordCredentials credentials);
@Consumes(TEXT_PLAIN)
@Get
String home(@Header(AUTHORIZATION) String authorization);
}
and my test is
package eco.aem.elements.stationcatalog;
import com.nimbusds.jwt.JWTParser;
import com.nimbusds.jwt.SignedJWT;
import io.micronaut.security.authentication.UsernamePasswordCredentials;
import io.micronaut.security.token.render.BearerAccessRefreshToken;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import java.text.ParseException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
@MicronautTest
class DeclarativeHttpClientWithJwtTest {
@Inject
AppClient appClient;
@Test
void verifyJwtAuthenticationWorksWithDeclarativeClient() throws ParseException {
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("sherlock", "password");
BearerAccessRefreshToken loginRsp = appClient.login(creds);
assertNotNull(loginRsp);
assertNotNull(loginRsp.getAccessToken());
assertInstanceOf(SignedJWT.class, JWTParser.parse(loginRsp.getAccessToken()));
System.out.println(loginRsp.getAccessToken());
System.out.println(loginRsp.getUsername());
String msg = appClient.home("Bearer " + loginRsp.getAccessToken());
assertEquals("sherlock", msg);
}
}
When I run the test, I get
io.micronaut.http.client.exceptions.HttpClientResponseException: Forbidden
When the code executes
String msg = appClient.home("Bearer " + loginRsp.getAccessToken());
What am I missing here? I’ve looked over this code over and over and I’m not seeing what the issue is.