jpa ticket存储,也就是可以让cas将ticket存储在后端的数据库,比如oracle,mysql等。这个功能可以让我们来构建高可用的cas 服务器集群。下面我会描述具体的配置,让cas如何将ticket存入后端oracle数据库,在实际生产应用中我也是存入oracle数据库的。其实存在哪个数据库都一样,相互转换很容易,只需要更改一下数据源及数据库驱动就可以了。cas/webapp/WEB-INF/spring-configuration/ticketRegistry.xml 找到这个文件,对于jpa ticket的配置,只需要更改这个文件。(对于cas spring配置文件,可参看cas入门之二spring配置文件)
步骤:
<bean id="ticketRegistry" class="org.jasig.cas.ticket.registry.DefaultTicketRegistry" />
替换为
<bean id="ticketRegistry" class="org.jasig.cas.ticket.registry.JpaTicketRegistry" />
并在ticketRegistry.xml文件中加入如下bean配置:
<!-- 让@PersistenceUnit and @PersistenceContext 自动注入 EntityManager/Factory 实例 --> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="generateDdl" value="true"/> <property name="showSql" value="true" /> </bean> </property> <property name="jpaProperties"> <props> <!-- 数据库驱动 --> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory" /> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 数据源配置略--> <bean id="dataSource" /> <bean id="cleanerLock" class="org.jasig.cas.ticket.registry.support.JpaLockingStrategy" p:uniqueId="${host.name}" p:applicationId="cas-ticket-registry-cleaner" />
这里主要说一下cleanerLock,它会在数据库是产生locks表:
Name Type Nullable Default Comments
--------------- ------------- -------- ------- --------
APPLICATION_ID VARCHAR2(255)
EXPIRATION_DATE DATE Y
UNIQUE_ID VARCHAR2(255) Y
在集群环境,多个cas 服务器共享同一个数据库节点,当需要清理ticket时,会出现表争用,但是cas通过cleanerLock解决了这个问题,同时并不会影响应用的性能。
<bean id="ticketRegistryCleaner" class="org.jasig.cas.ticket.registry.support.DefaultTicketRegistryCleaner" p:ticketRegistry-ref="ticketRegistry" />
更改为
<bean id="ticketRegistryCleaner" class="org.jasig.cas.ticket.registry.support.DefaultTicketRegistryCleaner" p:ticketRegistry-ref="ticketRegistry" p:lock-ref="cleanerLock" />
其余的不变。
主要的jar:
hibernate-core hibernate-entitymanager hibernate-jpa-2.0-api hibernate-commons-annotations hibernate-validator
另外还有相应数据库的jar,在此不列举。到此关于jpa ticket配置更改完毕。
jpa ticket配置更改完成,重新部署cas,会在相应的数据库,生成5张表:
LOCKS RegisteredServiceImpl SERVICETICKET TICKETGRANTINGTICKET rs_attributes
如果不能生成,这5张表在oracle的建表sql:
create table LOCKS( APPLICATION_ID VARCHAR2(255) not null, EXPIRATION_DATE DATE, UNIQUE_ID VARCHAR2(255) ) ; alter table LOCKS add primary key (APPLICATION_ID); create table RS_ATTRIBUTES( REGISTEREDSERVICEIMPL_ID NUMBER(19) not null, A_NAME VARCHAR2(255) not null, A_ID NUMBER(10) not null ); alter table RS_ATTRIBUTES add primary key (REGISTEREDSERVICEIMPL_ID, A_ID); create table RegisteredServiceImpl ( expression_type VARCHAR2(15) DEFAULT 'ant' not null, id number(19,0) not null, allowedToProxy char(1) not null, anonymousAccess char(1) not null, description varchar2(255), enabled char(1) not null, evaluation_order number(10,0) not null, ignoreAttributes char(1) not null, name varchar2(255), serviceId varchar2(255), ssoEnabled char(1) not null, theme varchar2(255), username_attr varchar2(256), primary key (id)); create table SERVICETICKET ( ID varchar2(255) not null, NUMBER_OF_TIMES_USED number(10,0), CREATION_TIME number(19,0), EXPIRATION_POLICY blob not null, LAST_TIME_USED number(19,0), PREVIOUS_LAST_TIME_USED number(19,0), FROM_NEW_LOGIN char(1) not null, TICKET_ALREADY_GRANTED char(1) not null, SERVICE blob not null, ticketGrantingTicket_ID varchar2(255), primary key (ID)); create table TICKETGRANTINGTICKET ( ID varchar2(255) not null, NUMBER_OF_TIMES_USED number(10,0), CREATION_TIME number(19,0), EXPIRATION_POLICY blob not null, LAST_TIME_USED number(19,0), PREVIOUS_LAST_TIME_USED number(19,0), AUTHENTICATION blob not null, EXPIRED char(1) not null, SERVICES_GRANTED_ACCESS_TO blob not null, ticketGrantingTicket_ID varchar2(255), primary key (ID)); alter table SERVICETICKET add constraint FK7645ADE132A2C0E5 foreign key (ticketGrantingTicket_ID) references TICKETGRANTINGTICKET; alter table TICKETGRANTINGTICKET add constraint FKB4C4CDDE32A2C0E5 foreign key (ticketGrantingTicket_ID) references TICKETGRANTINGTICKET; alter table rs_attributes add constraint FK4322E153C595E1F foreign key (RegisteredServiceImpl_id) references RegisteredServiceImpl; --下面两个索引是为了提高应用性能 CREATE INDEX ST_TGT_FK_I ON SERVICETICKET (TICKETGRANTINGTICKET_ID) COMPUTE STATISTICS; CREATE INDEX TGT_TGT_FK_I ON TICKETGRANTINGTICKET (TICKETGRANTINGTICKET_ID) COMPUTE STATISTICS;
发表评论(对文章涉及的知识点还有疑问,可以在这里留言,老高看到后会及时回复的。)