Bill,
following is a LogonInterceptor I use in my production applications:
1. LogonInterceptor
Code:
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.taha.web.spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.WebUtils;
import org.taha.domain.User;
import org.taha.web.Globals;
/**
* @author <a href="mailto:irbouh@gmail.com">Omar Irbouh</a>
* @version $Revision: 0.0.1 $
* @since 2004.11.01
*/
public class LogonInterceptor extends HandlerInterceptorAdapter {
private String loginPage;
private boolean saveUrl = false;
private String urlParameterName;
/* (non-Javadoc)
* @see org.springframework.web.servlet.handler.HandlerInterceptorAdapter#preHandle(HttpServletRequest, HttpServletResponse, Object)
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
User user = (User) WebUtils.getSessionAttribute(request, Globals.CONNECTED_USER);
if (user == null) {
String url = request.getServletPath();
String query = request.getQueryString();
ModelAndView modelAndView = new ModelAndView(loginPage);
if (saveUrl) {
if (query != null)
modelAndView.addObject(urlParameterName, url+"?"+query);
else
modelAndView.addObject(urlParameterName, url);
}
throw new ModelAndViewDefiningException(modelAndView);
}
else {
return true;
}
}
/**
* define loginPage to which the Interceptor will redirect the user
* @param loginPage context relative login page
*/
public void setLoginPage(String loginPage) {
this.loginPage = loginPage;
}
/**
* sets if the interceptor should send the requested URL to login page
* @param saveUrl
*/
public void setSaveUrl(boolean saveUrl) {
this.saveUrl = saveUrl;
}
/**
* sets the name of the parameter used by login page to recieve the requested URL
* @param urlParameterName
*/
public void setUrlParameterName(String urlParameterName) {
this.urlParameterName = urlParameterName;
}
}
2. configuration
Code:
<!-- Logon Interceptor -->
<bean id="logonInterceptor" class="org.taha.web.spring.LogonInterceptor">
<property name="loginPage">
<value>redirect:/login.htm</value>
</property>
<property name="saveUrl">
<value>true</value>
</property>
<property name="urlParameterName">
<value>targetUrl</value>
</property>
</bean>
3. loginForm (velocity)
Code:
...
<form action="login.htm" name="loginForm" method="post">
<input type="hidden" name="targetUrl" value="$!{request.getParameter("targetUrl")}">
...
</form>
...
HTH