-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
moon
committed
Jun 16, 2017
1 parent
41c66a4
commit 280a7a7
Showing
11 changed files
with
1,460 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
172 changes: 172 additions & 0 deletions
172
...n-redis/src/main/java/com/fsmeeting/cas/server/integration/redis/RedisTicketRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...-cas-server-integration-redis/src/main/resources/META-INF/spring/ticketRegistry-redis.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
29 changes: 29 additions & 0 deletions
29
...rc/test/java/com/fsmeeting/test/cas/server/integration/redis/RedisTicketRegistryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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));*/ | ||
} | ||
|
||
} |
Oops, something went wrong.