当前位置: 首页>>代码示例>>Java>>正文


Java Credentials类代码示例

本文整理汇总了Java中org.apache.commons.httpclient.Credentials的典型用法代码示例。如果您正苦于以下问题:Java Credentials类的具体用法?Java Credentials怎么用?Java Credentials使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Credentials类属于org.apache.commons.httpclient包,在下文中一共展示了Credentials类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getUrlContent

import org.apache.commons.httpclient.Credentials; //导入依赖的package包/类
/**
 * Retrieves the content under the given URL with username and passwort
 * authentication.
 * 
 * @param url
 *            the URL to read
 * @param username
 * @param password
 * @return the read content.
 * @throws IOException
 *             if an I/O exception occurs.
 */
private static byte[] getUrlContent(URL url, String username,
        String password) throws IOException {
    final HttpClient client = new HttpClient();

    // Set credentials:
    client.getParams().setAuthenticationPreemptive(true);
    final Credentials credentials = new UsernamePasswordCredentials(
            username, password);
    client.getState()
            .setCredentials(
                    new AuthScope(url.getHost(), url.getPort(),
                            AuthScope.ANY_REALM), credentials);

    // Retrieve content:
    final GetMethod method = new GetMethod(url.toString());
    final int status = client.executeMethod(method);
    if (status != HttpStatus.SC_OK) {
        throw new IOException("Error " + status + " while retrieving "
                + url);
    }
    return method.getResponseBody();
}
 
开发者ID:servicecatalog,项目名称:oscm,代码行数:35,代码来源:BasicAuthLoader.java

示例2: createAceOrderTestFolderWithOneAce

import org.apache.commons.httpclient.Credentials; //导入依赖的package包/类
/**
 * Helper to create a test folder with a single ACE pre-created
 */
