All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wicketstuff.shiro.page.LogoutPage Maven / Gradle / Ivy

Go to download

Shiro Security for Apache Wicket. (Shiro was formerly known as Apache Ki and JSecurity)

There is a newer version: 1.5-RC5.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.wicketstuff.shiro.page;

import javax.servlet.http.Cookie;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Page;
import org.apache.wicket.PageParameters;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.Session;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.Model;
import org.apache.wicket.protocol.http.WebResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class LogoutPage extends WebPage
{
  public static final String REDIRECTPAGE_PARAM = "redirectpage";
  
  static Logger log = LoggerFactory.getLogger( LogoutPage.class );

  /**
   * Constructor. The page will immediately redirect to the given url.
   *
   * @param url
   *            The url to redirect to
   */
  public LogoutPage(final CharSequence url)
  {
    doLogoutAndAddRedirect(url, getDelayTime());
  }


  public LogoutPage( final PageParameters parameters ) {
    String page = parameters.getString(REDIRECTPAGE_PARAM);
    Class pageClass;
    if ( page != null ) {
      try {
        pageClass = (Class)Class.forName(page);
      }
      catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
      }
    }
    else {
      pageClass = getApplication().getHomePage();
    }


    this.setStatelessHint( true );
    setResponsePage( pageClass );

    // this should remove the cookie...
    Subject subject = SecurityUtils.getSubject();
    log.info( "logout: "+subject );
    subject.logout();
    Session.get().invalidateNow(); // invalidate the wicket session
    return;
  }

  public LogoutPage( Class pageClass ) {
    doLogoutAndAddRedirect( urlFor(pageClass, null ), getDelayTime() );
  }


  /**
   * Constructor. The page will redirect to the given url after waiting for the given number of
   * seconds.
   *
   * @param url
   *            The url to redirect to
   * @param waitBeforeRedirectInSeconds
   *            The number of seconds the browser should wait before redirecting
   */
  private void doLogoutAndAddRedirect(final CharSequence url, final int waitBeforeRedirectInSeconds)
  {
    this.setStatelessHint( true );

    // this should remove the cookie...
    Subject subject = SecurityUtils.getSubject();
    log.info( "logout: "+subject );
    subject.logout();

    final WebMarkupContainer redirect = new WebMarkupContainer("redirect");
    final String content = waitBeforeRedirectInSeconds + ";URL=" + url;
    redirect.add(new AttributeModifier("content", new Model(content)));
    add(redirect);

    // invalidate the session
    Session.get().invalidateNow(); // invalidate the wicket session

    // HYMMMM
    Cookie c = new Cookie( "rememberMe", "xxx" );
    c.setMaxAge(0);
    ((WebResponse)RequestCycle.get().getResponse()).addCookie( c );
  }

  /**
   * @see org.apache.wicket.Component#isVersioned()
   */
  @Override
  public boolean isVersioned()
  {
    return false;
  }

  public int getDelayTime()
  {
    return 0;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy