@POST @Produces("application/json") @Consumes("application/json") public Response authenticateUser(Credentials credentials) { String username = credentials.getUsername(); String password = credentials.getPassword(); // Authenticate the user, issue a token and return a response }
private static Principal getAuthPrincipal(final AuthState authState) { AuthScheme scheme = authState.getAuthScheme(); if (scheme != null && scheme.isComplete() && scheme.isConnectionBased()) { Credentials creds = authState.getCredentials(); if (creds != null) { return creds.getUserPrincipal(); } } return null; }
/** * Retrieves the name to authenticate with. * * @return String the domain these credentials are intended to authenticate with. */ public String getDomain() { return this.principal.getDomain(); }
private CredentialsProvider makeCredentialsProvider(String host, int port, String username, String password) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials( new AuthScope(host, port), new UsernamePasswordCredentials(username, password) ); return credentialsProvider; }
public HttpBasicAuthInterceptor(String username, String password, CookieStore cookieStore, String cn, boolean isSSL, Map<String, String> additionalHeaders, Map<String, String> customCookies) { super(cookieStore, cn, isSSL, additionalHeaders, customCookies); this.authScheme = new BasicScheme(); if (username != null) { this.credentials = new UsernamePasswordCredentials(username, password); } }
/** * determine the Credentials implementation to use * @param auth * @return a {@code Credentials} instance, either {{@link UsernamePasswordCredentials} or {@link NTCredentials}} */ protected Credentials configureCredentials(AuthConfiguration auth) { if (null != auth.getCredentialType() && auth.getCredentialType().equalsIgnoreCase(AuthConfiguration.NT_CREDS)) { return new NTCredentials(auth.getUsername(), auth.getPassword(), auth.getHostname(), auth.getDomain()); } else { return new UsernamePasswordCredentials(auth.getUsername(), auth.getPassword()); } } }
/** * Returns a new instance of AuthScope used for proxy authentication. */ private static AuthScope newAuthScope(HttpClientSettings settings) { return new AuthScope(settings.getProxyHost(), settings.getProxyPort()); }
/** * Returns a new instance of NTCredentials used for proxy authentication. */ private static Credentials newNTCredentials(HttpClientSettings settings) { return new NTCredentials(settings.getProxyUsername(), settings.getProxyPassword(), settings.getProxyWorkstation(), settings.getProxyDomain()); }
public String getUserName() { return this.principal.getName(); }
@Override public int hashCode() { return this.principal.hashCode(); }
@Override public String toString() { return this.principal.toString(); }
public String getUserName() { return this.principal.getUsername(); }
/** * Assigns the given {@link AuthScheme authentication scheme}. * * @param authScheme the {@link AuthScheme authentication scheme} */ public void setAuthScheme(final AuthScheme authScheme) { if (authScheme == null) { invalidate(); return; } this.authScheme = authScheme; }
/** * Set credentials to field <code>credsProvider</code>. * * @param username the username, part of credentials * @param password the password, part of credentials */ public void setCredentials(String username, String password) { credsProvider.setCredentials( new AuthScope(host.getHostName(), host.getPort(), AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, password)); }
public Client basic(final String username, final String password) { creds = new UsernamePasswordCredentials(username, password); return this; }
DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials( new AuthScope("localhost", 443), new UsernamePasswordCredentials("username", "password"));
/** * Set authentication credentials to be used for the given host and port. * * @param host * @param port * @param user * @param pass */ public void basic(String host, int port, String user, String pass) { builder.getClient().getCredentialsProvider().setCredentials( new AuthScope(host, port), new UsernamePasswordCredentials(user, pass) ); }
DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials( new AuthScope("PROXY HOST", 8080), new UsernamePasswordCredentials("username", "password")); HttpHost targetHost = new HttpHost("TARGET HOST", 443, "https"); HttpHost proxy = new HttpHost("PROXY HOST", 8080); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
/** * BASIC authentication<br/> * Official Example: https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org * /apache/http/examples/client/ClientAuthentication.java */ private void addBasicCredentials(BasicAuthInfo authInfo, Map<AuthScope, Credentials> credentialsMap) { logger.info("BASIC authentication for: {}", authInfo.getLoginTarget()); Credentials credentials = new UsernamePasswordCredentials(authInfo.getUsername(), authInfo.getPassword()); credentialsMap.put(new AuthScope(authInfo.getHost(), authInfo.getPort()), credentials); }
public ProxyHostCredentialsProvider(ProxyHostProperties... proxyHostProperties) { for (ProxyHostProperties proxy : proxyHostProperties) { if (proxy != null && proxy.getUsername() != null && proxy.getPassword() != null) { AuthScope authscope = new AuthScope(proxy.getHost(), proxy.getPort()); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()); setCredentials(authscope, credentials); } } } }