【问题标题】:DIgest authentication in RESTful serviceRESTful 服务中的 DIgest 身份验证
【发布时间】:2015-03-01 00:27:25
【问题描述】:

我知道java中有类似下面的东西

urlConnection.setRequestProperty("授权", "基本" + 编码字符串);

用于将基本身份验证的授权标头发送到休息服务。 摘要式身份验证是否有任何等效属性。

请提出建议。

【问题讨论】:

    标签: java rest digest-authentication


    【解决方案1】:

    这是一个使用 HttpURLConnection 的示例:

    final String username = "username";
    final String password = "password";
    
    // Compatible for authentication Basic Digest Ntlm (not working with Negotiate)
    Authenticator.setDefault(new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            PasswordAuthentication pa = new PasswordAuthentication (username, password.toCharArray());
            return pa;
        }
    });
    
    BufferedReader in = null;
    StringBuffer sb = new StringBuffer();
    
    try {
        HttpURLConnection connection = (HttpURLConnection) new URL("http://127.0.0.1:8180/").openConnection();
    
        in = new BufferedReader(new InputStreamReader(connection
                .getInputStream()));
    
        String line;
        while ((line = in.readLine()) != null) {
            sb.append(line).append("\n");
        }
    } catch (java.net.ProtocolException e) {
        sb.append("User Or Password is wrong!");
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (Exception e) {
            System.out.println("Exception");
        }
    }
    
    System.out.println("The Data is: " + sb.toString());
    

    【讨论】:

      猜你喜欢
      • 2012-06-19
      • 2016-02-20
      • 2020-11-13
      • 1970-01-01
      • 2011-03-03
      • 2015-09-10
      • 2012-08-31
      • 2021-07-05
      • 2018-11-29
      相关资源
      最近更新 更多