0

i'm newbie with micronaut and can't get value in a body request.

I'm using "x-www-form-urlencoded", and i need get some values for use on authentication.

Controller:

@Controller
@Secured(SecurityRule.IS_AUTHENTICATED)
public class ControllerApi {
    @Post("/hello")
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public String helloPost(){

        return "POST hello ";
    }
}

AuthenticationProviderForm:

@Singleton
public class AuthenticationProviderForm implements AuthenticationProvider  {


    @Override
    public Publisher<AuthenticationResponse> authenticate(@Nullable HttpRequest<?> httpRequest,
                                                          AuthenticationRequest<?, ?> authenticationRequest) {
                   httpRequest.getBody();    //This return is "empty", and i need the values here.

    }
}

Request:

curl --location --request POST 'http://localhost:8080/hello' \
--header 'Origin: localhost' \
--header 'Authorization: Basic YWxhbjEyMzpjYWp1MTIz' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'scope=TESTE' \
--data-urlencode 'client_id=alan123' \
--data-urlencode 'client_secret=caju123'

Thanks !

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156

3 Answers3

0

Try seting your header to accept urlencoded content type:

httpRequest.setHeader('Content-Type', 'application/x-www-form-urlencoded');
Breno Araripe
  • 51
  • 1
  • 7
  • I can't do this, my httpRequest dont have this method... =/ ( import io.micronaut.http.HttpRequest ) – Alan Michel Jun 07 '22 at 14:12
  • "I can't do this, my httpRequest dont have this method... =/ ( import io.micronaut.http.HttpRequest )"- does `myRequest.getHeaders().add("Content-Type", "application/x-www-form-urlencoded")` work? – Jeff Scott Brown Jun 07 '22 at 19:05
0

The body will always be empty inside of a AuthenticationProvider

Since version 2.5, the Micronaut Framework executes the filters
and then it reads the HTTP Request’s body. 
SecurityFilter evaluates the beans of type SecurityRule. 
Because of that, SecurityRule cannot rely on HTTP Request’s body 
because the Micronaut Framework has not read the body yet.
ShingJo
  • 614
  • 1
  • 7
  • This answer is likely why you're not getting an httpRequest. You'll notice in my answer that I'm hitting the `/login` endpoint directly, and not using basic auth, which must behave differently as I'm getting a body. – Alpha Fighter Jun 09 '22 at 19:33
0

I had a similar issue where I needed to get at a captcha that was included in the post body. Here's what I ended up doing to get access to it.

my post looks like this:

### Login with captcha
POST http://localhost:8081/login
Content-Type: application/x-www-form-urlencoded

username=myuser&password=mypass&captcha=12345

my authProvider's authenticate method:

    @Override
    public Publisher<AuthenticationResponse> authenticate(@Nullable HttpRequest<?> httpRequest,
                                                          AuthenticationRequest<?, ?> authRequest)
    {
        if (httpRequest == null)
            return Mono.empty();

        final Optional<LoginCredentials> loginRequestOptional = httpRequest.getBody(LoginCredentials.class);
        if (loginRequestOptional.isEmpty())
        {
            return Mono.empty();
        }
        else
        {
            final LoginCredentials loginRequest = loginRequestOptional.get();
            return Flux.create(emitter -> {
                final AuthenticationResponse authResponse = authenticateHavingCaptcha(loginRequest, httpRequest);
                emitter.next(authResponse);
                emitter.complete();
            });
        }

and the LoginCredentials class looks like this:

@Introspected
public class LoginCredentials extends UsernamePasswordCredentials
{
    @NotBlank
    @NotNull
    private String captcha;


    public String getCaptcha()
    {
        return captcha;
    }


    public void setCaptcha(String captcha)
    {
        this.captcha = captcha;
    }
}

I don't know why you're getting an empty httpRequest, but hopefully this will help you on your way.

Alpha Fighter
  • 110
  • 2
  • 9