Java 如何要求Spring应用程序中的所有请求处理程序都具有@PreAuthorize

Java 如何要求Spring应用程序中的所有请求处理程序都具有@PreAuthorize,java,spring,security,spring-security,Java,Spring,Security,Spring Security,我希望将Spring安全应用程序切换到注释,但我希望在允许外部调用它之前,确保每个请求都有自己的@PreAuthorize注释 是否可以为此设置Spring Security策略?据我所知,无法定义此类策略 但您可以设置一个Spring MVC拦截器,该拦截器将在运行时检查相应处理程序方法上是否存在预授权注释: public class PreAuthorizeChecker implements HandlerInterceptor { @Override public bo

我希望将Spring安全应用程序切换到注释,但我希望在允许外部调用它之前,确保每个请求都有自己的
@PreAuthorize
注释


是否可以为此设置Spring Security策略?

据我所知,无法定义此类策略

但您可以设置一个Spring MVC拦截器,该拦截器将在运行时检查相应处理程序方法上是否存在预授权注释:

public class PreAuthorizeChecker implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod hm = (HandlerMethod) handler;
            PreAuthorize annotation = AnnotationUtils.findAnnotation(hm.getMethod(), PreAuthorize.class);
            if (annotation == null) {
                // prevent access to method wihout security restrictions
                throw new RuntimeException("Rights are not defined for this handler");
            }

        }
        return true;
    }
.....
}

谢谢-这正是我想要的。