【问题标题】:Can I send a java annotation at he method level, in a MVC controller method, the http request as a parameter?我可以在方法级别,在 MVC 控制器方法中,将 http 请求作为参数发送 java 注释吗?
【发布时间】:2020-02-05 05:36:49
【问题描述】:

我有一个标准的 Spring MVC Web 应用程序,我想添加一个带有参数和 HttpRequest 的自定义注释。

现在我知道注解参数是在编译时解析的,但是 Spring 安全性如何通过 @PreAuth 和其他东西访问会话和用户和东西...

我特别想获得 HttpRequest。

想出如何解决这个问题?

【问题讨论】:

  • 您正在寻找HandlerMethodArgumentResolver,但如果已经有可用的实现,您可能想说明您想要做什么。
  • 寻找 HttpRequest

标签: java spring-mvc annotations


【解决方案1】:

你应该实现PermissionEvaluator

public class CustomPermissionEvaluator implements PermissionEvaluator {


public boolean hasPermission(Authentication authentication, Object target,
        Object permission) {

        return // you logic;
}


public boolean hasPermission(Authentication authentication, Serializable targetId,String targetType, Object permission) {
        return // you logic;

}

}

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {

@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
    DefaultMethodSecurityExpressionHandler expressionHandler = 
      new DefaultMethodSecurityExpressionHandler();
    expressionHandler.setPermissionEvaluator(new CustomPermissionEvaluator());
    return expressionHandler;
}



@PreAuthorize("hasPermission(#foo, 'YOU_CUSTOM_PARAM')")
@PostMapping(value = "/pay")
public Foo create(Foo foo) {
    return foo;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 2016-09-04
    • 2012-11-06
    • 1970-01-01
    相关资源
    最近更新 更多