Java 我必须在接口或类上放置@Secured注释吗?

Java 我必须在接口或类上放置@Secured注释吗?,java,spring,spring-security,Java,Spring,Spring Security,在接口方法或实现接口的类中的方法上放置@Secured注释是否正确?对此有什么建议吗 当我深入研究定义@Secured注释的类时,我可以看到它具有@继承的注释集: @Target({ ElementType.METHOD, ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface Secured { /** * Returns the list

在接口方法或实现接口的类中的方法上放置
@Secured
注释是否正确?对此有什么建议吗

当我深入研究定义
@Secured
注释的类时,我可以看到它具有
@继承的
注释集:

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Secured {
    /**
     * Returns the list of security configuration attributes (e.g. ROLE_USER, ROLE_ADMIN).
     *
     * @return String[] The secure method attributes
     */
    public String[]value();
}

读过之后,我想我可以在接口上设置
@Secured
注释,以便在接口的所有实现上一致地强制执行授权

在您提供的链接中说,
@Transactional
也是继承的。让我们把它们的每一部分都分解一下

根据spring使用带有具体类的
@Transactional
注释。 您可以在接口中使用
@Transactional
注释,也可以在接口中使用方法。如果使用
基于接口的代理
,您可以认为这会像您预期的那样工作。未继承的注释指的是,如果您使用的是基于类的代理,则事务属性可能不会应用于该接口。因此,事务属性不能覆盖或包装最终对象

如果是这样,
@安全的
注释是
@继承的
,那么这可以在接口及其实现类中使用。 从春天开始:

安全注释用于定义业务方法的安全配置属性列表

例如:

 @Secured({ "ROLE_USER" })
 public void create(Contact contact);

 @Secured({ "ROLE_USER", "ROLE_ADMIN" })
 public void update(Contact contact);

 @Secured({ "ROLE_ADMIN" })
 public void delete(Contact contact);

因此,归根结底,一个接口可能有多个实现。因此,在接口中保留
@安全的
注释是有意义的。

如前所述,您可以对接口进行注释,但它仅在“使用基于接口的代理”时有效。一般来说,Java注释不是从接口继承的。@Andre,如果答案有用,那么你应该接受我对Stackoverflow用户的回答。@AtaurRahmanMunna done。谢谢!