【问题标题】:How to use intercept-url pattern and access?如何使用拦截 URL 模式和访问?
【发布时间】:2014-01-21 17:31:21
【问题描述】:

我正在使用 Spring 安全性,但我不太了解拦截 URL 的工作原理。这是我的 spring 安全配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">

    <!-- configuration des urls  -->
    <security:http auto-config="true">
        <security:intercept-url pattern="/*" access="ROLE_USER" />
        <security:form-login/>
    </security:http>


    <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider ref="authenticationProvider" />
    </security:authentication-manager>

    <bean id="authenticationProvider"
       class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <property name="userDetailsService" ref="userDetailsService" />
    </bean>

    <bean id="userDetailsService"
        class="com.nexthome.app.security.UserDetailsServiceImpl" />


</beans>

使用此配置,未经身份验证我无法访问。所以当访问 url http://localhost:8080/nexthome 时会显示一个 spring 登录表单(http://localhost:8080/nexthome/spring_security_login)。 但是当我将其更改为 .我无需登录即可访问http://localhost:8080/nexthome/user/login

我的目标是保护一些网址:

http://localhost:8080/nexthome  all people must access
http://localhost:8080/nexthome/login  all people must access ( how to display spring login form when trying to access the url?)
http://localhost:8080/nexthome/logout  access to ROLE_USER and ROLE_ADMIN

感谢您的帮助

【问题讨论】:

    标签: java spring spring-mvc spring-security


    【解决方案1】:

    security:intercept-url 标签上的模式属性使用Ant paths。在这种语法下,. 除了文字 . 之外没有任何意义。因此,该模式很可能无法被 Spring 安全性识别并被忽略。

    如果您想对/nexthome/logout url 而不是/nexthome/login/nexthome 要求身份验证,您可以使用以下配置:

    <security:http auto-config="true">
        <intercept-url pattern="/nexthome" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <intercept-url pattern="/nexthome/login" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <intercept-url pattern="/nexthome/logout" access="ROLE_USER,ROLE_ADMIN" />
        <security:form-login/>
    </security:http>
    

    【讨论】:

    • 我已经添加了您建议的代码,但现在我无需身份验证即可访问所有页面。我可以访问:主页/nexthome。但我也可以访问登录(不提示spring security自动生成的表单)并且无需登录即可访问注销。我想我错过了什么
    • 找到了kevin bowersox。
    【解决方案2】:

    我找到了问题的答案:

    <security:http auto-config="true">
            <security:intercept-url pattern="/register"
                access="IS_AUTHENTICATED_ANONYMOUSLY" />
            <security:intercept-url pattern="/login*"
                access="ROLE_USER" />
            <security:intercept-url pattern="/logout*"
                access="ROLE_USER,ROLE_ADMIN" />
            <security:form-login/>
     </security:http>
    

    现在 url
    http:localhost:8080/nexthome 是免费访问
    http:localhost:8080/nexthome/regsiter 是免费访问
    http:localhost:8080/nexthome/login 在访问资源之前提示 Spring Security 登录表单
    http:localhost:8080/nexthome/logout 提示 Spring Security访问资源前的登录表单

    【讨论】:

      【解决方案3】:

      在表单登录中使用这样的登录页面

      <securityform-login login-page="/login" >
      

      如果没有指定登录 URL,Spring Security 会自动在 / 处创建登录 URL spring_security_login。

      出于注销目的使用

      <intercept-url pattern="/nexthome/logout" access="ROLE_USER,ROLE_ADMIN" />
      

      【讨论】:

      • 当我点击链接 /nexthome/logout 时,没有创建 Spring 登录表单。
      猜你喜欢
      • 2011-07-11
      • 2013-06-04
      • 2017-10-02
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      相关资源
      最近更新 更多