jersey 1.x 拦截器配置

时间:2023-01-16 19:34:02
1、web.xml配置
    <servlet>
        <servlet-name>Rest_Servlet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>cn.firewarm.testRestShiro.rest</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
            <param-value>cn.firewarm.testRestShiro.filter.TestFilter</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

2、TestFilter代码
public class TestFilter implements ContainerRequestFilter {
    public ContainerRequest filter(ContainerRequest request) {
        if(判断条件){
            return request;
        }else{
            Response response = Response.ok("error").status(401).type(MediaType.APPLICATION_JSON).build();  
            throw new WebApplicationException(response); // Throw new UnAuthorized 
        }
 
    }