2012-10-05 61 views
0

我注意到在我的代碼中,我可以用@Secured(「ROLE_ADMIN」)和@RolesAllowed(「ROLE_ADMIN」)成功註釋服務類(@Service)上的方法。有用。雖然,當我將相同的註釋移動到控制器類(@Controller)時,只有@Secured被激活,而@RolesAllowed被忽略。我配置我的Spring Security註釋不一致?

<security:global-method-security 
      jsr250-annotations="enabled" 
      secured-annotations="enabled"/> 

根的applicationContext爲什麼不@RollesAllowed適用於控制器類,而@Secured是什麼?

+0

問題你爲什麼同時使用? – Woot4Moo

+0

以便我學會如何使用它們,然後可以有更多的教育意見,我會選擇。你知道爲什麼我看到問題中解釋的行爲? – Pomario

+0

我認爲這是因爲您正在嘗試使用兩組相似但不相同的註釋。據我記得'@ RolesAllowed'是純粹的Spring做法,'@ Secured'特別與Spring Security綁定。 – Woot4Moo

回答

1

@RolesAllowed在服務級別(但不在控制器中)工作的原因是由於我的配置錯誤。爲了使它工作,我必須在我的控制器掃描的相同配置文件(上下文)中聲明全局方法安全性。例如:

<context:component-scan base-package="mrpomario.springcore.mvc.controller"/> 
<security:global-method-security 
      jsr250-annotations="enabled" 
      secured-annotations="enabled"/> 
+0

這與你的問題不太一致。如果該聲明在包含控制器的應用程序上下文中不可見,則根本沒有任何安全註釋可用。但是,你說那個人在工作,但一個人不在。 –

1

混合不同的註釋並不能保證產生一致的行爲。

the manual

您可以在同一應用程序啓用多個類型的註釋,但只有一種類型應該用於任何接口或類的行爲將不會另行明確。如果找到適用於特定方法的兩個註釋,則只會應用其中的一個註釋。