private void createAceOrderTestFolderWithOneAce() throws IOException, JsonException {
	testUserId = H.createTestUser();
	
	testFolderUrl = H.createTestFolder();

	addOrUpdateAce(testFolderUrl, testUserId, true, null);

	//fetch the JSON for the acl to verify the settings.
	String getUrl = testFolderUrl + ".acl.json";

	Credentials creds = new UsernamePasswordCredentials("admin", "admin");
	String json = H.getAuthenticatedContent(creds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
	assertNotNull(json);
	
               JsonObject jsonObject = JsonUtil.parseObject(json);
               assertEquals(1, jsonObject.size());

               JsonObject user = jsonObject.getJsonObject(testUserId);
               assertNotNull(user);
               assertEquals(testUserId, user.getString("principal"));
               assertEquals(0, user.getInt("order"));

}
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:27,代码来源:ModifyAceTest.java

示例3: addOrUpdateAce

import org.apache.commons.httpclient.Credentials; //导入依赖的package包/类
/**
 * Helper to add or update an ace for testing
 */
private void addOrUpdateAce(String folderUrl, String principalId, boolean readGranted, String order) throws IOException, JsonException {
       String postUrl = folderUrl + ".modifyAce.html";

	//1. create an initial set of privileges
	List<NameValuePair> postParams = new ArrayList<NameValuePair>();
	postParams.add(new NameValuePair("principalId", principalId));
	postParams.add(new NameValuePair("[email protected]:read", readGranted ? "granted" : "denied"));
	postParams.add(new NameValuePair("[email protected]:write", "denied"));
	if (order != null) {
		postParams.add(new NameValuePair("order", order));
	}
	
	Credentials creds = new UsernamePasswordCredentials("admin", "admin");
	H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
}
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:19,代码来源:ModifyAceTest.java

示例4: testModifyAceResponseAsJSON

import org.apache.commons.httpclient.Credentials; //导入依赖的package包/类
/**
 * Test for SLING-1677
 */
@Test 
public void testModifyAceResponseAsJSON() throws IOException, JsonException {
	testUserId = H.createTestUser();
	
	testFolderUrl = H.createTestFolder();
	
       String postUrl = testFolderUrl + ".modifyAce.json";

	List<NameValuePair> postParams = new ArrayList<NameValuePair>();
	postParams.add(new NameValuePair("principalId", testUserId));
	postParams.add(new NameValuePair("[email protected]:read", "granted"));
	postParams.add(new NameValuePair("[email protected]:write", "denied"));
	postParams.add(new NameValuePair("[email protected]:modifyAccessControl", "bogus")); //invalid value should be ignored.
	
	Credentials creds = new UsernamePasswordCredentials("admin", "admin");
	String json = H.getAuthenticatedPostContent(creds, postUrl, HttpTest.CONTENT_TYPE_JSON, postParams, HttpServletResponse.SC_OK);

       //make sure the json response can be parsed as a JSON object
       JsonObject jsonObject = JsonUtil.parseObject(json);
	assertNotNull(jsonObject);
}
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:25,代码来源:ModifyAceTest.java

示例5: testRemoveAce

import org.apache.commons.httpclient.Credentials; //导入依赖的package包/类
public void testRemoveAce() throws IOException, JsonException {
	String folderUrl = createFolderWithAces(false);
	
	//remove the ace for the testUser principal
	String postUrl = folderUrl + ".deleteAce.html"; 
	List<NameValuePair> postParams = new ArrayList<NameValuePair>();
	postParams.add(new NameValuePair(":applyTo", testUserId));
       Credentials creds = new UsernamePasswordCredentials("admin", "admin");
	assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);

	//fetch the JSON for the acl to verify the settings.
	String getUrl = folderUrl + ".acl.json";

	String json = getAuthenticatedContent(creds, getUrl, CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
	assertNotNull(json);

	JsonObject jsonObject = JsonUtil.parseObject(json);
	assertNotNull(jsonObject);
	assertEquals(0, jsonObject.size());
}
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:21,代码来源:RemoveAcesTest.java

示例6: testRemoveAcesResponseAsJSON

import org.apache.commons.httpclient.Credentials; //导入依赖的package包/类
/**
 * Test for SLING-1677
 */
public void testRemoveAcesResponseAsJSON() throws IOException, JsonException {
	String folderUrl = createFolderWithAces(true);
	
	//remove the ace for the testUser principal
	String postUrl = folderUrl + ".deleteAce.json"; 
	List<NameValuePair> postParams = new ArrayList<NameValuePair>();
	postParams.add(new NameValuePair(":applyTo", testUserId));
	postParams.add(new NameValuePair(":applyTo", testGroupId));
       Credentials creds = new UsernamePasswordCredentials("admin", "admin");
       String json = getAuthenticatedPostContent(creds, postUrl, CONTENT_TYPE_JSON, postParams, HttpServletResponse.SC_OK);

       //make sure the json response can be parsed as a JSON object
       JsonObject jsonObject = JsonUtil.parseObject(json);
	assertNotNull(jsonObject);
}
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:19,代码来源:RemoveAcesTest.java

示例7: testAnonymousContent

import org.apache.commons.httpclient.Credentials; //导入依赖的package包/类
public void testAnonymousContent() throws Exception {
    // disable credentials -> anonymous session
    final URL url = new URL(HTTP_BASE_URL);
    final AuthScope scope = new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM);
    httpClient.getParams().setAuthenticationPreemptive(false);
    httpClient.getState().setCredentials(scope, null);
    
    try {
        assertContent();
    } finally {
        // re-enable credentials -> admin session
        httpClient.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials("admin", "admin");
        httpClient.getState().setCredentials(scope, defaultcreds);
    }
}
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:17,代码来源:AnonymousAccessTest.java

示例8: testValidatingIncorrectHttpBasicCredentials

