在Spring MVC 3.1中添加自定义RequestCondition [英] Adding custom RequestCondition's in Spring mvc 3.1

查看:462
本文介绍了在Spring MVC 3.1中添加自定义RequestCondition的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring mvc(3.1.1)应用程序,我想定义超出RequestMapping可用条件的条件.我有几件事要用它.

I have a Spring mvc (3.1.1) app, and I want to define conditions beyond what's available in RequestMapping. I have a couple of things I want to use it for.

首先,如果我可以针对不同的用户类型显示不同的主页,那就太好了

First, it would be nice if I could show a different home page for different user types:

@Controller
public class HomepageController {

    @RequestMapping(value = "/")
    @CustomCondition(roles = Guest.class)
    public String guestHome() { /*...*/ }

    @RequestMapping(value = "/")
    @CustomCondition(roles = Admin.class)
    public String adminHome() { /*...*/ }

}

第二,我希望该应用程序既可以充当网站,又可以作为REST服务(例如,用于移动应用程序),因此我想让网站同时访问html和json操作,并让该服务(不同子域)仅访问json操作(某种仅与网站网址匹配的@CustomCondition(web = true)类型)

Second, I want the app to function both as a web site and as a REST service (e.g. for mobile apps), so I'd want to let the website access both html and json actions, and let the service (different subdomain) only access json actions (some kind of @CustomCondition(web = true) which only matches website urls)

此功能可以在我计划的两种用途中使用吗?

Can this work for any of the two uses I'm planning?

我发现很少有关于自定义条件的文档,但确实找到了一个示例

I found very little documentation about custom conditions, but I did find one example that implements custom conditions which might be what I want, but it uses a @Configuration class instead of the XML configuration which I'm using and I don't want to move my entire spring xml definitions to a @Configuration class.

我可以在XML中为RequestMappingHandlerMapping定义一个customMethodCondition吗?

Can I define a customMethodCondition for RequestMappingHandlerMapping in the XML?

我尝试对RequestMappingHandlerMapping进行子类化并覆盖getCustomMethodCondition,以返回我的自定义RequestCondition,但是它不起作用-在我的情况下getMatchingCondition()没有触发.

I tried subclassing RequestMappingHandlerMapping and override getCustomMethodCondition, to return my custom RequestCondition, but it didn't work - getMatchingCondition() in my condition didn't fire.

任何帮助将不胜感激!

更新

我读了一些,看来RequestMappingHandlerMapping是一个新类(从3.1版开始).

I read a little more, and it looks like RequestMappingHandlerMapping is a new class (since ver 3.1).

在我的应用程序中发生的事情是,尝试覆盖并因此重新定义requestMappingHandlerMapping bean的@Configuration实际上有效,但是url映射(@Controller中的@RequestMapping方法)似乎已得到处理.两次,一次是由子类ExtendedRequestMappingHandlerMapping,一次是由原始RequestMappingHandlerMapping –首先是具有自定义条件,然后又没有条件.

What happens in my app is that the @Configuration that tries to override and thereby redefine the requestMappingHandlerMapping bean actually works, but the url mappings (@RequestMapping methods in @Controllers) seem to get processed twice, once by the subclass ExtendedRequestMappingHandlerMapping and once by the original RequestMappingHandlerMapping --first with a custom condition, and then again without it.

最重要的是我的自定义条件被忽略了.

Bottom line is my custom conditions are simply ignored.

这应该是高级模式,但是IMO应该很普遍...

This is supposed to be an advanced pattern, but IMO it should be quite common...

评论任何人吗?

推荐答案

Spring MVC已经提供了一种区分json和html的机制,RequestMapping批注采用了枚举属性,它可以查看请求的内容类型...

Spring MVC already provides a mechanism for distinguishing between json and html, the RequestMapping annotation takes a consumes attribute which looks at the content type of the request...

// REST version, Content-type is "application/json"
@RequestMapping(value = "/", consumes = "application/json")
public void myRestService() {
...

// HTML version, Content-type is not "application/json"
@RequestMapping(value = "/", consumes = "!application/json")
public void myHtmlService() {
...

使用相同网址但具有不同方法的另一种方法是使用param或headers属性...

Another way to use the same url but have distinct methods is with the param or headers attribute...

// the url is /?role=guest
@RequestMapping(value = "/", param = "role=guest")
public void guestService() {

// the url is / with header role=admin
@RequestMapping(value = "/", headers = "role=admin")
public void adminService() {

我想您会希望使用不同的URL来确保安全性.通常,使用Spring Security之类的东西,您可以将所有管理功能放在/admin下,并让框架对其进行全部管理...

I would think you would want distinct urls for security. Typically, with something like Spring Security, you would put all of the admin functionality under /admin and let the framework manage it all...

<http auto-config="true">
  <intercept-url pattern="/admin/**" access="ROLE_ADMIN" />
...

这足以满足您的用例吗?

Would this be sufficient for your use case(s)?

这篇关于在Spring MVC 3.1中添加自定义RequestCondition的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