vendredi 13 mars 2015

Spring Security Logout

I ran into trouble with Spring Security. I´m able to login but not to logout (at least not as expected).


After login, I will be redirected to /secure/home.xhtml <- this works fine and as expected. But I´m not able to logout through #{request.contextPath}/logout. (I have changed the logout url in the spring security config, but I also tried it with the default one) There is always a 404.


Here the code so far:


index.xhtm <- works fine



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


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


This is the way I tried to implement the logout regarding to other answers here on stackoverflow:



<a href="#{request.contextPath}/logout">logout</a>
<h:outputLink value="#{request.contextPath}/logout">Logout</h:outputLink>


But both links doesn´t work. I get the 404. I also read that you should replace request.contextPath with pageContext.request.contextPath but that did not work too. (instead of localhost:8080/myContext/logout the link will redirect me to localhost:8080/logout)


A tutorial showed me, that logging out can be also implemented with this:



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


At beginning it seemed to solve my problem, but after adding more pages to the secure section, for example "profile.xhtml", I came accross the undesireable behaviour that I get logged out after the page is loaded. So if I add the logout-form as above in my home.xhtml, it seems that I get logged out even if I do not click on logout. If I refresh the page, I will be redirected to index.xhtml (login) even if I did NOT click on Logout. So If I click on the link to profile.xhtml I will naturally be redirected to index.xhtml as spring thinks I logged out. Without this form, I stay logged in, but not able to logout! It´s a mess!


Ah and if I click on the logout button on the form, I get the following error:



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.


I have really no idea whats wrong with my config :(!


Here is my web.xml:



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://ift.tt/19L2NlC"
xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/19L2NlC http://ift.tt/1drxgYl"
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>


Using GlassFish 4.1, spring framework version 4.1.2.RELEASE, and spring security version 3.2.5.RELEASE.


I would appreciate every answer. This error already took two days without any solutions :(


Aucun commentaire:

Enregistrer un commentaire