【问题标题】:Namespace or URI based spring security基于命名空间或 URI 的 spring 安全性
【发布时间】:2012-07-04 14:07:11
【问题描述】:

我需要根据 URI 的命名空间来实现授权。 例如,所有类型的用户都可以访问 localhost:8080/common/*,并且 localhost:8080/admin/* 只有管理员用户才能访问。

我已经使用 UsernamePasswordAuthenticationFilter 进行登录,但我不知道如何检查每个请求的授权。

谁能指导我如何使用 Spring Security 实现这种授权?

感谢和问候。

【问题讨论】:

    标签: jakarta-ee spring-security authorization


    【解决方案1】:

    您可以使用

    <intercept-url> element
    

    intercept-url 元素可用于定义一个模式,该模式使用 ant 路径样式语法与传入请求的 URL 匹配。

    例如使用 SpEL 的 Spring 3.0 +:

    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/common/*" access="permitAll" />
        <intercept-url pattern="/registered/*" access="hasAnyRole('ROLE_USER,ROLE_ADMIN')" />
        <intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" />
    </http>
    

    如果您没有 SpEL 支持,请像这样使用:

    <http auto-config='true'>
        <intercept-url pattern="/common/*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <intercept-url pattern="/registered/*" access="ROLE_USER,ROLE_ADMIN" />
        <intercept-url pattern="/admin/*" access="ROLE_ADMIN" />
    </http>
    

    Documentation

    更新 - 如果您在 JSP/JSP 中进行转发,则需要在 http 元素上设置一次每次请求 =“false”。

    另见:

    【讨论】:

    • 感谢您的回复。实际上我已经尝试过相同的代码,但是这种授权只在登录(身份验证)时发生一次,以后不会发生在每个请求中。
    • 所以我认为您在 JSP/JSF 页面中进行转发。我已经更新了答案!
    猜你喜欢
    • 1970-01-01
    • 2017-11-24
    • 2016-01-17
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    相关资源
    最近更新 更多