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


Java BasicCredentials类代码示例

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


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

示例1: postNewTodoItem

import io.dropwizard.auth.basic.BasicCredentials; //导入依赖的package包/类
@Test
public void postNewTodoItem() throws UnsupportedEncodingException, AuthenticationException {
   final String newTodoItem = fixture("fixtures/todoItem_new.json");
   final TodoItem expectedTodoItem = new TodoItem(1, "make new todo items", true);
   when(oaccBasicAuthenticator.authenticate(any(BasicCredentials.class)))
         .thenReturn(java.util.Optional.ofNullable(oaccPrincipal));
   when(oaccPrincipal.getAccessControlContext()).thenReturn(oacc);
   when(todoItemService.createItem(eq(oacc), any(TodoItem.class)))
         .thenReturn(expectedTodoItem);

   final Response response = resources.getJerseyTest()
         .target("/todos")
         .request()
         .header(Header.Authorization.name(), getBasicAuthHeader(EMAIL, PASSWORD))
         .post(Entity.entity(newTodoItem, MediaType.APPLICATION_JSON));

   assertThat(response.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());   // 200 OK
   assertThat(response.readEntity(TodoItem.class)).isEqualTo(expectedTodoItem);
}
 
开发者ID:acciente,项目名称:oacc-example-securetodo,代码行数:20,代码来源:TodoItemResourceTest.java

示例2: postNewTodoItemThatFailsValidation

import io.dropwizard.auth.basic.BasicCredentials; //导入依赖的package包/类
@Test
public void postNewTodoItemThatFailsValidation() throws AuthenticationException, UnsupportedEncodingException {
   final String blankTodoItem = fixture("fixtures/todoItem_blank.json");
   when(oaccBasicAuthenticator.authenticate(any(BasicCredentials.class)))
         .thenReturn(java.util.Optional.ofNullable(oaccPrincipal));
   when(oaccPrincipal.getAccessControlContext()).thenReturn(oacc);
   when(todoItemService.createItem(eq(oacc), any(TodoItem.class)))
         .thenThrow(new IllegalArgumentException("Either title or completed (or both) is required"));

   final Response response = resources.getJerseyTest()
         .target("/todos")
         .request()
         .header(Header.Authorization.name(), getBasicAuthHeader(EMAIL, PASSWORD))
         .post(Entity.entity(blankTodoItem, MediaType.APPLICATION_JSON));

   assertThat(response.getStatus()).isEqualTo(422);   // 422 Unprocessable Entity
   verifyZeroInteractions(oacc);
}
 
开发者ID:acciente,项目名称:oacc-example-securetodo,代码行数:19,代码来源:TodoItemResourceTest.java

示例3: getTodoItems

import io.dropwizard.auth.basic.BasicCredentials; //导入依赖的package包/类
@Test
public void getTodoItems() throws AuthenticationException, UnsupportedEncodingException {
   final List<TodoItem> expectedTodoItems
         = Collections.singletonList(new TodoItem(1, "list all items", true));

   when(oaccBasicAuthenticator.authenticate(any(BasicCredentials.class)))
         .thenReturn(java.util.Optional.ofNullable(oaccPrincipal));
   when(oaccPrincipal.getAccessControlContext()).thenReturn(oacc);
   when(todoItemService.findByAuthenticatedUser(eq(oacc)))
         .thenReturn(expectedTodoItems);

   final Response response = resources.getJerseyTest()
         .target("/todos")
         .request()
         .header(Header.Authorization.name(), getBasicAuthHeader(EMAIL, PASSWORD))
         .get();

   assertThat(response.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());   // 200 OK
}
 
开发者ID:acciente,项目名称:oacc-example-securetodo,代码行数:20,代码来源:TodoItemResourceTest.java

示例4: patchTodoItem

import io.dropwizard.auth.basic.BasicCredentials; //导入依赖的package包/类
@Test
public void patchTodoItem() throws AuthenticationException, UnsupportedEncodingException {
   final long todoItemId = 1;
   final String todoItem = fixture("fixtures/todoItem_new.json");
   final TodoItem expectedTodoItem = new TodoItem(1, "update titles", false);
   when(oaccBasicAuthenticator.authenticate(any(BasicCredentials.class)))
         .thenReturn(java.util.Optional.ofNullable(oaccPrincipal));
   when(oaccPrincipal.getAccessControlContext()).thenReturn(oacc);
   when(todoItemService.updateItem(eq(oacc), any(Long.class), any(TodoItem.class)))
         .thenReturn(expectedTodoItem);

   final Response response = resources.getJerseyTest()
         .target("/todos/" + todoItemId)
         .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true) // to support PATCH
         .request()
         .header(Header.Authorization.name(), getBasicAuthHeader(EMAIL, PASSWORD))
         .build("PATCH", Entity.entity(todoItem, MediaType.APPLICATION_JSON))
         .invoke();

   assertThat(response.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());   // 200 OK
   assertThat(response.readEntity(TodoItem.class)).isEqualTo(expectedTodoItem);
}
 
