42
#ApacheCon Infinite Session Clustering with Apache Shiro & Cassandra Les Hazlewood @lhazlewood Apache Shiro Project Chair CTO, Stormpath stormpath.com ApacheCon 2014

Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Infinite  Session  Clustering  with    Apache  Shiro  &  Cassandra  

Les  Hazlewood  @lhazlewood  Apache  Shiro  Project  Chair  

CTO,  Stormpath  stormpath.com  

ApacheCon  2014  

Page 2: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

 .com  •  User  Management  and  Authen=ca=on  API  •  Security  for  your  applica=ons  •  User  security  workflows  •  Security  best  prac=ces  •  Developer  tools,  SDKs,  libraries  

Page 3: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

•  Applica=on  security  framework  

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

•  Quick  and  Easy  •  Simplifies  Security  

What  is  Apache  Shiro?  

Page 4: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Web  Session  Management  

Auxiliary  Features  

Authoriza=on  Authen=ca=on  

Cryptography  Session  

Management  

Web  Support  

Page 5: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Quick  Concepts  

Subject currentUser = SecurityUtils.getSubject();

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

Page 6: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Session  Management  Defined  Managing  the  lifecycle  of  Subject-­‐specific  temporal  data  context  

Page 7: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Session  Management  Features  •  Heterogeneous  client  access  •  POJO/J2SE  based  (IoC  friendly)  •  Event  listeners  •  Host  address  reten=on  •  Inac=vity/expira=on  support  (touch())  •  Transparent  web  use  -­‐  HKpSession  •  Container-­‐Independent  Clustering!  

Page 8: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Acquiring  and  CreaKng  Sessions  Subject subject = SecurityUtils.getSubject() //guarantee a session Session session = subject.getSession(); //get a session if it exists subject.getSession(false);

Page 9: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Session  API  getStartTimestamp()

getLastAccessTime()

getAttribute(key)

setAttribute(key, value)

get/setTimeout(long)

touch()

...

Page 10: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Session  Management  Architecture  Subject   .getSession()  à   Session  

Page 11: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Session  Management  Architecture  Subject  

SessionManager  

.getSession()  à   Session  

Page 12: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Session  Management  Architecture  Subject  

SessionManager  

.getSession()  à  

Session  Factory  

Session  

Page 13: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Session  Management  Architecture  Subject  

SessionManager  

SessionDAO  

.getSession()  à  

Session  Factory  

Session  

Page 14: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Session  Management  Architecture  Subject  

SessionManager  

SessionDAO  

.getSession()  à  

Session  ID  Generator  

Session  Factory  

Session  

Page 15: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Session  Management  Architecture  Subject  

SessionManager  

SessionDAO  

.getSession()  à  

Session  ID  Generator  

Session  Cache  

Session  Factory  

Session  

Page 16: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Session  Management  Architecture  Subject  

SessionManager  

SessionDAO  

.getSession()  à  

Session  ID  Generator  

Session  Cache  

Session  Factory  

Session  

Data  store  

Page 17: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Session  Management  Architecture  Subject  

SessionManager  

SessionDAO  

.getSession()  à  

Session  ID  Generator  

Session  Cache  

Session  Factory  

Valida=on  Scheduler  

Session  

Data  store  

Page 18: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Session  Management  Architecture  Subject  

SessionManager  

SessionDAO  

.getSession()  à  

Session  ID  Generator  

Session  Cache  

Session  Factory  

Valida=on  Scheduler   Session  

Listeners  

Session  

Data  store  

Page 19: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Session  Clustering:  Clustered  Data  Store  of  Choice  

SessionDAO  

Session  ID  Generator  

Session  Cache  

Valida=on  Scheduler  

Data  store  

Page 20: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Web  ConfiguraKon  •  web.xml  elements  

•  Protects  all  URLs  

•  Innova=ve  Filtering  (URL-­‐specific  chains)  

•  JSP  Tag  support  

•  Transparent  HKpSession  support  

Page 21: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#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: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#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: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#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: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Session  Clustering  

Page 25: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Two  Approaches  •  Write  a  SessionDAO  

 •  Use  EnterpriseCacheSessionDAO  and  

write  a  CacheManager  

Page 26: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Cassandra  SessionDAO  

Page 27: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

SessionDAO  Concerns  SessionManager  

SessionDAO  

Session  ID  Generator  

Session  Cache  

Data  store  

Page 28: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Custom  SessionDAO  public 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: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

NaKve  Web  Session  Manager  [main] sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager securityManager.sessionManager = $sessionManager

Page 30: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

Cassandra  SessionDAO  [main] ... cassandraCluster = com.leshazlewood.samples.shiro.cassandra.ClusterFactory

sessionDAO = com.leshazlewood.samples.shiro.cassandra.CassandraSessionDAO sessionDAO.cluster = $cassandraCluster sessionDAO.keyspaceName = shirosessions sessionDAO.tableName = sessions ...

Page 31: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

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

Page 32: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#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: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

No  ValidaKon  Scheduler?  

Page 34: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

No  ValidaKon  Scheduler?  Use  Cassandra’s  TTL  

Page 35: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

TTL  for  session  Kmeout  [main] # Cassandra can enforce a TTL. # No need for Shiro to invalidate! sessionManager.sessionValidationSchedulerEnabled = false

Page 36: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#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: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

But  what  about  tombstones!?!?  

Page 38: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#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: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

But  what  about  row  caching?  

Page 40: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

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

•  SSTable  likely  in  Opera=ng  System  page  cache  (off  heap)  

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

Page 41: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#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: Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ · #ApacheCon+ Infinite’SessionClustering’with’ Apache’Shiro’&’Cassandra’ LesHazlewood@ lhazlewood’

#ApacheCon  

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