春季安全注销
我遇到了Spring Security的麻烦。我可以登录但不能登出(至少不是如预期的那样)。春季安全注销
登录后,我将被重定向到/secure/home.xhtml < - 此工作正常,并按预期工作。但是我无法通过#{request.contextPath} /注销注销。 (我已经改变了春季安全配置注销URL,但我也有一个默认试过)总有一个404
这里至今代码:
index.xhtm < - 工作正常
<form method="POST" id="loginForm" action="#{request.contextPath}/j_spring_security_check" class="form-signin" autocomplete="off">
<div class="form-group">
<label for="username" class="control-label">#{bundle["login.username"]}</label>
<input type="text" name="username" id="username" class="input-block-level form-control"
placeholder="#{bundle['label.username']}" required="true" tabindex="1" />
<span class="help-block"></span>
</div>
<div class="form-group">
<label for="password" class="control-label">#{bundle["login.password"]}</label>
<input type="password" class="input-block-level form-control" name="password" id="password" tabindex="2"
placeholder="#{bundle['label.password']}" required="true" />
<span class="help-block"></span>
</div>
<button type="submit" tabindex="3" class="btn btn-success btn-block">#{bundle["login.action"]}</button>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
弹簧security.xhtml,HTTP的conf
<security:http use-expressions="true" >
<security:intercept-url pattern="/secure/**" access="hasAnyRole('USER','ADMIN')" />
<security:intercept-url pattern="/admin/**" access="hasRole('ADMIN')" />
<!--<security:access-denied-handler error-page="/404.xhtml" />-->
<security:form-login
login-page="/index.xhtml"
default-target-url="/secure/home.xhtml"
authentication-failure-url="/index.xhtml?error"
username-parameter="username"
password-parameter="password" />
<security:logout logout-url="/logout" logout-success-url="/index.xhtml?logout" invalidate-session="true" delete-cookies="JSESSIONID" />
<security:csrf />
</security:http>
这是我试图执行关于向其他的答案在这里STA注销的方式ckoverflow:
<a href="#{request.contextPath}/logout">logout</a>
<h:outputLink value="#{request.contextPath}/logout">Logout</h:outputLink>
但这两个链接都不起作用。我得到了404.我也读过你应该用pageContext.request.contextPath替换request.contextPath,但那也不行。 (以代替localhost:8080/myContext /注销链接将我重定向到本地主机:8080 /注销)
一个教程给我看,说注销可以用这个来还实施:
<form method="POST" id="loginForm" action="#{request.contextPath}/logout" class="form-signin" autocomplete="off">
<button type="submit" tabindex="3" class="btn btn-success btn-block">#{bundle["logout.action"]}</button>
<input type="hidden" name="#{_csrf.parameterName}" value="#{_csrf.token}" />
</form>
在开始它似乎解决了我的问题,但在向安全部分添加更多页面(例如“profile.xhtml”)之后,我加入了在加载页面后注销的不受欢迎的行为。 因此,如果我在我的home.xhtml中添加上面的注销表单,即使我没有点击注销,似乎也会注销。如果我刷新页面,即使没有点击注销,我也会被重定向到index.xhtml(登录)。所以,如果我点击链接到profile.xhtml,我会自然重定向到index.xhtml,因为春天认为我退出了。没有这个表单,我保持登录状态,但无法注销!一团糟!
啊,如果我在注销按钮单击窗体上,我得到以下错误:
HTTP Status 403 - Expected CSRF token not found. Has your session expired?
type Status report
messageExpected CSRF token not found. Has your session expired?
descriptionAccess to the specified resource has been forbidden.
我也实在没有想法什么毛病我的配置:(
这里是我的! web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>messages</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/application-context.xml
classpath:/application-security.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<!-- Predefined pages -->
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<!-- <error-page>
<error-code>403</error-code>
<location>/error.xhtml</location>
</error-page>-->
<error-page>
<error-code>404</error-code>
<location>/404.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.xhtml</location>
</error-page>
<error-page>
<exception-type>javax.faces.application.ServletException</exception-type>
<location>/index.xhtml</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.xhtml</location>
</error-page>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<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>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/logout</url-pattern>
</filter-mapping>
<!-- MIME TYPES -->
<mime-mapping>
<extension>css</extension>
<mime-type>text/css</mime-type>
</mime-mapping>
<mime-mapping>
<extension>eot</extension>
<mime-type>application/x-font-eot</mime-type>
</mime-mapping>
<mime-mapping>
<extension>js</extension>
<mime-type>text/javascript</mime-type>
</mime-mapping>
<mime-mapping>
<extension>latex</extension>
<mime-type>application/x-latex</mime-type>
</mime-mapping>
<mime-mapping>
<extension>otf</extension>
<mime-type>application/x-font-opentype</mime-type>
</mime-mapping>
<mime-mapping>
<extension>roff</extension>
<mime-type>application/x-troff</mime-type>
</mime-mapping>
<mime-mapping>
<extension>svg</extension>
<mime-type>application/svg+xml</mime-type>
</mime-mapping>
<mime-mapping>
<extension>ttf</extension>
<mime-type>application/x-font-ttf</mime-type>
</mime-mapping>
<mime-mapping>
<extension>woff</extension>
<mime-type>application/x-font-woff</mime-type>
</mime-mapping>
<mime-mapping>
<extension>woff2</extension>
<mime-type>application/x-font-woff2</mime-type>
</mime-mapping>
</web-app>
使用的GlassFish 4.1,Spring Framework的版本4.1.2.RELEASE和春季安全版本3.2.5.RELEASE
我希望每一个答案。这个错误已经花了两天,没有任何解决方案:(
你必须要在启用CRLF保护。
您可以尝试添加以下内容到形式,支持CRLF:
<input type="hidden"
name="${_csrf.parameterName}"
value="${_csrf.token}"/>
如果不工作,你可以删除CSRF - 尽管这不推荐。
更多信息:http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#csrf