Skip to content

Instantly share code, notes, and snippets.

@zwaldowski
Created April 8, 2013 17:49
Show Gist options
  • Save zwaldowski/5338863 to your computer and use it in GitHub Desktop.
Save zwaldowski/5338863 to your computer and use it in GitHub Desktop.
App Engine Cloud Endpoints backed by Shiro

This is a quick starting point for a web application on Google App Engine with Cloud Endpoints powered by Guice and authenticated with Shiro. Nice job with the SPI documentation, by the way, Google... :-/

package com.dizzytechnology.appengine.server.generic;
import com.google.api.server.spi.guice.GuiceSystemServiceServletModule;
import com.google.inject.*;
import com.google.inject.servlet.GuiceServletContextListener;
import org.apache.shiro.config.Ini;
import org.apache.shiro.guice.web.ShiroWebModule;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
public class MainContextListener extends GuiceServletContextListener {
private ServletContext servletContext = null;
public MainContextListener() {}
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
servletContext = servletContextEvent.getServletContext();
super.contextInitialized(servletContextEvent);
}
@Override
protected Injector getInjector() {
return Guice.createInjector(new MainShiroWebModule(servletContext), ShiroWebModule.guiceFilterModule(), new MainServletModule(), new MainModule());
}
private class MainServletModule extends GuiceSystemServiceServletModule {
@Override protected void configureServlets() {
Set<Class<?>> serviceClasses = new HashSet<>();
// add endpoint classes
// i.e., serviceClasses.add(AccountV1.class);
this.serveGuiceSystemServiceServlet("/_ah/spi/*", serviceClasses);
// filters, generic bindings
// i.e., filter("/*").through(ObjectifyFilter.class);
// serve bindings
// serve("path").with(Servlet.class);
}
}
private static class MainModule extends AbstractModule {
@Override
protected void configure() {
// misc injection, i.e., an ObjectifyFilter and ObjectifyFactor
}
}
private class MainShiroWebModule extends ShiroWebModule {
public MainShiroWebModule(ServletContext servletContext) {
super(servletContext);
}
@Override
protected void configureShiroWeb() {
// Shiro class binding
// i.e., bind(SessionDAO.class).to(EnterpriseCacheSessionDAO.class);
// Shiro realm binding
// i.e., bindRealm().to(DatabaseRealm.class);
// try/catch!
// Shiro filter chains
// Always remember to define your filter chains based on a FIRST MATCH WINS policy!
// Should work with App Engine Cloud Endpoints
// i.e.:
// addFilterChain("/logout", LOGOUT);
// addFilterChain("/_ah/api/myapi/v1/account/logout", NO_SESSION_CREATION, TOKEN_LOGOUT);
}
@Provides @Singleton
Ini loadShiroIni() throws MalformedURLException {
URL iniUrl = servletContext.getResource("/WEB-INF/shiro.ini");
return Ini.fromResourcePath("url:" + iniUrl.toExternalForm());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment