gpt4 book ai didi

basic-authentication - 删除向导:BasicAuth

转载 作者:行者123 更新时间:2023-12-03 03:09:54 29 4
gpt4 key购买 nike

使用Dropwizard Authentication 0.9.0-SNAPSHOT

我想检查数据库用户 (UserDAO) 的凭据。

我收到以下异常

! org.hibernate.HibernateException: No session currently bound to execution context

如何将 session 绑定(bind)到Authenticator?或者有更好的方法来检查数据库用户吗?

验证器类

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();
}
}

应用程序类

@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));

最佳答案

要获得与 0.9+ 一起使用的身份验证,您需要以下内容。具体可以引用这个changeset举个例子。

包含依赖项。

<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-auth</artifactId>
<version>${dropwizard.version}</version>
</dependency>

注册与身份验证相关的内容。

private void registerAuthRelated(Environment environment) {
UnauthorizedHandler unauthorizedHandler = new UnAuthorizedResourceHandler();
AuthFilter basicAuthFilter = new BasicCredentialAuthFilter.Builder<User>()
.setAuthenticator(new BasicAuthenticator())
.setAuthorizer(new UserAuthorizer())
.setRealm("shire")
.setUnauthorizedHandler(unauthorizedHandler)
.setPrefix("Basic")
.buildAuthFilter();

environment.jersey().register(new AuthDynamicFeature(basicAuthFilter));
environment.jersey().register(RolesAllowedDynamicFeature.class);
environment.jersey().register(new AuthValueFactoryProvider.Binder(User.class));

environment.jersey().register(unauthorizedHandler);

}

基本的验证器

public class BasicAuthenticator<C, P> implements Authenticator<BasicCredentials, User> {
@Override
public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException {
//do no authentication yet. Let all users through
return Optional.fromNullable(new User(credentials.getUsername(), credentials.getPassword()));
}
}

未授权处理程序

public class UnAuthorizedResourceHandler implements UnauthorizedHandler {

@Context
private HttpServletRequest request;

@Override
public Response buildResponse(String prefix, String realm) {
Response.Status unauthorized = Response.Status.UNAUTHORIZED;
return Response.status(unauthorized).type(MediaType.APPLICATION_JSON_TYPE).entity("Can't touch this...").build();
}

@Context
public void setRequest(HttpServletRequest request) {
this.request = request;
}
}

授权人

public class UserAuthorizer<P> implements Authorizer<User>{
/**
* Decides if access is granted for the given principal in the given role.
*
* @param principal a {@link Principal} object, representing a user
* @param role a user role
* @return {@code true}, if the access is granted, {@code false otherwise}
*/
@Override
public boolean authorize(User principal, String role) {
return true;
}
}

最后在您的资源中使用它

@GET
public Response hello(@Auth User user){
return Response.ok().entity("You got permission!").build();
}

关于basic-authentication - 删除向导:BasicAuth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32671392/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com