本文整理汇总了Java中io.dropwizard.auth.Authenticator类的典型用法代码示例。如果您正苦于以下问题:Java Authenticator类的具体用法?Java Authenticator怎么用?Java Authenticator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Authenticator类属于io.dropwizard.auth包,在下文中一共展示了Authenticator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: configure
import io.dropwizard.auth.Authenticator; //导入依赖的package包/类
@Override
public boolean configure(final FeatureContext featureContext) {
final UserRepository userRepo = CDI.current().select(UserRepository.class).get();
final Authenticator<String, User> authenticator = new GoogleAuthenticator(
authConfig.getClientId(), userRepo, authConfig.getHostedDomain()
);
final Authenticator<String, User> cachingAuthenticator = new CachingAuthenticator<>(
metricRegistry, authenticator, authConfig.getAuthenticationCachePolicy()
);
featureContext.register(new AuthDynamicFeature(
new OAuthCredentialAuthFilter.Builder<User>()
.setAuthenticator(cachingAuthenticator)
.setPrefix("Bearer")
.buildAuthFilter()));
featureContext.register(new AuthValueFactoryProvider.Binder<>(User.class));
return true;
}
示例2: getAuthenticator
import io.dropwizard.auth.Authenticator; //导入依赖的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']");
}
}
示例3: JWTAuthTestResourceConfig
import io.dropwizard.auth.Authenticator; //导入依赖的package包/类
public JWTAuthTestResourceConfig() {
super(true, new MetricRegistry());
final Authenticator<String, String> authenticator = new Authenticator<String, String>() {
@Override
public Optional<String> authenticate(String credentials) throws AuthenticationException {
if ("good-one".equals(credentials)) {
return Optional.of("good-one");
}
if ("bad-one".equals(credentials)) {
throw new AuthenticationException("server ran out of entropy");
}
return Optional.absent();
}
};
register(AuthFactory.binder(new JwtAuthFactory<>(authenticator, REALM, String.class).prefix(PREFIX)));
register(AuthResource.class);
}
示例4: createAuthenticator
import io.dropwizard.auth.Authenticator; //导入依赖的package包/类
/**
* <p>If a credentialFile is provided, this method will use that file to populate the list of Peers the Authenticator
* checks during request processing. If instead the "users" and "passwords" Strings are provided, this method will use
* those to populate the list of Peers.</p>
* @return An Authenticator appropriate for registering with Jersey as described
* https://dropwizard.github.io/dropwizard/manual/auth.html
*/
public Authenticator<BasicCredentials, Peer> createAuthenticator() {
PasswordEncryptor passwordEncryptor = encryptor.getPasswordEncryptor();
if (this.credentialFile != null) {
InputStream allowedPeersResource = this.getClass().getClassLoader().getResourceAsStream(this.credentialFile);
return new AllowedPeerAuthenticator(new FlatFilePeerDAO(allowedPeersResource),
passwordEncryptor);
}
else if (this.users != null && this.passwords != null && this.delimiter != null) {
return new AllowedPeerAuthenticator(new StringPeerDAO(this.users, this.passwords, this.delimiter),
passwordEncryptor);
}
else {
throw new IllegalStateException("Illegal call to createAuthenticator() when no valid configuration was set");
}
}
示例5: registerAuthenticator
import io.dropwizard.auth.Authenticator; //导入依赖的package包/类
/**
*
* @param environment The Dropwizard environment
* @param authorizer A specific authorizer to use instead of the default PermitAllAuthorizer. See
* http://www.dropwizard.io/0.9.1/docs/manual/auth.html for more details
*/
public void registerAuthenticator(Environment environment, Authorizer<Peer> authorizer) {
Preconditions.checkNotNull(environment, "Illegal call to registerAuthenticator with a null Environment object");
Authenticator<BasicCredentials, Peer> authenticator;
if (this.cachePolicy != null) {
authenticator = createCachingAuthenticator(environment.metrics());
}
else {
authenticator = createAuthenticator();
}
environment.jersey().register(new AuthDynamicFeature(
new BasicCredentialAuthFilter.Builder<Peer>()
.setAuthenticator(authenticator)
.setAuthorizer(authorizer)
.setRealm(this.realm)
.buildAuthFilter()));
environment.jersey().register(RolesAllowedDynamicFeature.class);
environment.jersey().register(new AuthValueFactoryProvider.Binder<>(Peer.class));
}
示例6: run
import io.dropwizard.auth.Authenticator; //导入依赖的package包/类
@Override
public void run(ExampleAppConfiguration configuration, Environment environment) throws Exception {
final LdapConfiguration ldapConfiguration = configuration.getLdapConfiguration();
Authenticator<BasicCredentials, User> ldapAuthenticator = new CachingAuthenticator<>(
environment.metrics(),
new ResourceAuthenticator(new LdapAuthenticator(ldapConfiguration)),
ldapConfiguration.getCachePolicy());
environment.jersey().register(new AuthDynamicFeature(
new BasicCredentialAuthFilter.Builder<User>()
.setAuthenticator(ldapAuthenticator)
.setRealm("LDAP")
.buildAuthFilter()));
environment.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class));
environment.healthChecks().register("ldap", new LdapHealthCheck<>(
new ResourceAuthenticator(new LdapCanAuthenticate(ldapConfiguration))));
}
示例7: testAuthenticate
import io.dropwizard.auth.Authenticator; //导入依赖的package包/类
@Test
public void testAuthenticate() throws AuthenticationException {
final Authenticator<BasicCredentials, Principal> authenticator = new BasicAuthenticator(getAuthFile());
final BasicCredentials credentials = new BasicCredentials("acoburn", "secret");
final Optional<Principal> res = authenticator.authenticate(credentials);
assertTrue(res.isPresent());
res.ifPresent(p -> {
assertEquals("https://acoburn.people.amherst.edu/", p.getName());
});
}
示例8: testAuthenticateNoWebid
import io.dropwizard.auth.Authenticator; //导入依赖的package包/类
@Test
public void testAuthenticateNoWebid() throws AuthenticationException {
final Authenticator<BasicCredentials, Principal> authenticator = new BasicAuthenticator(getAuthFile());
final BasicCredentials credentials = new BasicCredentials("other", "pass");
final Optional<Principal> res = authenticator.authenticate(credentials);
assertFalse(res.isPresent());
}
示例9: testAuthenticateInvalid
import io.dropwizard.auth.Authenticator; //导入依赖的package包/类
@Test
public void testAuthenticateInvalid() throws AuthenticationException {
final Authenticator<BasicCredentials, Principal> authenticator = new BasicAuthenticator(getAuthFile());
final BasicCredentials credentials = new BasicCredentials("acoburn", "incorrect");
final Optional<Principal> res = authenticator.authenticate(credentials);
assertFalse(res.isPresent());
}
示例10: testAuthenticateInvalidFile
import io.dropwizard.auth.Authenticator; //导入依赖的package包/类
@Test
public void testAuthenticateInvalidFile() throws AuthenticationException {
final Authenticator<BasicCredentials, Principal> authenticator = new BasicAuthenticator(
getAuthFile() + "missing");
final BasicCredentials credentials = new BasicCredentials("acoburn", "incorrect");
final Optional<Principal> res = authenticator.authenticate(credentials);
assertFalse(res.isPresent());
}
示例11: testAuthenticateUnreadableFile
import io.dropwizard.auth.Authenticator; //导入依赖的package包/类
@Test
public void testAuthenticateUnreadableFile() throws AuthenticationException {
final Authenticator<BasicCredentials, Principal> authenticator = new BasicAuthenticator(getAuthFile());
final BasicCredentials credentials = new BasicCredentials("acoburn", "secret");
final File userFile = new File(getAuthFile());
assumeTrue(userFile.setReadable(false));
final Optional<Principal> res = authenticator.authenticate(credentials);
assertFalse(res.isPresent());
userFile.setReadable(true);
}
示例12: testAuthenticate
import io.dropwizard.auth.Authenticator; //导入依赖的package包/类
@Test
public void testAuthenticate() throws AuthenticationException {
final Authenticator<String, Principal> authenticator = new AnonymousAuthenticator();
final Optional<Principal> res = authenticator.authenticate("blahblah");
assertTrue(res.isPresent());
res.ifPresent(p -> {
assertEquals(Trellis.AnonymousAgent.getIRIString(), p.getName());
});
}
示例13: testAuthenticate
import io.dropwizard.auth.Authenticator; //导入依赖的package包/类
@Test
public void testAuthenticate() throws AuthenticationException {
final String key = "c2VjcmV0";
final String token = Jwts.builder().setSubject("https://acoburn.people.amherst.edu")
.signWith(SignatureAlgorithm.HS512, key).compact();
final Authenticator<String, Principal> authenticator = new JwtAuthenticator(key, true);
final Optional<Principal> result = authenticator.authenticate(token);
assertTrue(result.isPresent());
result.ifPresent(p -> {
assertEquals("https://acoburn.people.amherst.edu", p.getName());
});
}
示例14: testAuthenticateNoSub
import io.dropwizard.auth.Authenticator; //导入依赖的package包/类
@Test
public void testAuthenticateNoSub() throws AuthenticationException {
final String key = "c2VjcmV0";
final String token = Jwts.builder().setIssuer("http://localhost")
.signWith(SignatureAlgorithm.HS512, key).compact();
final Authenticator<String, Principal> authenticator = new JwtAuthenticator(key, true);
final Optional<Principal> result = authenticator.authenticate(token);
assertFalse(result.isPresent());
}
示例15: testAuthenticateSubIss
import io.dropwizard.auth.Authenticator; //导入依赖的package包/类
@Test
public void testAuthenticateSubIss() throws AuthenticationException {
final String key = "c2VjcmV0";
final String token = Jwts.builder().setSubject("acoburn").setIssuer("http://localhost")
.signWith(SignatureAlgorithm.HS512, key).compact();
final Authenticator<String, Principal> authenticator = new JwtAuthenticator(key, true);
final Optional<Principal> result = authenticator.authenticate(token);
assertTrue(result.isPresent());
result.ifPresent(p -> {
assertEquals("http://localhost/acoburn", p.getName());
});
}