值得一提的是:我正在跟随一个关于使用Spring安全性保护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 Forbidden
。如果我理解正确的话,我配置Spring Security的方式应该不是这里的问题。
I 不工作,如果我只是添加
<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
。这开始让我发疯,因为我被困在这里这么多天,我不敢告诉你。你的帮助将受到感激和回报。
整个<<p> strong> 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>
错误出现在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
我所能说的是,这开始让人感觉很私人。
EDIT:因为use-expressions=true
是默认启用的,所以这个答案没有帮助。但是…我发现了你的错误:
spring在/login自动生成一个默认登录页面,但前提是你没有指定任何login-page
参数。您将参数设置为/login
,期望您将被重定向到这个默认页面。这就是为什么你得到404 not found错误。
因此,删除login-page="/login"
以使用默认值或创建自己的登录页面。authentication-failure-url="/login?error"
也一样:当使用默认页面时,删除它。
默认登录页面中的用户名和密码参数是"j_username"one_answers"j_password",spring找不到它们。如果你不使用自己的登录页面,请删除它们…
此外,您还为安全性添加了两次XML名称空间……你应该删除xmlns:security="http://www.springframework.org/schema/security"
仅适用于spring security 3.x
如果您想使用像permitAll
这样的表达式,您需要使用use-expressions="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>