我正在研究:使用 JAX-RS 和 Jersey 进行基于 REST 令牌的身份验证的最佳实践
但是我的过滤器没有触发,我的呼叫直接传递到端点......
我的安全界面:
@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface Secure {
}
我的过滤器:
@Secure
@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthenticationFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
// Get the HTTP Authorization header from the request
String authorizationHeader =
requestContext.getHeaderString(HttpHeaders.AUTHORIZATION);
// Check if the HTTP Authorization header is present and formatted correctly
if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) {
throw new NotAuthorizedException("Authorization header must be provided");
}
// Extract the token from the HTTP Authorization header
String token = authorizationHeader.substring("Bearer".length()).trim();
try {
Token tk = new Token();
tk.validarToken(token);
} catch (Exception e) {
requestContext.abortWith(
Response.status(Response.Status.UNAUTHORIZED).build());
}
}
我的端点:
package api;
import filters.Secure;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
@Path("service")
public class Service {
@GET
@Secure
@Path("/sapo")
@Produces("application/json")
@Consumes("application/json")
public Response mySecuredMethod() {
return Response.ok("sapo").build();
}
}
还有我的 web.xml(没关系??):
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>api</param-value>
</init-param>
<init-param>
<param-name>import javax.ws.rs.container.ContainerRequestFilter</param-name>
<param-value>filters.AuthenticationFilter;api.Service</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
我在堆栈中阅读了很多相关的问题,但找不到错误。
有人知道吗?
提前致谢。