Error en injeccio de dependències amb jersei , maven i java

Estic intentant crear un servei REST el qual tingui un filtre per a cada petició que es realitzi, aquest filtre el que farà és verificar si hi ha un testimoni , si això és correcte continués , cas contrari retornarà un UNAUTHORIZED . El problema és que intent injectar una dependència en el meu filtre , però em dóna el següent error :

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)

A l’ sembla la dependència no pot ser satisfeta. El codi que utilitzo és el següent :

FILTRE :

@[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 QUE DESIG INJECTAR

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; }}

LA INTERFÍCIE UTILITZADA PER LA CLASSE

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

EL TAG PERSONALITZAT

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 {}

he seguit un tutorial per a la validació i creació de JWT , i he quedat encallat en aquesta part .

Leave a Comment

L'adreça electrònica no es publicarà. Els camps necessaris estan marcats amb *