一些控制器的Spring mvc过滤器

Tob*_*bia 7 java spring controller spring-mvc

我需要处理所有来自某些Spring控制器的请求,以获取一些请求者信息或抛出异常,如安全过滤器.

我想在Spring中有什么东西像控制器的过滤器(我不需要它适用于所有控制器,但只适用于某些人).我不想通过url请求应用此过滤器,但使用类/方法扩展或注释.

这是我的实际解决方案:

@Controller
public class MyFilteredController extends FilterController {

    @RequestMapping("/filtered")
    public void test1(HttpServletRequest req){
        InfoBean infobean=filter(req);
        //... controller job
    }

}
Run Code Online (Sandbox Code Playgroud)

使用过滤方法扩展类的控制器.

public abstract FilterController{
    protected InfoBean filter(HttpServletRequest req){
        //... filter job
        return infobean;
    }
}
Run Code Online (Sandbox Code Playgroud)

Ali*_*ani 8

我不想通过url请求应用此过滤器,但使用类/方法扩展或注释

您可以HandlerInterceptor为此目的注册.例如,您可以将过滤器应用于SomeAnnotation使用以下代码注释的所有处理程序方法:

public class CustomHandlerIntercepter extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;

            SomeAnnotation annotation = handlerMethod.getMethodAnnotation(SomeAnnotation.class);
            if (annotation != null) {
                // do stuff
            }
        }
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,您应该注册您的拦截器WebConfig:

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CustomHandlerIntercepter());
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以在spring参考文档中阅读有关拦截器的更多信息.


Ess*_*Boy 0

使用 SpringDispatcherServlet 的一切都是基于 URL 的,我不认为你可以通过控制器来完成它。

您将需要使用过滤器,查看此处的 API https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/filter/package-summary.html 您可能会想要使用 OncePerRequestFilter。

public class MyFilter extends OncePerRequestFilter{

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        // TODO Auto-generated method stub

    }

}
Run Code Online (Sandbox Code Playgroud)

然后您需要在 web.xml 中添加过滤器

    <filter>
        <filter-name>requestFilter</filter-name>
        <filter-class>com.greg.MyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>errorHandlerFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
Run Code Online (Sandbox Code Playgroud)

现在有点麻烦了,如果你想在这里获取 Spring bean,你可以创建一个包含静态的 Bridge 类。

public class Bridge {

    private static PaymentService paymentService;

    public PaymentService getPaymentService() {
        return paymentService;
    }

    public void setPaymentService(PaymentService paymentService) {
        Bridge.paymentService = paymentService;
    }

}
Run Code Online (Sandbox Code Playgroud)

如果你想在这个类中注入一些spring beans

<bean id="paymentService" class="net.isban.example.service.PaymentService" />

    <bean class="net.isban.example.util.Bridge">
        <property name="paymentService" ref="paymentService" />
    </bean>
Run Code Online (Sandbox Code Playgroud)

然后在你的过滤器中(不是弹簧类)。

PaymentService paymentService = new Bridge().getPaymentService();
Run Code Online (Sandbox Code Playgroud)

很高兴有人向我展示了一种不那么老套的方法。