Java Spring boot@PreAuthorize(是否可以修改@PreAuthorize中方法的输入,然后传递到方法中)
我有一个带有@PreAuthorize(…)注释的方法,其中包含一些逻辑,可以查询API以获取有关用户可以查看的内容的信息。但是,我有一个端点,我需要添加这个@PreAuthorize注释,它接收一个更“复杂”的对象,我想稍微变换一下(该对象包含一个数组,在某些情况下我想添加/删除数据)Java Spring boot@PreAuthorize(是否可以修改@PreAuthorize中方法的输入,然后传递到方法中),java,spring,spring-boot,Java,Spring,Spring Boot,我有一个带有@PreAuthorize(…)注释的方法,其中包含一些逻辑,可以查询API以获取有关用户可以查看的内容的信息。但是,我有一个端点,我需要添加这个@PreAuthorize注释,它接收一个更“复杂”的对象,我想稍微变换一下(该对象包含一个数组,在某些情况下我想添加/删除数据) @PostMapping(“/search”) @预授权(@Service.isAuth(#searchParam)“) 公共响应搜索(SearchParams searchParam){ 返回服务.getSe
@PostMapping(“/search”)
@预授权(@Service.isAuth(#searchParam)“)
公共响应搜索(SearchParams searchParam){
返回服务.getSearchResult(searchParam);
}
是否有一种方法可以在@PreAuthorize注释中修改
searchParam
,然后将其传递到方法体中,我知道这可能不是正确的方法,也可能不是@PreAuthorize设计的目的,但即使使用不同类型的注释,也有任何方法可以做到这一点。显然,在最坏的情况下,我可以将逻辑移到方法体中,但如果可能的话,我更喜欢使用基于注释的解决方案,如@PreAuthorize。感谢您的帮助,即使是其他相关内容的链接也会很有用。我在谷歌上没有找到太多与此相关的内容。我认为最好的解决方案是制作一个处理程序/拦截器,然后用@PreAuthorize对其进行注释。因此,我认为您的思路是正确的,但是您需要确保修改代码以实现HandlerMapping接口来创建拦截器,然后重写预处理方法。在你需要用@PreAuthorize对它进行编程注释之后。最后一件事是使用包装器修改HttpWrapper,它不能手动完成。此处按顺序链接到相关资源:
- 创建处理程序/拦截器:
- 在拦截器中使用预授权:
- 要修改HttpServlet请求,您需要一个包装器:
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);
//TODO use the technique shown on the third link to wrap and modify the HttpServletRequest
if (annotation == null) {
// prevent access to method wihout security restrictions
throw new RuntimeException("Rights are not defined for this handler");
}
}
return true;
}
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);
//TODO use the technique shown on the third link to wrap and modify the HttpServletRequest
if (annotation == null) {
// prevent access to method wihout security restrictions
throw new RuntimeException("Rights are not defined for this handler");
}
}
return true;
}