简体   繁体   中英

JAVA: Restfull web service not passing parameter correctly

I am trying to create a simple rest service, using spring mvc. Everything is working fine except for one method!

I want to pass a signature(using a private key), encoded in Base64 (using bouncy castle encoder) as a parameter so i can validate it in the server side. The problem is, the signature is not getting to the server correctly!

My server method:

@RequestMapping("/indirectAcess")
public @ResponseBody byte[] getResource(
        @RequestParam(value="id", required=true) String idString,
        @RequestParam(value="url", required=true) String url, 
        @RequestParam(value="sig", required=true) byte[] sig){

    try{
        int id = Integer.parseInt(idString);
        if(IndirectAccessValidate(id, url, sig)){
            return "HERE IS YOUR RESOURCE".getBytes();
        }
        return null;
    } catch(Exception e){
        return "ERROR".getBytes();
    }

}

My client method:

private static void indirectAccess(Scanner in){
    System.out.print("URL: ");
    String url = in.nextLine();
    Teste finalUrl = client.generateIndirectAccess(url);
    byte[] response = rest.getForObject(indirect, byte[].class, finalUrl.getId(), finalUrl.getUrl(), finalUrl.getSig());
    System.out.println(new String(response));
}

I've tried to debug it and the server is receiving the ID and URL correctly, but not the signature (its just some kind of garbage).

Can anyone please help me? Thank you

After a while i solved it!

I still don't know why but, i had to pass the signature as Base64.encodeBase64URLSafeString(finalUrl.getSig()

and decoded it as Base64.decodeBase64(sig)

Server

    @RequestMapping("/indirectAcess")
    public @ResponseBody byte[] getResource(
            @RequestParam(value="id", required=true) String idString,
            @RequestParam(value="url", required=true) String url, 
            @RequestParam(value="sig", required=true) String sig){

        try{
            int id = Integer.parseInt(idString);
            if(IndirectAccessValidate(id, url, Base64.decodeBase64(sig))){
                return "HERE IS YOUR RESOURCE".getBytes();
            }
            return null;
        } catch(Exception e){
            return "ERROR".getBytes();
        }

    }

Client

private static void indirectAccess(Scanner in){
    System.out.print("URL: ");
    String url = in.nextLine();
    Teste finalUrl = client.generateIndirectAccess(url);
    byte[] response = rest.getForObject(indirect, byte[].class, finalUrl.getId(), finalUrl.getUrl(), Base64.encodeBase64URLSafeString(finalUrl.getSig()));
    System.out.println(new String(response));
}

I would appreciate if someone could explain me why this happened, because i still can find a reason for... Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM