Skip to content

Commit

Permalink
CAS
Browse files Browse the repository at this point in the history
  • Loading branch information
moon committed Jun 16, 2017
1 parent 41c66a4 commit 280a7a7
Show file tree
Hide file tree
Showing 11 changed files with 1,460 additions and 5 deletions.
34 changes: 29 additions & 5 deletions remind-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<module>remind-guava</module>
<module>remind-model</module>
<module>remind-cache</module>
<module>remind-cas-server</module>
<module>remind-cas-server-integration-redis</module>
</modules>
<packaging>pom</packaging>
<name>remind-parent</name>
Expand All @@ -26,6 +28,10 @@
<fastjson.version>1.2.28</fastjson.version>
<!-- ehcache -->
<ehcache.version>2.6.6</ehcache.version>
<!-- cas -->
<cas.version>4.2.7</cas.version>
<!-- redis -->
<redis.version>2.9.0</redis.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -53,11 +59,16 @@
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>

<!-- guava -->
<dependency>
Expand All @@ -80,6 +91,19 @@
<version>${ehcache.version}</version>
</dependency>

<!-- cas -->
<dependency>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server-core</artifactId>
<version>${cas.version}</version>
</dependency>

<!-- redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${redis.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
65 changes: 65 additions & 0 deletions remind-parent/remind-cas-server-integration-redis/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>remind-parent</artifactId>
<groupId>com.fsmeeting</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>remind-cas-server-integration-redis</artifactId>
<packaging>jar</packaging>

<name>remind-cas-server-integration-redis</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server-core</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>

<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArgs>
<arg>${project.basedir}/src/main/webapp/WEB-INF/lib</arg>
</compilerArgs>
</configuration>
</plugin>-->
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package com.fsmeeting.cas.server.integration.redis;

import org.jasig.cas.ticket.ServiceTicket;
import org.jasig.cas.ticket.Ticket;
import org.jasig.cas.ticket.TicketGrantingTicket;
import org.jasig.cas.ticket.registry.AbstractDistributedTicketRegistry;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collection;

/**
* @Description:
* @Author: yicai.liu
* @Date: 10:45 2017/6/15
*/
@Component("redisTicketRegistry")
public final class RedisTicketRegistry extends AbstractDistributedTicketRegistry implements DisposableBean {

/**
* Redis client.
*/
@Autowired
@Qualifier("jedisSentinelPool")
private JedisSentinelPool jedisPool;

/**
* ST最大空闲时间
*/
@Value("${st.timeToKillInSeconds:10}")
private int stTimeout;

/**
* TGT最大空闲时间
*/
@Value("${tgt.maxTimeToLiveInSeconds:28800}")
private int tgtTimeout;

@Override
protected void updateTicket(final Ticket ticket) {
logger.debug("Updating ticket {}", ticket);
Jedis jedis = jedisPool.getResource();
String ticketId = ticket.getId();
try {
jedis.expire(ticketId.getBytes(), getTimeout(ticket));
} catch (final Exception e) {
logger.error("Failed updating {}", ticket, e);
} finally {
jedis.close();
}
}

@Override
public void addTicket(final Ticket ticket) {
logger.debug("Adding ticket {}", ticket);
Jedis jedis = jedisPool.getResource();
String ticketId = ticket.getId();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(bos);
oos.writeObject(ticket);
} catch (IOException e) {
logger.error("adding ticket {} to redis error.", ticket);
} finally {
try {
if (null != oos) oos.close();
} catch (IOException e) {
logger.error("oos closing error when adding ticket {} to redis.", ticket);
}
}
jedis.setex(ticketId.getBytes(), getTimeout(ticket), bos.toByteArray());
jedis.close();
}

@Override
public boolean deleteSingleTicket(String ticketId) {
logger.debug("Deleting ticket {}", ticketId);
Jedis jedis = jedisPool.getResource();
try {
jedis.del(ticketId.getBytes());
return true;
} catch (final Exception e) {
logger.error("Failed deleting {}", ticketId, e);
return false;
} finally {
jedis.close();
}
}

@Override
public Ticket getTicket(final String ticketId) {
Jedis jedis = jedisPool.getResource();
try {
byte[] value = jedis.get(ticketId.getBytes());
if (null == value) {
logger.error("Failed fetching {}, ticketId is null. ", ticketId);
return null;
}
ByteArrayInputStream bais = new ByteArrayInputStream(value);
ObjectInputStream ois = null;
ois = new ObjectInputStream(bais);
final Ticket t = (Ticket) ois.readObject();
if (t != null) {
return getProxiedTicketInstance(t);
}
} catch (final Exception e) {
logger.error("Failed fetching {}. ", ticketId, e);
} finally {
jedis.close();
}
return null;
}

/**
* {@inheritDoc} * This operation is not supported. * * @throws UnsupportedOperationException if you try and call this operation.
*/
@Override
public Collection<Ticket> getTickets() {
throw new UnsupportedOperationException("GetTickets not supported.");
}

/**
* Destroy the client and shut down. * * @throws Exception the exception
*/
public void destroy() throws Exception {
jedisPool.destroy();
}

@Override
protected boolean needsCallback() {
return true;
}

/**
* Gets the timeout value for the ticket. * * @param t the t * @return the timeout
*/
private int getTimeout(final Ticket t) {
if (t instanceof TicketGrantingTicket) {
return this.tgtTimeout;
} else if (t instanceof ServiceTicket) {
return this.stTimeout;
}
throw new IllegalArgumentException("Invalid ticket type");
}

public void setJedisSentinelPool(JedisSentinelPool jedisPool) {
this.jedisPool = jedisPool;
}

public int getStTimeout() {
return stTimeout;
}

public void setStTimeout(int stTimeout) {
this.stTimeout = stTimeout;
}

public void setTgtTimeout(int tgtTimeout) {
this.tgtTimeout = tgtTimeout;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal:4096}"/>
<property name="maxIdle" value="${redis.pool.maxIdle:200}"/>
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis:3000}"/>
<property name="testOnBorrow" value="${redis.pool.testOnBorrow:true}"/>
<property name="testOnReturn" value="${redis.pool.testOnReturn:true}"/>
</bean>

<bean id="jedisSentinelPool" class="redis.clients.jedis.JedisSentinelPool">
<constructor-arg index="0" value="${redis.sentinel.masterName:mymaster}"/>
<constructor-arg index="1">
<set>
<value>${redis.sentinel.servers:192.168.7.178:26379}</value>
</set>
</constructor-arg>
<constructor-arg index="2" ref="poolConfig"/>
</bean>

</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.fsmeeting.test.cas.server.integration.redis;

import org.junit.Test;

/**
* @Description:
* @Author: yicai.liu
* @Date: 10:53 2017/6/15
*/
//https://github.com/apereo/cas/blob/4.2.x/cas-server-integration-memcached/src/test/java/org/jasig/cas
//@RunWith(Parameterized.class)
public class RedisTicketRegistryTest {


@Test
public void testWriteGetDelete() throws Exception {
//对ticket执行增查删操作
/* final String id = "ST-1234567890ABCDEFGHIJKL-crud";
final ServiceTicket ticket = mock(ServiceTicket.class, withSettings().serializable());
when(ticket.getId()).thenReturn(id);
registry.addTicket(ticket);
final ServiceTicket ticketFromRegistry = (ServiceTicket) registry.getTicket(id);
Assert.assertNotNull(ticketFromRegistry);
Assert.assertEquals(id, ticketFromRegistry.getId());
registry.deleteTicket(id);
Assert.assertNull(registry.getTicket(id));*/
}

}
Loading

0 comments on commit 280a7a7

Please sign in to comment.