Java 什么安全注释可用于允许仅匿名访问端点?

Java 什么安全注释可用于允许仅匿名访问端点?,java,jersey,jersey-2.0,dropwizard,Java,Jersey,Jersey 2.0,Dropwizard,我有一个注册端点,我只希望匿名用户能够访问它。换句话说,我只希望未经身份验证的用户能够发布到端点。做这件事最好的方法是什么 @Path("/accounts") public class AccountResource { @Inject private AccountService accountService; @DenyAll @POST public void register(CreateAccountJson account) {

我有一个注册端点,我只希望匿名用户能够访问它。换句话说,我只希望未经身份验证的用户能够发布到端点。做这件事最好的方法是什么

@Path("/accounts")
public class AccountResource {

    @Inject
    private AccountService accountService;

    @DenyAll
    @POST
    public void register(CreateAccountJson account) {
        try {
            accountService.registerUserAndCreateAccount(account.getEmail(),
                account.getPassword());
        } catch (RegistrationException e) {
            throw new BadRequestException(e.getMessage());
        }
    }
}

没有这样的注释。这个用例实际上不符合授权的语义。您可以使用的一个解决方法是注入
SecurityContext
。只需检查是否有
主体
。如果没有,则没有经过身份验证的用户。如果有,那么你可以发送一个404

@POST
public void register(@Context SecurityContext context, CreateAccountJson account) {

    if (context.getUserPrincipal() != null) {
        throw new NotFoundException();
    }
    ...
}
更新 如果您有很多这样的资源方法,那么最好使用名称绑定的过滤器。比如说

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface NonAuthenticated {}

@NonAuthenticated
// Perform before normal authorization filter
@Priority(Priorities.AUTHORIZATION - 1)
public class NonAuthenticatedCheckFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext request) {
        final SerurityContext context = request.getSecurityContext();
        if (context.getUserPrincipal() != null) {
            throw new ForbiddenException();
        }
    }
}

@POST
@NonAuthenticated
public void register(CreateAccountJson account) { }

// register the Dw
environment.jersey().register(NonAuthenticatedCheckFilter.class);

有关Jersey过滤器的更多信息,请参阅Jersey文档。

没有此类注释。这个用例实际上不符合授权的语义。您可以使用的一个解决方法是注入
SecurityContext
。只需检查是否有
主体
。如果没有,则没有经过身份验证的用户。如果有,那么你可以发送一个404

@POST
public void register(@Context SecurityContext context, CreateAccountJson account) {

    if (context.getUserPrincipal() != null) {
        throw new NotFoundException();
    }
    ...
}
更新 如果您有很多这样的资源方法,那么最好使用名称绑定的过滤器。比如说

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface NonAuthenticated {}

@NonAuthenticated
// Perform before normal authorization filter
@Priority(Priorities.AUTHORIZATION - 1)
public class NonAuthenticatedCheckFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext request) {
        final SerurityContext context = request.getSecurityContext();
        if (context.getUserPrincipal() != null) {
            throw new ForbiddenException();
        }
    }
}

@POST
@NonAuthenticated
public void register(CreateAccountJson account) { }

// register the Dw
environment.jersey().register(NonAuthenticatedCheckFilter.class);
有关Jersey过滤器的更多信息,请参阅Jersey文档