开发者社区> 问答> 正文

为什么java服务器把加签结果返给客户端,总说signature不匹配!!!!!

背景:一个简单的demo。android客户端发出下载云上一个文件请求,服务器加密,然后客户端就下载。
实现:我采用原始鉴权,在android客户端的覆盖TokenGenerator方法。依照adk所说,把content(httpMethod + "\n" + md5 + "\n" + type + "\n" + date + "\n" + ossHeaders
       + resource)发送给我的java服务器。在java服务器里加签,加完之后返回给客户端。客户端返回给TokenGenerator。
           我的java加签方式: HmacSHA1Signature hmacSHA1Signature=new HmacSHA1Signature();
                            signResult="OSS " + accessKey + ":"+hmacSHA1Signature.computeSignature(secretKey, getContent);
                            signResult=signResult.trim();
           这种方法返回去OSS报signature不匹配错误!!!!!!!!!!!!
           当然,我也试过: signResult=OSSToolKit.generateToken(accessKey, secretKey, getContent);
           这种方法,竟然signature返回值为null!!!!!!!!!!!!
问题:我想问,在java写的android服务器里怎么加签??
           你们倒是提供了关于服务器的信息,但总归语焉不详吧?而且在demo里面提到的服务器加签中:你把contnet post给  110.75.82.106/oss。那和服务器有什么关系,不是应该post            给服务器么??
吐槽:算了吧。好好解答一下,谢谢。

展开
收起
wangjiang123 2015-08-02 01:14:32 10013 0
3 条回答
写回答
取消 提交回答
  • Re为什么java服务器把加签结果返给客户端,总说signature不匹配!!!!!
    我这边没遇到什么问题。你可以先用Demo提供的本地加签方式跑通,就是这个:
           ossService.setGlobalDefaultTokenGenerator(new TokenGenerator() { // 设置全局默认加签器
                @Override
                public String generateToken(String httpMethod, String md5, String type, String date,
                        String ossHeaders, String resource) {

                    String content = httpMethod + "\n" + md5 + "\n" + type + "\n" + date + "\n" + ossHeaders
                            + resource;

                    return OSSToolKit.generateToken(accessKey, screctKey, content);
                }
            });
    成功后,保留这段代码,然后把你向server请求Token的代码也加到这里,两种方式都生成token,然后两个token都打印出来,对比一下看看是不是哪里出问题了。
    2015-08-03 09:06:46
    赞同 展开评论 打赏
  • java不懂,不过建议用官方的java代码跑通了,再一点一点对照修改。

    -------------------------

    我的理解应该都是用阿里的oss服务器吧,操作方式应该一样啊

    2015-08-02 09:26:12
    赞同 展开评论 打赏
  • Re为什么java服务器把加签结果返给客户端,总说signature不匹配!!!!!
    献上源码:
    Client
    ossService.setGlobalDefaultTokenGenerator(new TokenGenerator() { // 璁剧疆鍏ㄥ眬榛樿鍔犵鍣�
                @Override
                public String generateToken(String httpMethod, String md5, String type, String date,
                        String ossHeaders, String resource) {


                    String content = httpMethod + "\n" + md5 + "\n" + type + "\n" + date + "\n" + ossHeaders
                            + resource;
                    try {
                        //Create a socket to connect to the server
                        Socket socket=new Socket(serverUri,8000);


                        //Create an input stream to receive data from the server
                        fromServer=socket.getInputStream();
                        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(fromServer));


                        //Create an output stream to send data from the server
                        toServer=socket.getOutputStream();
                        PrintWriter printWriter=new PrintWriter(toServer);


                        content=content.replace('\n', '!');
                        content=content+"\n";
                        //Send content to server
                        printWriter.write(content);
                        //Test
                        textView1.setText("content has been already sent.It's value is "+content);


                        printWriter.flush();


                        //Recieve signature from server
                        while (true) {
                            signResult=bufferedReader.readLine().trim();
                            if(signResult!=null){
                                //Test
                                textView2.setText("Signature has already recieved.It's value is "+signResult);
                                break;
                            }


                        }
                        fromServer.close();
                        bufferedReader.close();
                        toServer.close();
                        printWriter.close();
                        socket.close();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }






                    //return "OSS " + accessKey + ":" + signature; // 注意"OSS"和accessKey之间存在一个空格!!!
                    return signResult;
                }




            });

    Server

    public static void main(String[] args) throws NoSuchAlgorithmException,InvalidKeyException{
            
              try{
                    //Create a server socket
                    serverSocket=new ServerSocket(8000);
                  
                    //Listen for a connection request
                    socket=serverSocket.accept();


                    //Create data input and output streams
                    inputFromClient=socket.getInputStream();
                    bufferedReader=new BufferedReader(new InputStreamReader(inputFromClient));
                    
                    outputToClient=socket.getOutputStream();
                    printWriter=new PrintWriter(outputToClient);


                    while (true) {
                        //Receive radius from the client
                        
                        String getContent=bufferedReader.readLine();
                        
                        //Test
                        if(getContent!=null){
                            getContent=getContent.replace('!', '\n');
                            System.out.println("Recieved content successful! It's:"+getContent);
                            //getContent=getContent.substring(0,getContent.length()-2);
                             //Encode
                            String signResult=null;
                            try {
                                //signResult=OSSToolKit.generateToken(accessKey, secretKey, getContent);
                                HmacSHA1Signature hmacSHA1Signature=new HmacSHA1Signature();
                                signResult="OSS " + accessKey + ":"+hmacSHA1Signature.computeSignature(secretKey, getContent);
                                signResult=signResult.trim();
                            } catch (Exception e) {
                                // TODO: handle exception
                                OSSLog.logD(e.toString());
                            }
                            
                            //Test
                            if(signResult!=null){
                                System.out.println("Encode signature successful! It's:"+signResult);
                            }else {
                                System.out.println("signature is null!");
                            }
                            //Send signature to the client
                            printWriter.write(signResult);
                            System.out.println("Send signature is successful!");
                            printWriter.flush();
                            break;
                        }
                        
                    }
                    inputFromClient.close();
                    bufferedReader.close();
                    outputToClient.close();
                    printWriter.close();
                    socket.close();
                    serverSocket.close();
                    
                }
                catch(IOException e){
                    e.printStackTrace();
                }
        
              
      }

















    -------------------------

    回2楼徇云一五的帖子
    官方的java代码,你是指 客户端+服务器 都是pc的方式。我跑过了,没问题的。而且 客户端+服务器 都是pc的方式并没有涉及到 服务器加签 操作。晕。。

    -------------------------

    回5楼qq449774982的帖子
    你那边也是 android客户端,服务器加密签方式?服务器用java吗??
    我java服务器也是用OSSToolKit.generateToken(accessKey, screctKey, content); 这个方法加密么??可是为什么一直返回null??
    2015-08-02 01:21:48
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
如何运维千台以上游戏云服务器 立即下载
网站/服务器取证 实践与挑战 立即下载
ECS快储存加密技术 立即下载