import org.apache.commons.httpclient.Credentials; //导入依赖的package包/类
@Test
public void testValidatingIncorrectHttpBasicCredentials() throws Exception {

    // assume http and webdav are on the same host + port
    URL url = new URL(HttpTest.HTTP_BASE_URL);
    Credentials defaultcreds = new UsernamePasswordCredentials("garbage", "garbage");
    H.getHttpClient().getState()
            .setCredentials(new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM), defaultcreds);

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new NameValuePair("j_validate", "true"));
    HttpMethod post = H.assertPostStatus(HttpTest.HTTP_BASE_URL + "/j_security_check",
            HttpServletResponse.SC_FORBIDDEN, params, null);
    assertXReason(post);

    HttpMethod get = H.assertHttpStatus(HttpTest.HTTP_BASE_URL + "/?j_validate=true",
            HttpServletResponse.SC_FORBIDDEN);
    assertXReason(get);
}
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:20,代码来源:AuthenticationResponseCodeTest.java

示例9: testPreventLoopIncorrectHttpBasicCredentials

import org.apache.commons.httpclient.Credentials; //导入依赖的package包/类
@Test
public void testPreventLoopIncorrectHttpBasicCredentials() throws Exception {

    // assume http and webdav are on the same host + port
    URL url = new URL(HttpTest.HTTP_BASE_URL);
    Credentials defaultcreds = new UsernamePasswordCredentials("garbage", "garbage");
    H.getHttpClient().getState()
            .setCredentials(new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM), defaultcreds);

    final String requestUrl = HttpTest.HTTP_BASE_URL + "/junk?param1=1";
    HttpMethod get = new GetMethod(requestUrl);
    get.setRequestHeader("Referer", requestUrl);
    get.setRequestHeader("User-Agent", "Mozilla/5.0 Sling Integration Test");
    int status = H.getHttpClient().executeMethod(get);
    assertEquals(HttpServletResponse.SC_UNAUTHORIZED, status);
}
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:17,代码来源:AuthenticationResponseCodeTest.java

示例10: assertAuthenticatedHttpStatus

import org.apache.commons.httpclient.Credentials; //导入依赖的package包/类
/** Verify that given URL returns expectedStatusCode
 * @throws IOException */
public void assertAuthenticatedHttpStatus(Credentials creds, String urlString, int expectedStatusCode, String assertMessage) throws IOException {
    URL baseUrl = new URL(HTTP_BASE_URL);
    AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
    GetMethod getMethod = new GetMethod(urlString);
    getMethod.setDoAuthentication(true);
    Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
    try {
        httpClient.getState().setCredentials(authScope, creds);

        final int status = httpClient.executeMethod(getMethod);
        if(assertMessage == null) {
            assertEquals(urlString,expectedStatusCode, status);
        } else {
            assertEquals(assertMessage, expectedStatusCode, status);
        }
    } finally {
        httpClient.getState().setCredentials(authScope, oldCredentials);
    }
}
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:22,代码来源:AuthenticatedTestUtil.java

示例11: createTestUser

import org.apache.commons.httpclient.Credentials; //导入依赖的package包/类
public String createTestUser() throws IOException {
      String postUrl = HTTP_BASE_URL + "/system/userManager/user.create.html";

      String testUserId = "testUser" + getNextInt();
      List<NameValuePair> postParams = new ArrayList<NameValuePair>();
      postParams.add(new NameValuePair(":name", testUserId));
      postParams.add(new NameValuePair("pwd", "testPwd"));
      postParams.add(new NameValuePair("pwdConfirm", "testPwd"));
Credentials creds = new UsernamePasswordCredentials("admin", "admin");
final String msg = "Unexpected status while attempting to create test user at " + postUrl; 
      assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, msg);
      
      final String sessionInfoUrl = HTTP_BASE_URL + "/system/sling/info.sessionInfo.json";
      assertAuthenticatedHttpStatus(creds, sessionInfoUrl, HttpServletResponse.SC_OK, 
              "session info failed for user " + testUserId);

      return testUserId;
  }
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:19,代码来源:AuthenticatedTestUtil.java

示例12: testCreateUserResponseAsJSON

import org.apache.commons.httpclient.Credentials; //导入依赖的package包/类
/**
 * Test for SLING-1677
 */
