Errore in Iniezione delle dipendenze con Jersey, Maven e Java

Sto cercando di creare un servizio di riposo Un filtro per ogni richiesta effettuata, questo filtro che farà è verificare se c’è un token, se questo è corretto continuò, altrimenti restituirà un UNAUTHORIZED. Il problema è che cerco di iniettare una dipendenza dal mio filtro, ma mi dà il seguente errore:

GRAVE: Exception during lifecycle processingjava.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=GeneradorDeTokens,parent=JWTTokenNeededFilter,qualifiers={},position=-1,optional=false,self=false,unqualified=null,270994059) at com.sun.enterprise.web.WebApplication.start(WebApplication.java:136) at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122) at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:294) at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:357) at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:501) at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:220) at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:508) at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:544) at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:540) at java.security.AccessController.doPrivileged(Native Method) at javax.security.auth.Subject.doAs(Subject.java:360) at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:539) at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:570) at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:562) at java.security.AccessController.doPrivileged(Native Method) at javax.security.auth.Subject.doAs(Subject.java:360) at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:561) at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1469) at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:111) at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1851) at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1727) at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534) at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224) at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189) at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:483) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:180) at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206) at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180) at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235) at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201) at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112) at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539) at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573) at java.lang.Thread.run(Thread.java:745)

Apparentemente la dipendenza non può essere soddisfatta. Il codice che uso è il seguente:

Filtro:

@[email protected]@Priority(Priorities.AUTHENTICATION)public class JWTTokenNeededFilter implements ContainerRequestFilter { @Inject private TokenFactory tokenFactory; @Override public void filter(ContainerRequestContext requestContext) throws IOException { //Obteniendo los encabezados HTTP Authorization de la peticion String encabezadosAut = requestContext.getHeaderString(HttpHeaders.AUTHORIZATION); //Extrayendo el token de los encabezados HTTP Authorization String token = encabezadosAut.substring("Bearer".length()).trim(); try { //Validando el token Key key = tokenFactory.generarToken(); Jwts.parser().setSigningKey(key).parseClaimsJws(token); } catch (ExpiredJwtException | UnsupportedJwtException | MalformedJwtException | SignatureException | IllegalArgumentException e) { requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).build()); } }}

La classe che voglio iniettare

import java.security.Key;import javax.crypto.spec.SecretKeySpec;/** * */public class GeneradorDeTokens implements TokenFactory{ @Override public Key generarToken(){ String llave="simplekey"; Key key=new SecretKeySpec(llave.getBytes(),0,llave.getBytes().length,"DES"); return key; }}

L’interfaccia utilizzata dalla classe

import java.security.Key;/** * */public interface TokenFactory { Key generarToken();}

il tag personalizzato

import static java.lang.annotation.ElementType.METHOD;import static java.lang.annotation.ElementType.TYPE;import java.lang.annotation.Retention;import static java.lang.annotation.RetentionPolicy.RUNTIME;import java.lang.annotation.Target;import javax.ws.rs.NameBinding;/** * */@[email protected](RUNTIME)@Target({TYPE,METHOD})public @interface JWTokenNeeded {}

Ho seguito un tutorial per la convalida e la creazione di JWT, e sono stato bloccato in questa parte.

Leave a Comment

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *