2011-11-11 60 views
0

我已經應用了下面的代碼,但它不工作,任何人都可以給我一些解決方案。謝謝。是否有可能爲Spring @MVC控制器應用方法級安全性?

控制器的方法:

@Controller 
public class UserController { 
@Secured("ROLE_USER") 
@RequestMapping(value="user/{userName}", method=RequestMethod.GET) 
    public @ResponseBody User getAvailability(@PathVariable String userName, HttpServletResponse response) { } 
} 

的applicationContext-security.xml文件相關的配置:

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

    <http> 
     <intercept-url pattern="/user" requires-channel="https"/> 
     <http-basic/> 
    </http> 

    <authentication-manager alias="authenticationManager"> 
     <authentication-provider> 
      <user-service> 
       <user password="password" name="manager" authorities="ROLE_MANAGER"/> 
       <user password="password" name="user" authorities="ROLE_USER"/> 
      </user-service> 
     </authentication-provider> 
    </authentication-manager> 

回答

0

您希望使用<intercept_url>標籤應用的安全性。

例如:

<intercept-url pattern="/user" requires-channel="https" access="ROLE_USER"/> 
+0

嗨ericacm!非常感謝您的答覆,是的,在我這樣做的時候,但現在(與更新的Spring Security)似乎我們可以做同樣的訪問扭曲的東西,在方法級別知道,這意味着,而不是做任何XML配置,直接我們可以表示特定的方法。 – Channa

0

如果你想使用基於URL的訪問控制,您需要添加/ **

<intercept-url pattern="/user/**" requires-channel="https"/> 

如果你想方法級別的安全性,那麼你需要

<context:component-scan base-package="com.yourcompany.etc"/> 

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

也在您的web.xml中,您需要將spring security config作爲參數添加到您的servlet

<servlet> 
    <servlet-name>myservlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>WEB-INF/applicationContext-security.xml</param-value> 
    </init-param> 
    <load-on-startup>2</load-on-startup> 
</servlet> 
+0

嗨Peter Szanto!非常感謝您的回答,對於這些配置,@Secured不會在Spring Controller內部工作(使用@Controller)。 – Channa

+0

它也可以在控制器上工作,但是您需要將spring安全相關​​的XML添加到servlet的xml中。至少<安全:全球方法安全secured-annotations =「enabled」/>應該在那裏 –

+0

我更新了我的答案,以顯示如何導入安全bean –

相關問題