@Test 
public void testCreateUserResponseAsJSON() throws IOException, JsonException {
       String postUrl = HttpTest.HTTP_BASE_URL + "/system/userManager/user.create.json";

	testUserId = "testUser" + random.nextInt();
	List<NameValuePair> postParams = new ArrayList<NameValuePair>();
	postParams.add(new NameValuePair(":name", testUserId));
	postParams.add(new NameValuePair("marker", testUserId));
	postParams.add(new NameValuePair("pwd", "testPwd"));
	postParams.add(new NameValuePair("pwdConfirm", "testPwd"));
	Credentials creds = new UsernamePasswordCredentials("admin", "admin");
	String json = H.getAuthenticatedPostContent(creds, postUrl, HttpTest.CONTENT_TYPE_JSON, postParams, HttpServletResponse.SC_OK);

	//make sure the json response can be parsed as a JSON object
	JsonObject jsonObj = JsonUtil.parseObject(json);
	assertNotNull(jsonObj);
}
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:21,代码来源:CreateUserTest.java

示例13: testUpdateUserResponseAsJSON

import org.apache.commons.httpclient.Credentials; //导入依赖的package包/类
/**
 * Test for SLING-1677
 */
@Test 
public void testUpdateUserResponseAsJSON() throws IOException, JsonException {
	testUserId = H.createTestUser();
	
       String postUrl = HttpTest.HTTP_BASE_URL + "/system/userManager/user/" + testUserId + ".update.json";

	List<NameValuePair> postParams = new ArrayList<NameValuePair>();
	postParams.add(new NameValuePair("displayName", "My Updated Test User"));
	postParams.add(new NameValuePair("url", "http://www.apache.org/updated"));
	Credentials creds = new UsernamePasswordCredentials(testUserId, "testPwd");
	String json = H.getAuthenticatedPostContent(creds, postUrl, HttpTest.CONTENT_TYPE_JSON, postParams, HttpServletResponse.SC_OK);

	//make sure the json response can be parsed as a JSON object
	JsonObject jsonObj = JsonUtil.parseObject(json);
	assertNotNull(jsonObj);
}
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:20,代码来源:UpdateUserTest.java

示例14: testCanAddUser

import org.apache.commons.httpclient.Credentials; //导入依赖的package包/类
/**
 * Checks whether the current user has been granted privileges
 * to add a new user.
 */
@Test 
public void testCanAddUser() throws JsonException, IOException {
	testUserId = H.createTestUser();

	String getUrl = HttpTest.HTTP_BASE_URL + "/system/userManager/user/" + testUserId + ".privileges-info.json";

	//fetch the JSON for the test page to verify the settings.
	Credentials testUserCreds = new UsernamePasswordCredentials(testUserId, "testPwd");

	String json = H.getAuthenticatedContent(testUserCreds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
	assertNotNull(json);
	JsonObject jsonObj = JsonUtil.parseObject(json);
	
	assertEquals(false, jsonObj.getBoolean("canAddUser"));
}
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:20,代码来源:UserPrivilegesInfoTest.java

示例15: testCanAddGroup

import org.apache.commons.httpclient.Credentials; //导入依赖的package包/类
/**
 * Checks whether the current user has been granted privileges
 * to add a new group.
 */
@Test 
public void testCanAddGroup() throws IOException, JsonException {
	testUserId = H.createTestUser();

	String getUrl = HttpTest.HTTP_BASE_URL + "/system/userManager/user/" + testUserId + ".privileges-info.json";

	//fetch the JSON for the test page to verify the settings.
	Credentials testUserCreds = new UsernamePasswordCredentials(testUserId, "testPwd");

	String json = H.getAuthenticatedContent(testUserCreds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
	assertNotNull(json);
	JsonObject jsonObj = JsonUtil.parseObject(json);
	
	assertEquals(false, jsonObj.getBoolean("canAddGroup"));
}
 
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:20,代码来源:UserPrivilegesInfoTest.java


注:本文中的org.apache.commons.httpclient.Credentials类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。