我正在尝试创建一个仅对包含用户角色的单个请求有效的bean。在调用任何控制器方法之前,我将在@Around方法中填充角色。然后,我需要稍后访问这些角色以进行其他授权检查。
@Component
@Aspect
public class SecurityAudit {
@Autowired
private CurrentRoles currentRoles;
@Around("@annotation(requestMapping) && execution( *
com.myapp.controller..*.*(..))")
public Object around(ProceedingJoinPoint pjp, RequestMapping requestMapping)
throws Throwable {
...
...
//I populate the roles with a db lookup. They will be referenced here, and later in controller methods as-needed.
}
}
package com.myapp.model;
...
...
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"sso",
"roles"
})
@Component
@Scope(value="request", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class CurrentRoles {
@JsonProperty("roles")
private Set<Role> roles;
...
...
}
我得到以下内容:
org.springframework.beans.factory.BeanCreationException:创建名为'securityAudit'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.myapp.model.CurrentRoles com.myapp.currentRoles;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到[com.myapp.model.CurrentRoles]类型的限定bean用于依赖:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
Aspect是在启动时创建的。我希望注入的请求范围的bean在请求开始进入之前保持为null,然后我可以为该特定请求填充currentRoles bean。
答案 0 :(得分:0)
很可能你忘记了Service类中的@Service注释;)