Results 1 to 3 of 3

Thread: restrict propagation of model attributes

  1. #1
    Join Date
    Feb 2013
    Posts
    2

    Default restrict propagation of model attributes

    Hi all,

    currently I am learning spring mvc. Here is my situation...

    Lets say I have model class like this:

    Code:
    class Person {
      private String firstname;
      private String surname;
      private Integer likesCount;
      // ...setters and getters
    }
    Then I have controller to enable user to change his name...

    Code:
    @Controller
    class PersonUpdateController {
      @RequestMapping(value = "\person", method = RequestMethod.POST)
      public update(@ModelAttribute("person") Person person, ...) {
        // ...validation and save
      }
    }
    And finally I have form...

    Code:
    <f:form action="person" method="post" modelAttribute="person">
      <f:input path="firstname"/>
      <f:input path="surname"/>
      <input type="submit"/>
    </f:form>
    When user uses this form, he is able to post only firstname and surname to the backend. But technically it is possible to send in request also likesCount, and that is security issue. In php framework Yii it is possible to specify which attributes should be propagated/saved to backend by defining validation criteria on every model class. Is something like that possible in spring? I think some kind of interceptor could do the trick? Or do you have some design pattern to solve this? Thank you.

  2. #2
    Join Date
    Jan 2013
    Posts
    11

    Default

    You can do it in you Controller as:
    Code:
    @Controller
    class PersonUpdateController {
      @RequestMapping(value = "\person", method = RequestMethod.POST)
      public update(@ModelAttribute("person") Person person, ...) {
         Person newPerson = new Person();
         newPerson.setFirstname(person.getFirstname());
         newPerson.setSubmit(person.getSubmit());
        // ...validation and save
      }
    }

  3. #3
    Join Date
    Feb 2013
    Posts
    2

    Default

    Thanks for reply. I got also another useful hint here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •