要求フィルターの作業を CDI Bean に委任しようとしています。しかし、Glassfish 4.0 では、RestURL を呼び出すと例外が発生します。
フィルター定義
@Provider
@Authenticated
public class AuthenticationFilter implements ContainerRequestFilter{
@Inject
private AuthenticationService authenticationService;
@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
String token = containerRequestContext.getHeaderString("x-authentication-token");
if (!authenticationService.isAuthenticated(token)) {
containerRequestContext.abortWith(Response.serverError().entity("No authentication").build());
}
}
}
特定のリソース URL にタグ付けできるようにするための NameBinding の定義。
@NameBinding
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Authenticated {
}
そして、CDI Bean 定義。
@ApplicationScoped
public class AuthenticationService {
public boolean isAuthenticated(String token) {
boolean result = false;
if (token != null && token.length() > 0) {
....
}
return result;
}
}
そして、WEB-INF ディレクトリに bean.xml ファイルがあります。
<beans
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>
ありがとうルディ