- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用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/
我正在尝试将 go 应用程序部署到 Google App Engine,但由于此错误导致编译失败。 request.BasicAuth undefined (type *http.Request ha
我正在尝试使用express-basic-auth对express应用程序中的端点进行一些基本授权,但它一直给我一个401未经授权的错误。我认为我在 Post man 中发送的 header 不正确:
我正在使用 node、express 和 connect 来构建一个简单的应用程序,并按如下方式实现基本的 HTTP 身份验证(为简洁起见,省略了大量代码): var express = requir
我正在将 Express 从 3.x 迁移到 4.x,由于 Express 4 没有 basicAuth,我尝试将其替换为 basic-auth module . 我遇到的问题是异步检查凭据(通过我的
使用Dropwizard Authentication 0.9.0-SNAPSHOT 我想检查数据库用户 (UserDAO) 的凭据。 我收到以下异常 ! org.hibernate.Hibernat
我正在尝试使用 token 实现oauth。一切看起来都很好,但是在 POST 之后 http://localhost:8080/oauth/token?grant_type=password 设置B
喷雾很难!!我现在知道我对 HTTP 协议(protocol)的了解还远远不够,API 设计也不容易。然而,我仍然非常希望我的练习应用程序能够工作。我正在为 POST/PUT/DELETE 方法编写此
假设我已经使用 BasicAuth 启用了对资源的身份验证: class MyBasicAuth(BasicAuth): def check_auth(self,username,passwo
即使我使用 Tornado 发送 401 状态代码,我也没有在 IE/Firefox 中看到密码提示:- import tornado.ioloop import tornado.web class
我开始构建一个非常小的 API,我需要验证一些端点,而不是所有端点。我希望能够选择使用要在“路由”正文中调用的 authenticate! 方法来强制验证的端点。 例如: resource :grou
我正在尝试为我的新手项目实现基本身份验证(来自 Spring Security)。为了创建用户,我将 JSON 发送到 /register 处的 POST,然后创建用户并将其放入 H2 内存数据库中
我正在运行 prerender server一切正常,但现在我想使用 basicAuth 设置一些安全性. 在我的控制台中,我导出了用户名和密码 export BASIC_AUTH_USERNAME=
我的 NodeJS 服务器,使用express,有一堆条目来指定各种路由: app.post('list_streams.json', auth, stream_handler.list_str
我正在尝试使用 Flask 和 HTTP Basic Auth 创建登录系统。我的问题是,我有责任从数据库提供用户信息,还是 basicauth 为我创建和访问这些数据库?如果没有,我可以用什么来做到
我对在 Yii2 中创建 REST api 的简单程度印象深刻。但是,我在理解基本身份验证时遇到了一些麻烦。我的需求非常简单,我希望我的解决方案能够效仿。 我在这里需要基本 token 身份验证。我现
我刚刚使用 express basic auth 在 nodejs 中创建了基本身份验证 var express = require('express'); var app = express();
为了使用基本身份验证来保护整个应用程序,这种方式可以为所有路由器做: package main import ( "crypto/subtle" "net/http" "git
最近我听说 Twitter 将关闭 Twitter API 上的基本身份验证,他们转向 OAuth。 所以我想知道 BasicAuth、OAuth 和 XAuth 之间有什么区别? 每个 Auth 的
在我的设置中,我有一个向我的系统发送 Http 请求的上游系统。这些 Http 请求的 header 中包含 basicAuth token 。 我正在使用 Spring-boot 和外部 tomca
登录认证的路由——app.Handle("GET", "/v1/users/token", u.Token) . 我们可以从 request.BasicAuth 中获取名称和密码。 func (u *
我是一名优秀的程序员,十分优秀!