【问题标题】:Is that possible to apply method level security for Spring @MVC controllers?是否可以为 Spring @MVC 控制器应用方法级安全性?
【发布时间】:2011-11-17 23:02:16
【问题描述】:

我已经应用了下面的代码,但它不起作用,任何人都可以给我一些解决方案。谢谢。

控制器方法:

@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>

【问题讨论】:

    标签: spring-mvc spring-security


    【解决方案1】:

    您想使用&lt;intercept_url&gt; 标记应用安全性。

    例如:

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

    【讨论】:

    • 嗨,ericacm!非常感谢您的回复,是的,过去我已经这样做了,但是现在(使用更新的 Spring Security)似乎我们可以在方法级别上做同样的访问扭曲的东西,这意味着而不是直接做任何 xml 配置我们可以将其表示为特定的方法。
    【解决方案2】:

    如果你想使用基于 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 安全配置作为参数添加到您的 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>
    

    【讨论】:

    • 嗨彼得·桑托!非常感谢您的回答,使用这些配置,@Secured 将无法在 Spring 控制器(使用 @Controller)内工作。
    • 它也适用于控制器,但您需要将 spring 安全相关的 XML 添加到 servlet 的 xml 中。至少应该有
    • 嗨彼得·桑托!非常感谢释放回来。我会试试的,谢谢。
    猜你喜欢
    • 2014-01-02
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多