Java源码示例:org.apache.http.auth.BasicUserPrincipal
示例1
private CloseableHttpClient getHttpClient() {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new Credentials() {
@Override
public Principal getUserPrincipal() {
return new BasicUserPrincipal("guest");
}
@Override
public String getPassword() {
return "guest-password";
}
});
return HttpClients.custom()
.setDefaultCredentialsProvider(credentialsProvider)
.build();
}
示例2
/**
* @throws Exception if the test fails
*/
@Test
public void credentialsAndEmptyPath() throws Exception {
final URL url = new URL("http://john.smith:[email protected]");
final WebRequest request = new WebRequest(url);
final Credentials credentials = request.getUrlCredentials();
assertNotNull("Credentials object is null", credentials);
assertEquals(new BasicUserPrincipal("john.smith"), credentials.getUserPrincipal());
assertEquals("secret", credentials.getPassword());
}
示例3
/**
* @throws Exception if the test fails
*/
@Test
public void credentialsAndPathWithDots() throws Exception {
final URL url = new URL("http://john.smith:[email protected]/../foo.html");
final WebRequest request = new WebRequest(url);
final Credentials credentials = request.getUrlCredentials();
assertNotNull("Credentials object is null", credentials);
assertEquals(new BasicUserPrincipal("john.smith"), credentials.getUserPrincipal());
assertEquals("secret", credentials.getPassword());
}
示例4
/**
* @throws Exception if the test fails
*/
@Test
public void credentialsAndInternationalizedDomainName() throws Exception {
final URL url = new URL("http://john.smith:[email protected]öcälhöst/");
final WebRequest request = new WebRequest(url);
final Credentials credentials = request.getUrlCredentials();
assertNotNull("Credentials object is null", credentials);
assertEquals(new BasicUserPrincipal("john.smith"), credentials.getUserPrincipal());
assertEquals("secret", credentials.getPassword());
}
示例5
/**
* @throws Exception if the test fails
*/
@Test
public void credentialsOnlyUsernameInURL() throws Exception {
final URL url = new URL("http://[email protected]/");
final WebRequest request = new WebRequest(url);
final Credentials credentials = request.getUrlCredentials();
assertEquals(new BasicUserPrincipal("john.smith"), credentials.getUserPrincipal());
assertEquals("", credentials.getPassword());
}
示例6
/**
* @throws Exception if the test fails
*/
@Test
public void credentialsOnlyPasswordInURL() throws Exception {
final URL url = new URL("http://:[email protected]/");
final WebRequest request = new WebRequest(url);
final Credentials credentials = request.getUrlCredentials();
assertEquals(new BasicUserPrincipal(""), credentials.getUserPrincipal());
assertEquals("secret", credentials.getPassword());
}
示例7
/**
* @throws Exception if the test fails
*/
@Test
public void credentialsEmptyURL() throws Exception {
final URL url = new URL("http://:@localhost/");
final WebRequest request = new WebRequest(url);
final Credentials credentials = request.getUrlCredentials();
assertEquals(new BasicUserPrincipal(""), credentials.getUserPrincipal());
assertEquals("", credentials.getPassword());
}
示例8
private HttpServletRequest makeAuthRequestWrapper(HttpServletRequest request, UserLoginInfo userLoginInfo) {
// see BasicAuthPlugin implementation for reference of why this is here
return new HttpServletRequestWrapper(request) {
@Override
public Principal getUserPrincipal() {
return new BasicUserPrincipal(userLoginInfo.getSolrUsername());
}
};
}
示例9
AuthorizationContext getMockContext(Map<String, Object> values) {
return new MockAuthorizationContext(values) {
@Override
public Principal getUserPrincipal() {
Object userPrincipal = values.get("userPrincipal");
return userPrincipal == null ? null : new BasicUserPrincipal(String.valueOf(userPrincipal));
}
};
}
示例10
@Override
AuthorizationContext getMockContext(Map<String, Object> values) {
return new MockAuthorizationContext(values) {
@Override
public Principal getUserPrincipal() {
String userPrincipal = (String) values.get("userPrincipal");
return userPrincipal == null ? null :
principals.get(userPrincipal) != null ? principals.get(userPrincipal) :
new BasicUserPrincipal(userPrincipal);
}
};
}
示例11
protected void forward(String user, HttpServletRequest req, ServletResponse rsp,
FilterChain chain) throws IOException, ServletException {
if(user != null) {
final Principal p = new BasicUserPrincipal(user);
req = wrapWithPrincipal(req, p);
}
chain.doFilter(req, rsp);
}
示例12
@Test
public void returnsFailureIfCannotFindUser() throws Exception {
User amy = user().withFirstName("Amy").build();
given(userRepository.findOne(amy.getId())).willReturn(amy);
Principal p = new BasicUserPrincipal("name");
Try<User> userTry = underTest.extractUserFromPrincipal(p);
assertThat(userTry, isFailure(instanceOf(UserSessionFailure.class)));
}
示例13
@Test
public void testChooseClientAliasNotfound() throws Exception {
final X509KeyManager m = new CertificateStoreX509KeyManager(new DisabledCertificateIdentityCallback(), new Host(new TestProtocol()), new DisabledCertificateStore()).init();
assertNull(m.chooseClientAlias(new String[]{"RSA", "DSA"},
new Principal[]{new BasicUserPrincipal("user")}, new Socket("test.cyberduck.ch", 443)));
}
示例14
@SuppressForbidden(reason = "Needs currentTimeMillis to compare against time in header")
@Override
public boolean doAuthenticate(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws Exception {
String requestURI = request.getRequestURI();
if (requestURI.endsWith(PublicKeyHandler.PATH)) {
numPassThrough.inc();
filterChain.doFilter(request, response);
return true;
}
long receivedTime = System.currentTimeMillis();
String header = request.getHeader(HEADER);
if (header == null) {
//this must not happen
log.error("No SolrAuth header present");
numMissingCredentials.inc();
filterChain.doFilter(request, response);
return true;
}
List<String> authInfo = StrUtils.splitWS(header, false);
if (authInfo.size() < 2) {
log.error("Invalid SolrAuth Header {}", header);
numErrors.mark();
filterChain.doFilter(request, response);
return true;
}
String nodeName = authInfo.get(0);
String cipher = authInfo.get(1);
PKIHeaderData decipher = decipherHeader(nodeName, cipher);
if (decipher == null) {
log.error("Could not decipher a header {} . No principal set", header);
numMissingCredentials.inc();
filterChain.doFilter(request, response);
return true;
}
if ((receivedTime - decipher.timestamp) > MAX_VALIDITY) {
log.error("Invalid key request timestamp: {} , received timestamp: {} , TTL: {}", decipher.timestamp, receivedTime, MAX_VALIDITY);
numErrors.mark();
filterChain.doFilter(request, response);
return true;
}
final Principal principal = "$".equals(decipher.userName) ?
SU :
new BasicUserPrincipal(decipher.userName);
numAuthenticated.inc();
filterChain.doFilter(wrapWithPrincipal(request, principal), response);
return true;
}
示例15
SetupFilter( String userName ) {
subject = new Subject();
subject.getPrincipals().add( new BasicUserPrincipal( userName ) );
}
示例16
SetupFilter( String userName ) {
subject = new Subject();
subject.getPrincipals().add( new BasicUserPrincipal( userName ) );
}
示例17
SetupFilter( String userName ) {
subject = new Subject();
subject.getPrincipals().add( new BasicUserPrincipal( userName ) );
}