Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

spring security - Redirect to Previous url after TIMEOUT

I am using Spring 3.1 version.

I have implemented spring security for login to my web portal. It works fine except for one issue. I have set session timeout to 2 min.

Once timeout happens and then user click any URL, It gets redirected to logout page. But when user re authenticates, user directly lands on the home page which is default target URL instead of last access page.

Like if user is accessed /home/editproduct then after timeout & when he again reautenticate he should be accessed to the home/editproduct instead of only /home page.

i am using spring with JSON & AJAX call.

<bean id="myNePublicUserNamePasswordAuthFilter"
class="com.ne.mynelson.authentication.publicuser.MyNePublicUserPasswordAuthFilter">
    <property name="filterProcessesUrl" value="/service/json_authentication_check"></property>
    <property name="authenticationManager" ref="myNePublicUserAuthenticationManager" />
    <property name="authenticationFailureHandler" ref="failureHandler" />
    <property name="authenticationSuccessHandler" ref="successHandler" />
    <property name="authenticationInputProcessor" ref="myNePublicUserAuthInputProcessor"></property>
</bean>
<bean id="successHandler"
class="com.ne.mynelson.authentication.publicuser.MyNePublicUserAuthSuccessHandler">
    <property name="authHandlerView" ref="authHandlerView"></property>
    <property name="sessionRegistry" ref="sessionRegistry"></property>
    <property name="publicLoginManager" ref="publicLoginManager"></property>
</bean>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EDIT: For SessionManagementFilter

You need to implement the InvalidSessionStrategy, override the onInvalidSessionDetected method, just like SimpleRedirectInvalidSessionStrategy, but before redirect, you need to create a new session, and save the request to session.

HttpSession session = request.getsession(false);
if (session != null) {
    // for creating a new session
    session.invalidate();
}
DefaultSavedRequest savedRequest = new DefaultSavedRequest(request,
                    new PortResolverImpl());
request.getSession(true).setAttribute("SPRING_SECURITY_SAVED_REQUEST", savedRequest);
redirectStrategy.sendRedirect(request, response, destinationUrl);

and then inject this bean to SessionManagementFilter.

EDIT: For ConcurrentSessionFilter

If you use the concurrentSessionFilter, you can implement SessionInformationExpiredStrategy, just like SimpleRedirectSessionInformationExpiredStrategy, and in the method onExpiredSessionDetected, still do the same thing like I post above, before redirect, create new session, and put the save request to new session, you can get the requestby event.getRequest(), then inject this sessionInfomationExpiredStrategy to concurrentSessionFilter.

public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException {
    logger.debug("Redirecting to '" + destinationUrl + "'");
    DefaultSavedRequest savedRequest = new DefaultSavedRequest(event.getRequest(),
                        new PortResolverImpl());
    request.getSession(true).setAttribute("SPRING_SECURITY_SAVED_REQUEST", savedRequest);
    redirectStrategy.sendRedirect(event.getRequest(), event.getResponse(), destinationUrl);
}

Finally , Using SavedRequestAwareAuthenticationSuccessHandler instead of SimpleUrlAuthenticationSuccessHandler. It will try to get the request target url and then redirect to the saved URL.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...