开发者ID:acciente,项目名称:oacc-example-securetodo,代码行数:23,代码来源:TodoItemResourceTest.java

示例5: patchTodoItemWithoutAuthorization

import io.dropwizard.auth.basic.BasicCredentials; //导入依赖的package包/类
@Test
public void patchTodoItemWithoutAuthorization() throws AuthenticationException, UnsupportedEncodingException {
   final long todoItemId = 1;
   final String todoItem = fixture("fixtures/todoItem_new.json");
   when(oaccBasicAuthenticator.authenticate(any(BasicCredentials.class)))
         .thenReturn(java.util.Optional.ofNullable(oaccPrincipal));
   when(oaccPrincipal.getAccessControlContext()).thenReturn(oacc);
   when(todoItemService.updateItem(eq(oacc), any(Long.class), any(TodoItem.class)))
         .thenThrow(new NotAuthorizedException("not authorized"));

   final Response response = resources.getJerseyTest()
         .target("/todos/" + todoItemId)
         .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true) // to support PATCH
         .request()
         .header(Header.Authorization.name(), getBasicAuthHeader(EMAIL, PASSWORD))
         .build("PATCH", Entity.entity(todoItem, MediaType.APPLICATION_JSON))
         .invoke();

   assertThat(response.getStatus()).isEqualTo(Response.Status.FORBIDDEN.getStatusCode());   // 403 Forbidden
}
 
开发者ID:acciente,项目名称:oacc-example-securetodo,代码行数:21,代码来源:TodoItemResourceTest.java

示例6: patchTodoItemThatDoesNotExist

import io.dropwizard.auth.basic.BasicCredentials; //导入依赖的package包/类
@Test
public void patchTodoItemThatDoesNotExist() throws AuthenticationException, UnsupportedEncodingException {
   final long todoItemId = 1;
   final String todoItem = fixture("fixtures/todoItem_new.json");
   when(oaccBasicAuthenticator.authenticate(any(BasicCredentials.class)))
         .thenReturn(java.util.Optional.ofNullable(oaccPrincipal));
   when(oaccPrincipal.getAccessControlContext()).thenReturn(oacc);
   when(todoItemService.updateItem(eq(oacc), any(Long.class), any(TodoItem.class)))
         .thenThrow(new IllegalArgumentException("Resource " + todoItemId + " not found!"));

   final Response response = resources.getJerseyTest()
         .target("/todos/" + todoItemId)
         .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true) // to support PATCH
         .request()
         .header(Header.Authorization.name(), getBasicAuthHeader(EMAIL, PASSWORD))
         .build("PATCH", Entity.entity(todoItem, MediaType.APPLICATION_JSON))
         .invoke();

   assertThat(response.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());   // 404 Not Found
   verifyZeroInteractions(oacc);
}
 
开发者ID:acciente,项目名称:oacc-example-securetodo,代码行数:22,代码来源:TodoItemResourceTest.java

示例7: patchTodoItemThatFailsValidation

import io.dropwizard.auth.basic.BasicCredentials; //导入依赖的package包/类
@Test
public void patchTodoItemThatFailsValidation() throws AuthenticationException, UnsupportedEncodingException {
   final long todoItemId = 1;
   final String blankTodoItem = fixture("fixtures/todoItem_blank.json");
   when(oaccBasicAuthenticator.authenticate(any(BasicCredentials.class)))
         .thenReturn(java.util.Optional.ofNullable(oaccPrincipal));
   when(oaccPrincipal.getAccessControlContext()).thenReturn(oacc);
   when(todoItemService.updateItem(eq(oacc), any(Long.class), any(TodoItem.class)))
         .thenThrow(new IllegalArgumentException("Either title or completed (or both) is required."));

   final Response response = resources.getJerseyTest()
         .target("/todos/" + todoItemId)
         .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true) // to support PATCH
         .request()
         .header(Header.Authorization.name(), getBasicAuthHeader(EMAIL, PASSWORD))
         .build("PATCH", Entity.entity(blankTodoItem, MediaType.APPLICATION_JSON))
         .invoke();

   assertThat(response.getStatus()).isEqualTo(422);   // 422 Unprocessable Entity
   verifyZeroInteractions(oacc);
}
 
