繁体   English   中英

使用摘要身份验证Java保护其余Web服务

[英]securing rest web service with digest authentication java

我正在使用apache httpclient 4.3.3开发一个rest客户端,该客户端支持HTTP Basic和Digest身份验证。 我需要一个带有摘要身份验证的其余Web服务示例来测试我的客户端。 任何人都可以帮助我,即使是在线休息Web服务也将非常感激。

这是摘要身份验证的客户端代码:

final HttpHost targetHost = new HttpHost("localhost", 8080, "http");
    final CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(user, password));

    // Create AuthCache instance
    final AuthCache authCache = new BasicAuthCache();
    // Generate DIGEST scheme object and add it to the local auth cache
    DigestScheme digestAuth = new DigestScheme();
    // Suppose we already know the realm name
    digestAuth.overrideParamter("realm", "some-realm");
    // Suppose we already know the expected nonce value
    digestAuth.overrideParamter("nonce", "some-nonce");
    authCache.put(targetHost, digestAuth);

    // Add AuthCache to the execution context
    HttpClientContext context = HttpClientContext.create();
    context.setAuthCache(authCache);

之后,我可以致电:

RestClient genericRestClient = new GenericRestClient.Builder(METHOD_URL)
            .setUser(DEFAULT_USER).setPassword(DEFAULT_PASS)
            .setAuthType(AuthenticationType.DIGEST_AUTH)
            .setHttpVersion(HTTPVersion.HTTP_1_1).build();
    genericRestClient.doGet();

我需要将其他Web服务与摘要身份验证一起使用,以测试我的客户端。

我已经开始使用此Web服务:

@GET
@Path("/get")
@Produces("application/json")
public Product getProduct(@Context HttpHeaders headers) {
    .....

    Product product = new Product();
    product.setName("Product 1");
    product.setQty(50);

    return product;

感谢您提供任何帮助

我终于找到了一个很好的示例spring-security-rest-digest-auth和spring security,它可能对某些人有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM