Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java dropwizard中的身份验证流_Java_Rest_Authentication_Dropwizard - Fatal编程技术网

Java dropwizard中的身份验证流

Java dropwizard中的身份验证流,java,rest,authentication,dropwizard,Java,Rest,Authentication,Dropwizard,我已经通过许多论坛了解了流程,但仍然混淆了正确的流程 我正在使用Dropwizard,首先我想从RESTAPI获取令牌(用户名和密码将在基本身份验证中提供),然后下次每次请求都会传递该令牌 主类 environment.jersey() .register( new AuthDynamicFeature( new JwtAuthFilter.Bu

我已经通过许多论坛了解了流程,但仍然混淆了正确的流程

我正在使用Dropwizard,首先我想从RESTAPI获取令牌(用户名和密码将在基本身份验证中提供),然后下次每次请求都会传递该令牌

主类

    environment.jersey()
                .register(
                        new AuthDynamicFeature(
                                new JwtAuthFilter.Builder<User>()
                                        .setAuthenticator(new MarginCalcAuthenticator())
                                        .setAuthorizer(
                                                new CalcAuthorizer())
                                        .setRealm("BASIC-AUTH-REALM")
                                        .buildAuthFilter()));
environment.jersey().register(RolesAllowedDynamicFeature.class);
        environment.jersey().register(new AuthValueFactoryProvider.Binder<User>(User.class));
@GET
    @Path("token")
    @Produces(MediaType.TEXT_PLAIN)
    public Response genToken(@Context SecurityContext sc){
        return Response
                .ok()
                .header("Authorization", "Bearer "+AdminAuthenticationService.issueToken((br.dc.auth.User) sc
                        .getUserPrincipal())).build();
    }

我正在从Postman调试,它正在命中我的API genToken,但它从未到达JwtAuthFilter或CalAuthenticator。有人能帮我理解这个流程吗?我想了解流程。

正如Paul提到的,身份验证需要使用@RoleAllowed(或任何其他authz anno)注释的类或方法。身份验证只在您告诉它的方法(或类)上完成

流量
在环境中注册您的过滤器、验证器等->启动服务器->从UI或邮递员处请求令牌->它将命中您的AuthFilter->您可以调用验证器进行令牌验证->验证您的请求并相应地发送响应

是否允许使用
@RolesAllowed
(或任何其他authz anno)注释类或方法?验证只在您告诉它的方法(或类)上完成。谢谢Paul。我忘记加了。现在它正在点击AuthFilter,我可以进行身份验证,但我仍然对流感到困惑。它只点击了AuthFilter,但我仍然不知道CalAuthenticator什么时候会接到呼叫。我可以只使用CalAuthenticator还是只使用AuthFilter?你更喜欢哪一个?如果两者都需要,那么您需要调用
AuthFilter
authenticate()
方法。看看这个和。您应该了解这个流程,以及您自己的authfilter实现需要做什么。感谢您提供这些详细信息。
public class CalcAuthenticator implements Authenticator<JWTCredentials, User> {

    public Optional<User> authenticate(JWTCredentials credentials)
            throws AuthenticationException {
        AdminAuthenticationService authService = new AdminAuthenticationService();

        User userObj = authService.authenticate(credentials.getJwtToken());
        if (userObj == null) {
            throw new WebApplicationException(Status.UNAUTHORIZED);
        }
        return Optional.of(userObj);
    }

}
@GET
    @Path("token")
    @Produces(MediaType.TEXT_PLAIN)
    public Response genToken(@Context SecurityContext sc){
        return Response
                .ok()
                .header("Authorization", "Bearer "+AdminAuthenticationService.issueToken((br.dc.auth.User) sc
                        .getUserPrincipal())).build();
    }