我遇到了问题,但不知道如何解决它,任何帮助都会得到评估。 我需要通过一些身份验证来保护 REST 方法的安全,并且我在应用程序中扮演的角色很少。我在类中的方法处放置了 @Secured 注释,该注释实现了一些接口(interface),并且 Spring 不创建 bean(在我的情况下是 @Controller ),没有任何日志消息。但如果该类没有实现该接口(interface) – bean 就会创建得很好。
在代码中它看起来像这样:
Controller :
public class Controller implements API {
@Secured("ROLE_ADMIN")
@RequestMapping(value = "/one/", method = RequestMethod.GET)
public String test() {
return "ONE";
}
}
界面:
public interface API {
String test();
}
在我的情况下, Controller bean 没有创建。但如果我删除 implements API
一切都会正常工作,将创建 bean 并且安全性将发挥作用。
我使用基于 XML 的配置和位置
<secutiry:global-method-security pre-post-annotations="enabled" jsr250-annotations="enabled" secured-annotations="enabled">
在mvc-config.xml处启用注释。
我跟踪它,如果注释放置在没有实现接口(interface)的类中的方法上,Spring 在构建上下文时使用 MethodSecurityMetadataSourceAdvisor.MethodSecurityMetadataSourcePointcut
,但是当类实现接口(interface)时 - Spring 使用 TransactionAttributeSourcePointcut
。
@RolesAllowed
具有相同的效果。
检查版本
<spring.version>4.3.7.RELEASE</spring.version>
<spring-security.version>4.2.2.RELEASE</spring-security.version>
和
<spring.version>4.3.23.RELEASE</spring.version>
<spring-security.version>4.2.13.RELEASE</spring-security.version>
最新的Spring Boot具有相同的效果。
有人解决了同样的问题吗?非常感谢。
最佳答案
尝试添加这个- @EnableGlobalMethodSecurity(securedEnabled = true) 在配置类的类级别,我们可以在任何 @Configuration 实例上使用 @EnableGlobalMethodSecurity 注释来启用基于注释的安全性。例如,以下代码将启用 Spring Security 的 @Secured 注释。
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class MethodSecurityConfig {
// ...
}
关于java - 实现接口(interface)时未创建带有 @Secured 注释的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57567969/