Alex Rivera | Logout

Can Spring Security use @PreAuthorize on Spring controllers methods?

Asked 2010-06-21T18:53:25.560
77

Can Spring Security use @PreAuthorize on Spring controllers methods?

Edit
Report

1 Answer

18

If you're using Spring 3.1, you can do some pretty cool stuff with this. Take a look at https://github.com/mohchi/spring-security-request-mapping. It's a sample project that integrates @PreAuthorize with Spring MVC's RequestMappingHandlerMapping so that you can do something like:

@RequestMapping("/")
@PreAuthorize("isAuthenticated()")
public String authenticatedHomePage() {
    return "authenticatedHomePage";
}

@RequestMapping("/")
public String homePage() {
    return "homePage";
}

A request for "/" will call authenticatedHomePage() if the user is authenticated. Otherwise it will call homePage().

answered 2012-09-25T00:00:22.893

Your Answer