为什么 'permitAll()' 不起作用?

Why is 'permitAll()' not working?

值得一提:我正在学习有关 Securing GWT apps with Spring Security 的教程。


我不明白。我似乎无法让 permitAll 正常工作。

这是我当前的配置:

<http auto-config="true">
    <intercept-url pattern="/**" access="permitAll" />
    <form-login 
        login-page="/login" 
        default-target-url="/welcome" 
        authentication-failure-url="/login?error" 
        username-parameter="username"
        password-parameter="password" />
</http>

如果我在 //localhost:8080 上访问我的网站,该网站不会完全加载,因为请求

//localhost:8080/app/xsrf

403 Forbidden 出于某种原因。我配置的方式 Spring 如果我没看错的话安全应该不是这里的问题。

如果我简单地添加

<intercept-url pattern="/**" access="permitAll" />

<http ..>所做的工作是添加这个:

<http pattern="/app/xsrf" security="none"/>

我想明白为什么,因为这不是我配置 Spring 安全性的方式.. 添加每一个应该被允许的 URL。

我面临的一个 附加 问题是,无论出于何种原因(可能是相同的)我无法访问 //localhost:8080/login。这意味着如果我将我的登录信息提交到 /login,我将得到 403 Forbidden.

现在,人们会认为添加 <http pattern="/login" security="none"/> 会有所帮助,但 没有 。如果我将它添加到我的配置中,我将在这个特定的 URL 上得到 404 Not Found

这让我发疯,因为我被困在这里这么多天我不敢告诉你。您的帮助将得到赞赏和回报。


整个applicationContext-service.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-4.0.xsd">

    <!-- Imports -->
    <beans:import resource="applicationContext-jooq.xml"/>

    <!-- /////////////////////////////////////////////////////////////// -->
    <!-- // BEGIN Spring Security -->

    <http pattern="/app/xsrf" security="none"/>
    <!-- <http pattern="/login" security="none"/> -->

    <http auto-config="true">
        <intercept-url pattern="/**" access="permitAll" />

        <form-login 
            login-page="/login" 
            default-target-url="/welcome" 
            authentication-failure-url="/login?error" 
            username-parameter="username"
            password-parameter="password" />
    </http>

    <beans:bean id="authenticationListener" 
            class="com.mz.server.web.auth.CustomAuthenticationListener"/>

    <beans:bean id="authenticationProvider" 
            class="com.mz.server.web.auth.CustomAuthenticationProvider"/>

    <beans:bean id="userDetailsService" 
            class="com.mz.server.web.service.CustomUserDetailsService"/>

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

    <!-- // END Spring Security -->
    <!-- /////////////////////////////////////////////////////////////// -->
    <!-- // BEGIN Services -->

    <beans:bean id="loginService" class="com.mz.server.web.service.LoginService">
        <beans:constructor-arg ref="dslContext" />
    </beans:bean>

    <!-- // END Services -->

</beans:beans>

编辑:

减少 applicationContext-service.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-4.0.xsd">

    <!-- Imports -->
    <beans:import resource="applicationContext-jooq.xml"/>

    <!-- //////////////////////////////////////////////////////////////////////////////// -->
    <!-- // BEGIN Spring Security -->

    <global-method-security pre-post-annotations="enabled"/>

    <http auto-config="true">
        <intercept-url pattern="/**" access="permitAll" />
    </http>

    <!-- // END Spring Security-->

</beans:beans>

