Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom RequestCondition's in Spring mvc 3.1

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() { /*...*/ }

}

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.

Can I define a customMethodCondition for RequestMappingHandlerMapping in the XML?

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

Any help would be greatly appreciated!

UPDATE

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

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.

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

Comments anyone?

like image 686
ori Avatar asked Oct 09 '22 04:10

ori


1 Answers

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() {
...

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() {

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)?

like image 189
hyness Avatar answered Oct 12 '22 12:10

hyness