Restlet如何在HTTP基本身份验证中解密秘密

时间:2015-05-24 10:02:01

标签: java rest base64 jax-rs restlet

我有以下类处理我的一条路线:

public class HotelsSrv extends ServerResource implements 
   HotelsListResource {
   private String hotelId;

   @Override
   protected void doInit() throws ResourceException {
       super.doInit();
       String str;
       String secret = getRequest().getChallengeResponse().getSecret().toString();
       byte[] bytes = new BASE64Decoder().decodeBuffer(secret)
       str = new String(bytes);

       System.out.println("user: "+getRequest().getChallengeResponse().getIdentifier());

       System.out.println("password: "+str);
}

我正在尝试解密这个秘密,所以我可以使用自定义程序验证它,但这行提出了未知的异常:

    byte[] bytes = new BASE64Decoder().decodeBuffer(secret)

2 个答案:

答案 0 :(得分:2)

试试这段代码

public void authenticate(HttpServletRequest req) {
    String authhead = req.getHeader("Authorization");

        if (authhead != null) {
            // *****Decode the authorisation String*****
            byte[] e = Base64.decode(authhead.substring(6));
            String usernpass = new String(e);
            // *****Split the username from the password*****
            String user = usernpass.substring(0, usernpass.indexOf(":"));
            String password = usernpass.substring(usernpass.indexOf(":") + 1);
            // check username and password
        }
}

答案 1 :(得分:1)

无需对秘密进行编码/解码。它存储在ChallengeResponse类中的char表中,仅出于安全原因(参见javadocs,此链接有更多解释http://www.careercup.com/question?id=14955419

String secret = new String(getRequest().getChallengeResponse().getSecret());