这是web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <display-name>GWT Application | mz</display-name>

    <welcome-file-list> <!-- Default page to serve -->
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <!-- //////////////////////////////////////////////////////////////////////////////// -->
    <!-- // BEGIN Filters -->

    <!-- Spring Security -->

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- // END FILTERS -->
    <!-- //////////////////////////////////////////////////////////////////////////////// -->
    <!-- // BEGIN Listeners -->

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>com.mz.server.web.ServerConfig</listener-class>
    </listener>

    <!-- // END Listeners -->
    <!-- //////////////////////////////////////////////////////////////////////////////// -->
    <!-- // BEGIN Servlets -->

    <servlet>
        <servlet-name>login</servlet-name>
        <servlet-class>com.mz.server.web.servlet.LoginServletImpl</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/app/login</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>xsrf</servlet-name>
        <servlet-class>com.google.gwt.user.server.rpc.XsrfTokenServiceServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>xsrf</servlet-name>
        <url-pattern>/app/xsrf</url-pattern>
    </servlet-mapping>

    <servlet> <!-- Dispatcher Servlet for REST API for Mobile Devices -->
        <servlet-name>mobile-restapi</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>    
    <servlet-mapping>
        <servlet-name>mobile-restapi</servlet-name>
        <url-pattern>/app/restapi/*</url-pattern>
    </servlet-mapping>

    <!-- // END Servlets -->
    <!-- //////////////////////////////////////////////////////////////////////////////// -->
    <!-- // BEGIN Context Parameter -->

    <context-param>
        <param-name>
            gwt.xsrf.session_cookie_name
        </param-name>
        <param-value>
            mzsid
        </param-value>
    </context-param>

    <context-param>
        <param-name>
            contextConfigLocation
        </param-name>
        <param-value>
            classpath:/**/spring-config.xml
            classpath*:applicationContext-service.xml
        </param-value>
    </context-param>

    <!-- // END Context Parameter -->
    <!-- //////////////////////////////////////////////////////////////////////////////// -->

</web-app>

编辑: 因为 use-expressions=true 默认启用,所以这个答案没有帮助。 但是...我发现了你的错误:
spring 在 /login 自动生成一个默认的登录页面,但前提是你没有指定任何 login-page 参数。您将参数设置为 /login,期望您将被重定向到此默认页面。 这就是为什么您收到 404 not found 错误的原因。
所以删除 login-page="/login" 以使用默认值或创建您自己的登录页面。
authentication-failure-url="/login?error" 相同:使用默认页面时将其删除。
默认登录页面中的用户名和密码参数是 "j_username" 和 "j_password",而 spring 找不到这些。如果您不使用自己的登录页面,请删除它们...
您还为安全添加了 xml 命名空间两次...您应该删除 xmlns:security="http://www.springframework.org/schema/security"

仅适用于spring安全3.x
如果你想使用像 permitAll 这样的表达式,你需要使用 use-expressions="true" 来启用它们。 不要忘记允许访问您的登录页面所需的资源。 here 你可以找到一些关于表达式使用的帮助。 我的问题是,如果您使用单独的登录。jsp/html 页面或“/login”指的是您的 gwt 应用程序中的 place/historytoken?

它如何工作的小例子:

<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/login" access="permitAll" />
    <intercept-url pattern="/resources/**" access="permitAll" />
    <intercept-url pattern="/**" access="isFullyAuthenticated()" />
    <!-- if you are not completely familiar how to use expressions -->
    <!-- then using this will probably easier: -->
    <!-- <http auto-config="true" use-expressions="false">
        <!-- <intercept-url pattern="/login" access="IS_AUTHENTICATED_ANONYMOUSLY" /> -->
        <!-- <intercept-url pattern="/resources/**" access="IS_AUTHENTICATED_ANONYMOUSLY" /> -->
        <!-- <intercept-url pattern="/**" access="IS_AUTHENTICATED_FULLY" /> -->

    <!-- why not your /index.html/jsp?? -->
    <form-login 
        default-target-url="/welcome" 
        always-use-default-target="true"/>
</http>

看来错误在 web.xml 中。而不是 <url-pattern>/*</url-pattern>(如我所遵循的教程中所述),它应该是 /**:

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <!-- It appears that this should say '/**' and not '/*' as stated in many
        tutorials 
        (e.g. http://websystique.com/spring-security/spring-security-4-hello-world-annotation-xml-example/).  -->
    <url-pattern>/**</url-pattern>
</filter-mapping>

有趣的是,我现在得到以下信息“INFO”:

INFO: Suspicious url pattern: "/**" in context [] - see section SRV.11.2 of the Servlet specification

我只能说,这开始让人觉得很私人了..