Spring security 在springsecurity@PreAuthorize中使用其他bean和方法
我想在Spring Security中对SpEL使用Spring security 在springsecurity@PreAuthorize中使用其他bean和方法,spring-security,Spring Security,我想在Spring Security中对SpEL使用@PreAuthorize,例如 但是,在SpringSecurity4.1.4中使用它时,这对我来说不起作用。下面是我的示例代码: bean类: package com.service.spel; 导入org.springframework.stereotype.Component; @组件(value=“accessBean”) 公共类AccessCheckBean{ 公共字符串getPermission(){ /** *返回api权限 *
@PreAuthorize
,例如
但是,在SpringSecurity4.1.4中使用它时,这对我来说不起作用。下面是我的示例代码:
bean类:
package com.service.spel;
导入org.springframework.stereotype.Component;
@组件(value=“accessBean”)
公共类AccessCheckBean{
公共字符串getPermission(){
/**
*返回api权限
*/
字符串scope=“#oauth2.hasScope('resource.Update')”;
返回范围;
}
}
在控制器中:
@PreAuthorize(“accessBean.getPermission()”)
@GetMapping(“/perm”)
public@ResponseBody字符串getPerm(){
返回“烫发”;
}
错误消息:
无法计算表达式“accessBean.GetPermission()”的值
似乎我不能像上面那样使用SpEL,那么如果我使用的是此版本的Spring Security,我如何继续?您必须使用
@
,请参阅:
在Web安全表达式中引用bean
如果希望扩展可用的表达式,可以很容易地引用您公开的任何Springbean。例如,假设您有一个名为webSecurity
的Bean,其中包含以下方法签名:
公共类网站安全性{
公共布尔检查(身份验证、HttpServletRequest){
...
}
}
您可以使用以下方法参考该方法:
...
或者在Java配置中
http
.授权请求()
.antMatchers(“/user/**”).access(@webSecurity.check(身份验证,请求)”)
...
因此OP的控制器方法的SpEL是相同的:
@webSecurity.check(authentication,request)
?@hc\u dev:它将是@PreAuthorize(@accessBean.getpermission())
。