开发者ID:acciente,项目名称:oacc-example-securetodo,代码行数:22,代码来源:TodoItemResourceTest.java

示例8: putShareAssociationOnTodoItem

import io.dropwizard.auth.basic.BasicCredentials; //导入依赖的package包/类
@Test
public void putShareAssociationOnTodoItem() throws AuthenticationException, UnsupportedEncodingException {
   final long todoItemId = 1;
   final String targetEmail = "[email protected]";
   when(oaccBasicAuthenticator.authenticate(any(BasicCredentials.class)))
         .thenReturn(java.util.Optional.ofNullable(oaccPrincipal));
   when(oaccPrincipal.getAccessControlContext()).thenReturn(oacc);

   final Response response = resources.getJerseyTest()
         .target("/todos/" + todoItemId)
         .queryParam("share_with", targetEmail)
         .request()
         .header(Header.Authorization.name(), getBasicAuthHeader(EMAIL, PASSWORD))
         .put(Entity.json(""));

   assertThat(response.getStatus()).isEqualTo(Response.Status.NO_CONTENT.getStatusCode());   // 204 No Content
   assertThat(response.readEntity(Object.class)).isNull();
}
 
开发者ID:acciente,项目名称:oacc-example-securetodo,代码行数:19,代码来源:TodoItemResourceTest.java

示例9: putShareAssociationOnTodoItemWithoutAuthorization

import io.dropwizard.auth.basic.BasicCredentials; //导入依赖的package包/类
@Test
public void putShareAssociationOnTodoItemWithoutAuthorization() throws AuthenticationException, UnsupportedEncodingException {
   final long todoItemId = 1;
   final String targetEmail = "[email protected]";
   when(oaccBasicAuthenticator.authenticate(any(BasicCredentials.class)))
         .thenReturn(java.util.Optional.ofNullable(oaccPrincipal));
   when(oaccPrincipal.getAccessControlContext()).thenReturn(oacc);
   doThrow(new NotAuthorizedException("not authorized"))
         .when(todoItemService).shareItem(eq(oacc), eq(todoItemId), eq(targetEmail));

   final Response response = resources.getJerseyTest()
         .target("/todos/" + todoItemId)
         .queryParam("share_with", targetEmail)
         .request()
         .header(Header.Authorization.name(), getBasicAuthHeader(EMAIL, PASSWORD))
         .put(Entity.json(""));

   assertThat(response.getStatus()).isEqualTo(Response.Status.FORBIDDEN.getStatusCode());   // 403 Forbidden
   verifyZeroInteractions(oacc);
}
 
开发者ID:acciente,项目名称:oacc-example-securetodo,代码行数:21,代码来源:TodoItemResourceTest.java

示例10: putShareAssociationOnTodoItemOrUserThatDoesNotExist

import io.dropwizard.auth.basic.BasicCredentials; //导入依赖的package包/类
@Test
public void putShareAssociationOnTodoItemOrUserThatDoesNotExist() throws AuthenticationException, UnsupportedEncodingException {
   final long todoItemId = 1;
   final String targetEmail = "[email protected]";
   when(oaccBasicAuthenticator.authenticate(any(BasicCredentials.class)))
         .thenReturn(java.util.Optional.ofNullable(oaccPrincipal));
   when(oaccPrincipal.getAccessControlContext()).thenReturn(oacc);
   doThrow(new IllegalArgumentException("Resource " + todoItemId + " not found!"))
         .when(todoItemService).shareItem(eq(oacc), eq(todoItemId), eq(targetEmail));

   final Response response = resources.getJerseyTest()
         .target("/todos/" + todoItemId)
         .queryParam("share_with", targetEmail)
         .request()
         .header(Header.Authorization.name(), getBasicAuthHeader(EMAIL, PASSWORD))
         .put(Entity.json(""));

   assertThat(response.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());   // 404 Not Found
   verifyZeroInteractions(oacc);
}
 
开发者ID:acciente,项目名称:oacc-example-securetodo,代码行数:21,代码来源:TodoItemResourceTest.java

示例11: lookup

import io.dropwizard.auth.basic.BasicCredentials; //导入依赖的package包/类
private Optional<String> lookup(final BasicCredentials creds) {
    final File file = new File(credentialsFile);
    if (!file.exists()) {
        return empty();
    }

    try (final Stream<String> fileLines = lines(file.toPath())) {
        return fileLines.map(String::trim).filter(line -> !line.startsWith("#"))
            .map(line -> line.split(":", 3)).filter(x -> x.length == 3)
            .filter(d -> d[0].trim().equals(creds.getUsername()) && d[1].trim().equals(creds.getPassword()))
            .map(d -> d[2].trim()).findFirst();
    } catch (final IOException ex) {
        LOGGER.error("Error processing credentials file: {}", ex.getMessage());
    }
    return empty();
}
 
