Code:
public class PortletSessionContextIntegrationInterceptor
implements InitializingBean, HandlerInterceptor {
protected static final Log logger = LogFactory.getLog(PortletSessionContextIntegrationInterceptor.class);
public static final String ACEGI_SECURITY_CONTEXT_KEY = HttpSessionContextIntegrationFilter.ACEGI_SECURITY_CONTEXT_KEY;
private static final String SESSION_EXISTED = PortletSessionContextIntegrationInterceptor.class.getName() + ".SESSION_EXISTED";
private Class context;
private Object contextObject;
/**
* Indicates if this interceptor can create a <code>PortletSession</code> if needed
* (sessions are always created sparingly, but setting this value to false
* will prohibit sessions from ever being created). Defaults to true.
*/
private boolean allowSessionCreation = true;
public void setAllowSessionCreation(boolean allowSessionCreation) {
this.allowSessionCreation = allowSessionCreation;
}
public boolean isAllowSessionCreation() {
return allowSessionCreation;
}
public void setContext(Class secureContext) {
this.context = secureContext;
}
public Class getContext() {
return context;
}
public void afterPropertiesSet() throws Exception {
if ((this.context == null)
|| (!SecurityContext.class.isAssignableFrom(this.context))) {
throw new IllegalArgumentException(
"context must be defined and implement Context (typically use net.sf.acegisecurity.context.security.SecureContextImpl)");
}
this.contextObject = generateNewContext();
}
/* (non-Javadoc)
* @see org.springframework.web.portlet.HandlerInterceptor#preHandle(javax.portlet.PortletRequest, javax.portlet.PortletResponse, java.lang.Object)
*/
public boolean preHandle(PortletRequest request, PortletResponse response,
Object handler) throws Exception {
if (SecurityContextHolder.getContext() != null) {
if (logger.isWarnEnabled()) {
logger.warn(
"ContextHolder should have been null but contained: '"
+ SecurityContextHolder.getContext() + "'; setting to null now");
}
SecurityContextHolder.setContext(null);
}
PortletSession portletSession = null;
boolean portletSessionExistedAtStartOfRequest = false;
try {
portletSession = request.getPortletSession(false);
} catch (IllegalStateException ignored) {}
if (portletSession != null) {
portletSessionExistedAtStartOfRequest = true;
Object contextObject = portletSession.getAttribute(ACEGI_SECURITY_CONTEXT_KEY, PortletSession.APPLICATION_SCOPE);
if (contextObject != null) {
if (contextObject instanceof SecurityContext) {
if (logger.isDebugEnabled()) {
logger.debug(
"Obtained from ACEGI_SECURITY_CONTEXT a valid SecurityContext and set to SecurityContextHolder: '"
+ contextObject + "'");
}
SecurityContextHolder.setContext((SecurityContext) contextObject);
} else {
if (logger.isWarnEnabled()) {
logger.warn(
"ACEGI_SECURITY_CONTEXT did not contain a SecurityContext but contained: '"
+ contextObject
+ "'; are you improperly modifying the PortletSession directly (you should always use SecurityContextHolder) or using the PortletSession attribute reserved for this class?");
}
}
} else {
if (logger.isDebugEnabled()) {
logger.debug(
"PortletSession returned null object for ACEGI_SECURITY_CONTEXT");
}
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("No PortletSession currently exists");
}
}
if (SecurityContextHolder.getContext() == null) {
SecurityContextHolder.setContext(generateNewContext());
if (logger.isDebugEnabled()) {
logger.debug(
"As SecurityContextHolder null, setup SecurityContextHolder with a fresh new instance: '"
+ SecurityContextHolder.getContext() + "'");
}
}
request.setAttribute(SESSION_EXISTED, new Boolean(portletSessionExistedAtStartOfRequest));
return true;
}
/* (non-Javadoc)
* @see org.springframework.web.portlet.HandlerInterceptor#postHandle(javax.portlet.RenderRequest, javax.portlet.RenderResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
*/
public void postHandle(RenderRequest request, RenderResponse response,
Object handler, ModelAndView modelAndView) throws Exception {
}
/* (non-Javadoc)
* @see org.springframework.web.portlet.HandlerInterceptor#afterCompletion(javax.portlet.PortletRequest, javax.portlet.PortletResponse, java.lang.Object, java.lang.Exception)
*/
public void afterCompletion(PortletRequest request, PortletResponse response,
Object handler, Exception ex) throws Exception {
PortletSession portletSession = null;
boolean portletSessionExistedAtStartOfRequest = ((Boolean)request.getAttribute(SESSION_EXISTED)).booleanValue();
// Store context back to PortletSession
try {
portletSession = request.getPortletSession(false);
} catch (IllegalStateException ignored) {}
if ((portletSession == null) && portletSessionExistedAtStartOfRequest) {
if (logger.isDebugEnabled()) {
logger.debug(
"PortletSession is now null, but was not null at start of request; session was invalidated, so do not create a new session");
}
}
// Generate a PortletSession only if we need to
if ((portletSession == null) && !portletSessionExistedAtStartOfRequest) {
if (!allowSessionCreation) {
if (logger.isDebugEnabled()) {
logger.debug(
"Whilst SecurityContextHolder contents have changed, the PortletSessionContextIntegrationInterceptor is prohibited from creating a PortletSession by the allowSessionCreation property being false");
}
} else if (!contextObject.equals(SecurityContextHolder.getContext())) {
if (logger.isDebugEnabled()) {
logger.debug(
"PortletSession being created as SecurityContextHolder contents are non-default");
}
try {
portletSession = request.getPortletSession(true);
} catch (IllegalStateException ignored) {}
} else {
if (logger.isDebugEnabled()) {
logger.debug(
"PortletSession still null, but SecurityContextHolder has not changed from default: ' "
+ SecurityContextHolder.getContext()
+ "'; not creating PortletSession or storing SecurityContextHolder contents");
}
}
}
// If PortletSession exists, store current SecurityContextHolder contents
if (portletSession != null) {
portletSession.setAttribute(ACEGI_SECURITY_CONTEXT_KEY,
SecurityContextHolder.getContext(), PortletSession.APPLICATION_SCOPE);
if (logger.isDebugEnabled()) {
logger.debug("SecurityContext stored to PortletSession: '"
+ SecurityContextHolder.getContext() + "'");
}
}
// Remove SecurityContextHolder contents
SecurityContextHolder.setContext(null);
if (logger.isDebugEnabled()) {
logger.debug(
"SecurityContextHolder set to null as request processing completed");
}
}
public SecurityContext generateNewContext() throws PortletException {
try {
return (SecurityContext) this.context.newInstance();
} catch (InstantiationException ie) {
throw new PortletException(ie);
} catch (IllegalAccessException iae) {
throw new PortletException(iae);
}
}
public void afterActionCompletion(ActionRequest arg0, ActionResponse arg1, Object arg2, Exception arg3) throws Exception {
// TODO Auto-generated method stub
}
public void afterRenderCompletion(RenderRequest arg0, RenderResponse arg1, Object arg2, Exception arg3) throws Exception {
// TODO Auto-generated method stub
}
public void postHandleRender(RenderRequest arg0, RenderResponse arg1, Object arg2, org.springframework.web.portlet.ModelAndView arg3) throws Exception {
// TODO Auto-generated method stub
}
public boolean preHandleAction(ActionRequest arg0, ActionResponse arg1, Object arg2) throws Exception {
// TODO Auto-generated method stub
return false;
}
public boolean preHandleRender(RenderRequest arg0, RenderResponse arg1, Object arg2) throws Exception {
// TODO Auto-generated method stub
return false;
}
}
When I now want to write a Filter - can you give me some advices...