ben veritabanı kullanıcı (UserDAO) karşı kimlik bilgilerini kontrol etmek istiyorum Dropwizard Authentication 0.9.0-SNAPSHOTDropwizard: Temel Kimlik
kullanma.
aşağıdaki istisna
olsun! org.hibernate.HibernateException: Hiçbir oturum şu an nasıl Authenticator'a oturumu bağlamak yürütme içeriği
bağlı? Veya veritabanı kullanıcısına karşı daha iyi kontrol yöntemleri var mı?
Kimlik Kanıtlayıcı Sınıf
package com.example.helloworld.auth;
import com.example.helloworld.core.User;
import com.example.helloworld.db.UserDAO;
import com.google.common.base.Optional;
import io.dropwizard.auth.AuthenticationException;
import io.dropwizard.auth.Authenticator;
import io.dropwizard.auth.basic.BasicCredentials;
public class ExampleAuthenticator implements Authenticator<BasicCredentials, User> {
UserDAO userDAO;
public ExampleAuthenticator(UserDAO userDAO) {
this.userDAO = userDAO;
}
@Override
public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException {
Optional<User> user;
user = (Optional<User>) this.userDAO.findByEmail(credentials.getUsername());
if ("secret".equals(credentials.getPassword())) {
return Optional.of(new User(credentials.getUsername()));
}
return Optional.absent();
}
}
Uygulama Sınıfı
@Override
public void run(HelloWorldConfiguration configuration, Environment environment) throws Exception {
final UserDAO userDAO = new UserDAO(hibernate.getSessionFactory());
environment.jersey().register(new AuthDynamicFeature(
new BasicCredentialAuthFilter.Builder<User>()
.setAuthenticator(new ExampleAuthenticator(userDAO))
.setAuthorizer(new ExampleAuthorizer())
.setRealm("SUPER SECRET STUFF")
.buildAuthFilter()));
environment.jersey().register(RolesAllowedDynamicFeature.class);
//If you want to use @Auth to inject a custom Principal type into your resource
environment.jersey().register(new AuthValueFactoryProvider.Binder(User.class));
environment.jersey().register(new UserResource(userDAO));
unutmayın bu soru 0.9.0- rc ifade ettiğini rc4. –
0.9.1.Final – harshil