Skip to content
This repository was archived by the owner on May 30, 2022. It is now read-only.
This repository was archived by the owner on May 30, 2022. It is now read-only.

jax.rs 包原生的@Provider无效,自定义rest api annotation不生效 #189

@JinAirsOs

Description

@JinAirsOs

发布一个facade,定义interface,然后自己新建了一个Auth的annotation,但是发现不生效
@get
@path("user/{id}")
@Auth
Result getUser(@PathParam("id") String id);
annotation的定义在这
@namebinding
@retention(RUNTIME)
@target({METHOD, TYPE})
public @interface Auth {
}
@Provider
@Auth
@priority(Priorities.AUTHENTICATION)
public class JWTAuthFilter1 implements ContainerRequestFilter {

private Logger logger = LoggerFactory.getLogger(JWTAuthFilter1.class);
@Resource
private Environment environment;

private String secret;

private UserDAO userDAO;

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {

    // Get the HTTP Authorization header from the request
    String authorizationHeader = requestContext.getHeaderString(HttpHeaders.AUTHORIZATION);

    // Extract the token from the HTTP Authorization header
    String token = authorizationHeader.substring("Bearer".length()).trim();

    try {

        secret = environment.getProperty("spring.application.secret");

        Claims claims = JWT.parseJWT(token,secret);
        logger.info("valid token : " + token);
        Date now = new Date();
        if(claims.getExpiration().after(now)) {
            throw new Exception("token expired");
        }
        String userId = claims.getId();
        Long id = Long.parseLong(userId);
        Optional userOptional = userDAO.findById(id);
        if(!userOptional.isPresent()){
            //no such user
            throw new Exception("token invalid");
        } else {
            requestContext.setProperty("user",userOptional.get());
        }

    } catch (Exception e) {
        logger.info("invalid token : " + token);
        requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
    }
}

}

我实际测试过,在jax.rs 空项目中定义这个annotation是会生效的,但是没法在sofaboot里生效?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions