Transcript
Page 1: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Infinite Session Clustering with Apache Shiro & Cassandra

Les Hazlewood @lhazlewoodApache Shiro Project Chair

CTO, Stormpath stormpath.com

ApacheCon 2014

Page 2: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

.com• User Management and Authentication API• Security for your applications• User security workflows• Security best practices• Developer tools, SDKs, libraries

Page 3: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

• Application security framework

• ASF TLP http://shiro.apache.org

• Quick and Easy

• Simplifies Security

What is Apache Shiro?

Page 4: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Web Session Management

Auxiliary Features

AuthorizationAuthentication

CryptographySession

Management

Web Support

Page 5: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Quick Concepts

Subject currentUser = SecurityUtils.getSubject();

currentUser.login(...)currentUser.isPermitted(...)

IBM_USER
Font too small. Can we reduce the line count of this code block?
Page 6: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Session Management DefinedManaging the lifecycle of Subject-specific

temporal data context

Page 7: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Session Management Features• Heterogeneous client access• POJO/J2SE based (IoC friendly)• Event listeners• Host address retention• Inactivity/expiration support (touch())• Transparent web use - HttpSession• Container-Independent Clustering!

Page 8: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Acquiring and Creating SessionsSubject subject =

SecurityUtils.getSubject()

//guarantee a session

Session session = subject.getSession();

//get a session if it exists

subject.getSession(false);

Page 9: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Session APIgetStartTimestamp()

getLastAccessTime()

getAttribute(key)

setAttribute(key, value)

get/setTimeout(long)

touch()

...

Page 10: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Session Management ArchitectureSubject .getSession() Session

Page 11: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Session Management ArchitectureSubject

SessionManager

.getSession() Session

Page 12: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Session Management ArchitectureSubject

SessionManager

.getSession()

Session Factory

Session

Page 13: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Session Management ArchitectureSubject

SessionManager

SessionDAO

.getSession()

Session Factory

Session

Page 14: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Session Management ArchitectureSubject

SessionManager

SessionDAO

.getSession()

Session ID Generator

Session Factory

Session

Page 15: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Session Management ArchitectureSubject

SessionManager

SessionDAO

.getSession()

Session ID Generator

Session Cache

Session Factory

Session

Page 16: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Session Management ArchitectureSubject

SessionManager

SessionDAO

.getSession()

Session ID Generator

Session Cache

Session Factory

Session

Data store

Page 17: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Session Management ArchitectureSubject

SessionManager

SessionDAO

.getSession()

Session ID Generator

Session Cache

Session Factory

ValidationScheduler

Session

Data store

Page 18: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Session Management ArchitectureSubject

SessionManager

SessionDAO

.getSession()

Session ID Generator

Session Cache

Session Factory

ValidationScheduler Session

Listeners

Session

Data store

Page 19: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Session Clustering:Clustered Data Store of Choice

SessionDAO

Session ID Generator

Session Cache

ValidationScheduler

Data store

Page 20: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Web Configuration• web.xml elements

• Protects all URLs

• Innovative Filtering (URL-specific chains)

• JSP Tag support

• Transparent HttpSession support

Page 21: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

web.xml<listener> <listener-class> org.apache.shiro.web.env.EnvironmentLoaderListener </listener-class></listener>

<filter> <filter-name>ShiroFilter</filter-name> <filter-class> org.apache.shiro.web.servlet.ShiroFilter </filter-class></filter>

Page 22: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

web.xml cont’d<filter-mapping> <filter-name>ShiroFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> <dispatcher>ERROR</dispatcher></filter-mapping>

Page 23: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

shiro.ini overview[main]# bean config here

[users]# optional static user accounts (and their roles) here

[roles]# optional static roles (and their permissions) here [urls]# filter chains here

Page 24: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Session Clustering

Page 25: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Two Approaches• Write a SessionDAO

• Use EnterpriseCacheSessionDAO and write a CacheManager

Page 26: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Cassandra SessionDAO

Page 27: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

SessionDAO Concerns

SessionManager

SessionDAO

Session ID Generator

Session Cache

Data store

Page 28: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Custom SessionDAOpublic class MySessionDAO extends AbstractSessionDAO { protected void doCreate(Session s){...} protected void doReadSession(Serializable id){...} protected void delete(Session s){...} protected void update(Session s){...} Collection<Session> getActiveSessions(){...}}

Or

public class MySessionDAO extends CachingSessionDAO {... //enables write-through caching}

Page 29: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Native Web Session Manager[main]sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManagersecurityManager.sessionManager = $sessionManager

Page 30: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Cassandra SessionDAO[main]...

cassandraCluster = com.leshazlewood.samples.shiro.cassandra.ClusterFactory

sessionDAO = com.leshazlewood.samples.shiro.cassandra.CassandraSessionDAOsessionDAO.cluster = $cassandraClustersessionDAO.keyspaceName = shirosessionssessionDAO.tableName = sessions

...

Page 31: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Plug in the SessionDAO[main]...sessionManager.sessionDAO = $sessionDAO

Page 32: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Sessions Table (CQL 3)CREATE TABLE sessions ( id timeuuid PRIMARY KEY, start_ts timestamp, stop_ts timestamp, last_access_ts timestamp, timeout bigint, expired boolean, host varchar, serialized_value blob)

Page 33: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

No Validation Scheduler?

Page 34: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

No Validation Scheduler?Use Cassandra’s TTL

Page 35: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

TTL for session timeout[main]# Cassandra can enforce a TTL. # No need for Shiro to invalidate!

sessionManager.sessionValidationSchedulerEnabled = false

Page 36: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Session Upsert (CQL 3)UPDATE sessions USING TTL $timeout SET start_ts = ?, stop_ts = ?, last_access_ts = ?, timeout = ?, expired = ?, host = ?, serialized_value = ?WHERE id = ?

Page 37: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

But what about tombstones!?!?

Page 38: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Sessions Table (revised)CREATE TABLE sessions ( id timeuuid PRIMARY KEY, start_ts timestamp, stop_ts timestamp, last_access_ts timestamp, timeout bigint, expired boolean, host varchar, serialized_value blob) WITH gc_grace_seconds = 86400 AND compacation = {‘class’:’LeveledCompactionStrategy’}

Page 39: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

But what about row caching?

Page 40: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Row Cache?Probably don’t need it (but maybe in some cases it would be useful)

• SSTable likely in Operating System page cache (off heap)

• DO use Key Cache (very important, enabled by default in 1.2)

Page 41: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Code$ git clone https://github.com/lhazlewood/shiro-cassandra-sample.git

$ cd shiro-cassandra-sample

$ $CASSANDRA_HOME/bin/cassandra

$ mvn jetty:run

Open a browser to http://localhost:8080

Page 42: ApacheCon 2014: Infinite Session Clustering with Apache Shiro & Cassandra

#ApacheCon

Thank You!• [email protected]• Twitter: @lhazlewood• http://www.stormpath.com