【发布时间】:2014-03-18 23:33:43
【问题描述】:
首先,我在 Google 上进行了广泛的搜索,虽然看起来应该有一个修复程序,但我无法成功引用 PermissionEvaluator 内部注入的 @Bean:
在该问题的 cmets 部分,Rob Winch 提供了解决建议
要解决此问题,您可以使用 LazyInitTargetSource 代理您的 permissionEvaluator
话虽如此,我在实现发布的 XML 的基于注释的 JavaConfig 版本时遇到了麻烦。 我正在使用 Spring Boot 1.0.0.BUILD-SNAPSHOT 和 spring-boot-starter-security。
我有一个类来配置方法安全性如下:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
expressionHandler.setPermissionEvaluator(new MyPermissionEvaluator());
expressionHandler.setParameterNameDiscoverer(new SimpleParameterDiscoverer());
return expressionHandler;
}
}
还有PermissionEvaluator的开头:
public class MyPermissionEvaluator implements PermissionEvaluator {
private static final Logger LOG = LoggerFactory.getLogger(MyPermissionEvaluator.class);
@Autowired
private UserRepository userRepo;
@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
if (authentication == null || !authentication.isAuthenticated()) {
return false;
}
if (permission instanceof String) {
switch((String) permission) {
case "findUser":
return handleUserPermission(authentication, targetDomainObject);
default:
LOG.error("No permission handler found for permission: " + permission);
}
}
return false;
}
@Override
public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) {
throw new RuntimeException("Id-based permission evaluation not currently supported.");
}
private boolean handleUserPermission(Authentication auth, Object targetDomainObject) {
if (targetDomainObject instanceof Long) {
boolean hasPermission = userRepo.canFind((Long) targetDomainObject);
return hasPermission;
}
return false;
}
}
需要做什么才能从PremissionEvaluator 中获得对我的UserRepository 的引用?我尝试了各种解决方法,但都没有成功。好像没有什么可以把@Autowired变成PermissionEvaluator...
【问题讨论】:
标签: spring spring-security spring-aop spring-boot