为什么java服务器把加签结果返给客户端,总说signature不匹配!!!!!
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??
赞0
踩0