Spring: controller inheritance using @Controller annotation
Asked Answered
E

3

8

I'd like to be able to create a base controller in my Spring app that, among other things, determines if a user is a registered user or not. This base controller, following the template design pattern, would contain an abstract protected method that controller subclasses would implement.

The abstract method would have passed to it an instance of User, registered or otherwise. However, I have no idea how I would do this since it seems that by using controllers purely using the @Controller annotation each controller is free to define their request handling method however they like.

Would creating some sort of user service class that is injected into each controller and used to validate a user be one way to get around this? This begs the question (at least for me) how does such a controller get a hold of a HttpServletRequest or the Session object?

Thanks.

Eucalyptus answered 27/1, 2010 at 5:10 Comment(0)
A
1

I think the Base Controller is not a good idea if the only code it is to have is for UserAuthentication...instead use Spring security. This is the best option.

Alternatively, you can have methods like this...take a look at the Spring reference..

@Controller("loginController")
public class LoginController {    

   @RequestMapping(value="/login.do", method=RequestMethod.POST)
   public String login(Model model, HttpServletRequest request) {

      String userIdFromRequest = (String)request.getParameter("userId");
      String password = (String)request.getParameter("password");

      boolean verified = ...send userIdFromRequest and password to the user service for 
      verification...

      if (verified){
        request.getSession().setAttribute("userId", userIdFromRequest);
      }

   }          

   //More Methods

}

Did it help?

-SB

Asteriated answered 27/1, 2010 at 5:48 Comment(1)
Thank you SB, I've considered that but just collecting some opinions on this right now. I am seriously considering Spring Security but also want to avoid code duplication everywhere -- I need to read up on Spring Security though.Eucalyptus
L
15
  1. Define an abstract BaseController, with no annotations
  2. Define concrete and abstract methods
  3. Call these methods from subclasses (which are annotated with @Controller) whenever needed.
Latinize answered 27/1, 2010 at 7:27 Comment(2)
Thanks Bozho, that's sort of what I was thinking. I appreciate the response.Eucalyptus
But what if some concrete methods in the BaseController needs to access some autowired services ? I imagine a BaseController that have a common @Autowired private UserService userService. Does this mean that the abstract BaseController should be annotated with @Controller too to make the autowiring works ?Sharpshooter
A
1

I think the Base Controller is not a good idea if the only code it is to have is for UserAuthentication...instead use Spring security. This is the best option.

Alternatively, you can have methods like this...take a look at the Spring reference..

@Controller("loginController")
public class LoginController {    

   @RequestMapping(value="/login.do", method=RequestMethod.POST)
   public String login(Model model, HttpServletRequest request) {

      String userIdFromRequest = (String)request.getParameter("userId");
      String password = (String)request.getParameter("password");

      boolean verified = ...send userIdFromRequest and password to the user service for 
      verification...

      if (verified){
        request.getSession().setAttribute("userId", userIdFromRequest);
      }

   }          

   //More Methods

}

Did it help?

-SB

Asteriated answered 27/1, 2010 at 5:48 Comment(1)
Thank you SB, I've considered that but just collecting some opinions on this right now. I am seriously considering Spring Security but also want to avoid code duplication everywhere -- I need to read up on Spring Security though.Eucalyptus
H
0

The basic problem is that annotational bootstrapping is not polymorphic. I found this paper useful: http://sanguinecomputing.com/design-pattern-for-hierarchical-controller-organization-with-annotational-configuration-spring-mvc-3/

Homely answered 28/4, 2013 at 19:53 Comment(1)
It would be useful if you could summarise the paper here.Manville

© 2022 - 2024 — McMap. All rights reserved.