提问者:小点点

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


值得一提的是:我正在学习有关使用Spring Security保护GWT应用程序的教程。

我不明白。我似乎无法让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禁止。如果我理解正确,我配置Spring Security的方式不应该是这里的问题。

我不工作,如果我简单地添加

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

到<代码>

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

我想了解为什么,因为这不是我想要配置Spring Security的方式。.添加每个应该允许的URL。

我面临的另一个问题是,无论出于什么原因(也许是相同的),我都无法访问//localhost:8080/login。这意味着如果我将登录名提交给/login,我将获得403禁止

现在,人们会认为添加

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

整个应用程序上下文服务. 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>

编辑:

减少应用上下文服务. 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>

共2个答案

匿名用户

错误似乎在web. xml中。而不是

<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: Suspicious url pattern: "/**" in context [] - see section SRV.11.2 of the Servlet specification

我只能说这开始有点私人化了…

匿名用户

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

仅适用于Spring Security 3. x
如果您想使用像permitAll这样的表达式,您需要使用use-表达式="true"启用它们。不要忘记允许访问您的登录页面所需的资源。在这里,您可以找到一些关于表达式使用的帮助。我的问题是,如果您使用的是单独的login.jsp/html页面,或者“/login”指的是gwt应用程序中的一个地方/历史令牌?

它如何工作的小例子:

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