セキュリティで保護されていない方法でアクセスされている場合、いくつかのメソッドを制限したいと考えています。リクエストが安全なチャネルを介して送信されたかどうかを確認する @Secure アノテーションを作成しています。ただし、リクエストの HttpContext をキャプチャするメソッド injectable を作成することはできません。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Secure {
}
public class SecureProvider<T> implements InjectableProvider<Secure, AbstractResourceMethod> {
@Override
public ComponentScope getScope() {
return ComponentScope.PerRequest;
}
@Override
public Injectable<?> getInjectable(ComponentContext componentContext,
Secure annotation,
AbstractResourceMethod method) {
return new SecureInjectable();
}
}
public class SecureInjectable<T> extends AbstractHttpContextInjectable<T> {
@Override
public T getValue(HttpContext context) {
// validation here
return null;
}
}
私は Dropwizard フレームワークを使用しているため、プロバイダーの初期化は次のように簡単です。
environment.addProvider(new SessionRestrictedToProvider<>(new SessionAuthenticator(), "MySession"));
environment.addProvider(new SecureProvider<>());
environment.setSessionHandler(new SessionHandler());
使用法:
@Resource
@Path("/account")
public class AccountResource {
@GET
@Path("/test_secure")
@Secure
public Response isSecure() {
return Response.ok().build();
}
}
この時点で、メソッドで HttpContext Injectable が機能しないと想定していますが、このアノテーションを実装するために利用できる他のオプションについて途方に暮れています。