开发者ID:trellis-ldp,项目名称:trellis-rosid,代码行数:17,代码来源:BasicAuthenticator.java

示例12: authenticate

import io.dropwizard.auth.basic.BasicCredentials; //导入依赖的package包/类
@Override
public Optional<Person> authenticate(BasicCredentials credentials) throws AuthenticationException {
	List<Person> p = dao.findByDomainUsername(credentials.getUsername());
	if (p.size() > 0) { 
		Person person = p.get(0);
		if (person.getStatus().equals(PersonStatus.INACTIVE)) { 
			//An Inactive person just logged in, make them active. 
			person.setStatus(PersonStatus.ACTIVE);
			AnetObjectEngine.getInstance().getPersonDao().update(person);
		}
		return Optional.of(person);
       }
       
	if (credentials.getUsername().equals(credentials.getPassword())) {
		//Special development mechanism to perform a 'first login'. 
		Person newUser = new Person();
		newUser.setName(credentials.getUsername());
		newUser.setRole(Role.ADVISOR);
		newUser.setDomainUsername(credentials.getUsername());
		newUser.setStatus(PersonStatus.NEW_USER);
		newUser = dao.insert(newUser);
       	
		return Optional.of(newUser);
       }
	return Optional.empty();
}
 
开发者ID:deptofdefense,项目名称:anet,代码行数:27,代码来源:AnetDevAuthenticator.java

示例13: authenticate

import io.dropwizard.auth.basic.BasicCredentials; //导入依赖的package包/类
@Override
public Optional<User> authenticate(final BasicCredentials credentials) throws AuthenticationException {
    String username = credentials.getUsername();
    String password = credentials.getPassword();

    try {
        try (AutoclosableDirContext context = buildContext(username, password)) {

            //Get the user DN
            SearchResult userSearched = searchUser(context, username);

            //Check if user is in the prod group
            final boolean prodUser = checkIfUserBelongsToGroup(context, userSearched.getNameInNamespace(), configuration.getProdGroupName());
            final boolean techUser = checkIfUserBelongsToGroup(context, userSearched.getNameInNamespace(), configuration.getTechGroupName());

            User user = new User(username, prodUser, techUser);
            return Optional.of(user);
        }

    } catch (NamingException e) {
        LOGGER.debug("{} failed to authenticate {}", username);
    }

    return Optional.empty();
}
 
开发者ID:voyages-sncf-technologies,项目名称:hesperides,代码行数:26,代码来源:LDAPAuthenticator.java

示例14: getAuthenticator

import io.dropwizard.auth.basic.BasicCredentials; //导入依赖的package包/类
@JsonIgnore
public Optional<Authenticator<BasicCredentials, User>> getAuthenticator() {
    final String authType = authenticatorType.toLowerCase();

    if (authType.equals("none")) {
        return Optional.empty();
    } else if (authType.equals("simple")) {
        return Optional.of(new SimpleAuthenticator());
    } else if (authType.equals("ldap")) {
        if (ldapConfiguration == null) {
            throw new IllegalArgumentException("Authenticator type is set to 'ldap' but ldap configuration is empty.");
        }

        return Optional.of(new LDAPAuthenticator(ldapConfiguration));
    } else {
        throw new IllegalArgumentException("Authenticator " + authenticatorType + " is unknow. Use one of ['none', 'simple', 'ldap']");
    }
}
 
开发者ID:voyages-sncf-technologies,项目名称:hesperides,代码行数:19,代码来源:HesperidesConfiguration.java

示例15: authenticate

import io.dropwizard.auth.basic.BasicCredentials; //导入依赖的package包/类
@Override public Optional<User> authenticate(BasicCredentials credentials)
    throws AuthenticationException {
  User user = null;
  String username = credentials.getUsername();
  if (!User.isSanitizedUsername(username)) {
    logger.info("Username: {} must match pattern: {}", username, User.USERNAME_PATTERN);
    return Optional.empty();
  }

  String password = credentials.getPassword();

  // Get hashed password column from BCrypt table by username
  Optional<String> optionalHashedPwForUser = userDAO.getHashedPassword(username);
  if (!optionalHashedPwForUser.isPresent()) {
    return Optional.empty();
  }

  if (BCrypt.checkpw(password, optionalHashedPwForUser.get())) {
    user = User.named(username);
  }

  return Optional.ofNullable(user);
}
 
开发者ID:square,项目名称:keywhiz,代码行数:24,代码来源:BcryptAuthenticator.java


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