【问题标题】:Rest Android security technology choice [closed]休息Android安全技术选择[关闭]
【发布时间】:2014-05-14 20:08:24
【问题描述】:

我正在构建 Web 服务(使用 J2EE)以及一个 android 客户端。现在我面临选择哪种安全技术?并将其与我的 Rest 服务集成。 我爬了谷歌,建议是关于 Oauth2.0、使用 TLS 的基本身份验证、api 密钥。 在我使用 HttpClient 的 android 应用程序中,如果集成这些解决方案之一将需要对我的代码进行重大更改,我不会。在这个场合,任何人都可以让我很好地描述前两个,并建议最好的教程和例子......

【问题讨论】:

  • can anyone could make me a great description of the two first ones with suggestion of best tutorials with example ?Google 对此非常有用
  • 3个shemes哪个最好?

标签: android security rest jakarta-ee oauth


【解决方案1】:
public class DataLoader {
DefaultHttpClient sslClient;
private boolean silent;

public class CustomX509TrustManager implements X509TrustManager {

    @Override
    public void checkClientTrusted(X509Certificate[] chain, String authType)
            throws CertificateException {
    }

    @Override
    public void checkServerTrusted(
            java.security.cert.X509Certificate[] certs, String authType)
            throws CertificateException {
    }

    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }
}

public class CustomSSLSocketFactory extends SSLSocketFactory {
    SSLContext sslContext = SSLContext.getInstance("TLS");

    public CustomSSLSocketFactory(KeyStore truststore)
            throws NoSuchAlgorithmException, KeyManagementException,
            KeyStoreException, UnrecoverableKeyException {
        super(truststore);
        TrustManager tm = new CustomX509TrustManager();
        sslContext.init(null, new TrustManager[] { tm }, null);
    }

    public CustomSSLSocketFactory(SSLContext context)
            throws KeyManagementException, NoSuchAlgorithmException,
            KeyStoreException, UnrecoverableKeyException {
        super(null);
        sslContext = context;
    }

    @Override
    public Socket createSocket(Socket socket, String host, int port,
            boolean autoClose) throws IOException, UnknownHostException {
        return sslContext.getSocketFactory().createSocket(socket, host,
                port, autoClose);
    }

    @Override
    public Socket createSocket() throws IOException {
        return sslContext.getSocketFactory().createSocket();
    }
}

public HttpResponse execute(HttpUriRequest request) {
    if (!(silent)) {
        Log.i("Performing request", "Performing request");
        for (Header header : request.getAllHeaders())
            Log.i("Performing request",
                    "Performing request "
                            + new StringBuilder()
                                    .append("Request header: ")
                                    .append(header.getName()).append(" - ")
                                    .append(header.getValue()).toString());
    }
    HttpResponse response = null;
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(null, new TrustManager[] { new CustomX509TrustManager() },
                new SecureRandom());
        HttpClient client = new DefaultHttpClient();
        SSLSocketFactory ssf = new CustomSSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = client.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 443));
        sslClient = new DefaultHttpClient(ccm, client.getParams());
        response = sslClient.execute(request);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return response;
}

}

【讨论】:

  • 嘿,我的朋友,请使用这个类。它将帮助您克服所有安全问题,谢谢
  • 您上面的代码暗示我在 Rest 服务器上使用 TLS/SSL 证书。正确的?如何在 glassfish 4.0 上启用 ssl over Rest?如何获得证书以用于您的课程?
  • 您已经开发了一个 servlet。正确的?它只不过是一个网页。如果您想要启用 SSL 的网页,那么您必须在安装该证书后购买 SSL 证书。如果您没有该证书,请使用上述代码。上述代码适用于所有安全问题。如果您满意,请点赞。谢谢。
  • 我没有使用 servlet iam bulding Rest 服务,使用 jersey 框架而不是 http,但我不知道将 https 与它集成或使用 oauth 功能来确保安全。我不再想要auth,因为它使用第三方服务器进行身份验证。那么如何在 Rest 服务端启用 ssl/TLS
  • 您必须从威瑞信和 thawte 等公司购买 SSL/TSL 证书。谢谢
猜你喜欢
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-13
  • 2015-09-29
  • 2013-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多