I am using Java 8, Spring MVC 4, Spring Boot, and Gradle for my REST application.
I would like to add security to my REST application via custom method annotations within certain Spring MVC 4 controllers.
Here is a basic example.
HomeController.java
package myapp;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
@RequestMapping("/")
public class HomeController {
@RequestMapping("/")
public String index() {
return "<h1>Hello, World!</h1><p>Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum</p>";
}
@CustomSecurityAnnotation
@RequestMapping("/secure")
public String secure() {
return "<h1>Secured!</h1><p>Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum</p>";
}
}
I would like CustomSecurityAnnotation
to run a custom method that will check the headers of the incoming REST request, look at HTTP header Authorization
, pull the value provided (if one was provided), and run code against that value to validate the request before allowing the method to proceed. The annotation should have the ability to override the response and provide a HTTP 401 or 403 if the authorization is invalid, and allow the method under the annotation to run if I decide the annotation custom method passed successfully.
I realize there is something similar I could do with PreAuthorize
and other MVC annotations but I'm looking at packaging up custom logic within a method inside a single annotation to be used on any method on any controller of my choosing.
Thanks!