Tag - 拦截器

jersey java 拦截器 微服务 MicroService    2017-07-19 21:37:32    1160
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 UnAuthor