Implementing Permission Validation in Spring MVC Using Custom Annotations

This article explains how to implement permission validation in Spring MVC by defining user, role, and resource tables, exploring three approaches—including request URI checks, method inspection, and custom annotations—and provides complete Java code examples for a custom annotation, controller, and interceptor to enforce access control.

Top Architect
Top Architect
Top Architect
Implementing Permission Validation in Spring MVC Using Custom Annotations

The article discusses permission validation in a Spring MVC project, describing the typical user, role, and resource tables with many‑to‑many relationships and the need to verify a user's authority in an interceptor after a request arrives.

It outlines three possible methods: (1) checking the request URI against allowed operations, which is cumbersome; (2) inspecting the handler method name and its RequestMapping annotation; and (3) defining a custom annotation to mark operations.

Example of a method‑level custom annotation:

@Retention(RUNTIME)
@Target(METHOD)
public @interface MyOperation {
    String value() default ""; // default empty
}

Controller using the annotation:

@Controller("testController")
public class TestController {
    @MyOperation("用户修改") // main point
    @RequestMapping("test")
    @ResponseBody
    public String test(String id) {
        return "Hello,2018!" + id;
    }
}

Interceptor that reads the annotation:

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    System.out.println("进入拦截器");
    if (handler instanceof HandlerMethod) {
        HandlerMethod h = (HandlerMethod) handler;
        System.out.println("用户想执行的操作是:" + h.getMethodAnnotation(MyOperation.class).value());
        // perform permission check here
    }
    return HandlerInterceptor.super.preHandle(request, response, handler);
}

For class‑level annotations, the same @interface can target TYPE and be retrieved with

h.getMethod().getDeclaringClass().getAnnotation(MyOperation.class)

. The article also notes that using RequestMapping directly (instead of shortcuts like GetMapping) can avoid the need for a custom annotation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavapermissionCustom AnnotationInterceptorSpring MVC
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.