WSSE timestamp generation in Android

a 夏天 提交于 2019-12-10 11:45:29

问题


I have a web service with WSSE authentication. The web service is written in Symfony2 and I can access it throught a PHP client and also with the plugin for chrome called REST Console, so it works. To generate the headers manually I have used this wsse generator.

To make the Android client I have followed this tutorial, it works but only when it doesn't generate the timestamp.

The code which generate the timestamp is:

private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
...
private String generateTimestamp() {
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    return sdf.format(new Date());
}

The String it returns it is correct, the format and everything look perfect but it tells "not authorized", but it works when I write the timestamp directly like this:

private String generateTimestamp() {
    return "2014-03-25T09:00:26Z";
}

I have checked the encoding, trying to encode it like UTF-8 And Cp-1252 (the file encoding):

private String generateTimestamp() {
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    String hora = sdf.format(new Date());

    String correctString = "fail";
    try {
        correctString = new String(hora.getBytes(), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    correctString.hashCode();
    //String buena = "2014-03-25T08:10:59Z";
    return correctString ;
}

I generated the timestamp using Calendar.YEAR + Calendar.MONTH.... but still doesn't work.

At the moment I am trying to test it in a real phone but I don't think it is an emulator problem.

Do you have any suggestion? Thank you very much in advance!


回答1:


The solution that works is:

private String generateTimestamp() {
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    String hora = sdf.format(new Date());

    return "\""+hora+"\"";
}

The problem is that the final String that Android sends to the webservice has two double quotes like this:

X-WSSE: UsernameToken Username="esther", PasswordDigest="FgWu9GAKkbmsc52fZHu3VINHlKw=", Nonce="OWVkNDMxMWY2OGE4MTZjOQ==", Created=""2014-03-25T09:00:26Z""

But it works, even if it is not the best solution.

I think it is a different between how Java/Android/SimpleDateFormat generate the string, but...

I have tested trying to concatenate empty string but it still doesn't work.



来源:https://stackoverflow.com/questions/22629847/wsse-timestamp-generation-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!