TicketGrantingTicketServices
This commit is contained in:
@@ -46,6 +46,11 @@ public class CasBaseAuthorizeEndpoint extends AuthorizeBaseEndpoint{
|
||||
@Qualifier("casTicketServices")
|
||||
protected TicketServices ticketServices;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("casTicketGrantingTicketServices")
|
||||
protected TicketServices casTicketGrantingTicketServices;
|
||||
|
||||
|
||||
public void setContentType(
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
|
||||
@@ -81,7 +81,7 @@ public class CasRestV1Endpoint extends CasBaseAuthorizeEndpoint{
|
||||
|
||||
TicketGrantingTicketImpl ticketGrantingTicket=new TicketGrantingTicketImpl("Random",WebContext.getAuthentication(),null);
|
||||
|
||||
String ticket=ticketServices.createTicket(ticketGrantingTicket);
|
||||
String ticket=casTicketGrantingTicketServices.createTicket(ticketGrantingTicket);
|
||||
String location = applicationConfig.getServerPrefix()+"/authz/cas/v1/tickets/" + ticket;
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("location", location);
|
||||
@@ -110,7 +110,7 @@ public class CasRestV1Endpoint extends CasBaseAuthorizeEndpoint{
|
||||
@RequestParam(value=CasConstants.PARAMETER.REST_PASSWORD,required=false) String password){
|
||||
try {
|
||||
TicketGrantingTicketImpl ticketGrantingTicketImpl =
|
||||
(TicketGrantingTicketImpl) ticketServices.consumeTicket(ticketGrantingTicket);
|
||||
(TicketGrantingTicketImpl) casTicketGrantingTicketServices.get(ticketGrantingTicket);
|
||||
|
||||
AppsCasDetails casDetails=casDetailsService.getAppDetails(casService);
|
||||
|
||||
@@ -133,7 +133,7 @@ public class CasRestV1Endpoint extends CasBaseAuthorizeEndpoint{
|
||||
HttpServletResponse response){
|
||||
try {
|
||||
TicketGrantingTicketImpl ticketGrantingTicketImpl =
|
||||
(TicketGrantingTicketImpl) ticketServices.consumeTicket(ticketGrantingTicket);
|
||||
(TicketGrantingTicketImpl) casTicketGrantingTicketServices.get(ticketGrantingTicket);
|
||||
if(ticketGrantingTicketImpl != null) {
|
||||
return new ResponseEntity<>("", HttpStatus.OK);
|
||||
}
|
||||
@@ -152,7 +152,7 @@ public class CasRestV1Endpoint extends CasBaseAuthorizeEndpoint{
|
||||
HttpServletResponse response){
|
||||
try {
|
||||
TicketGrantingTicketImpl ticketGrantingTicketImpl =
|
||||
(TicketGrantingTicketImpl) ticketServices.consumeTicket(ticketGrantingTicket);
|
||||
(TicketGrantingTicketImpl) casTicketGrantingTicketServices.remove(ticketGrantingTicket);
|
||||
if(ticketGrantingTicketImpl != null) {
|
||||
return new ResponseEntity<>("", HttpStatus.OK);
|
||||
}
|
||||
@@ -184,7 +184,7 @@ public class CasRestV1Endpoint extends CasBaseAuthorizeEndpoint{
|
||||
UserInfo userInfo =WebContext.getUserInfo();
|
||||
TicketGrantingTicketImpl ticketGrantingTicket=new TicketGrantingTicketImpl("Random",WebContext.getAuthentication(),null);
|
||||
|
||||
String ticket=ticketServices.createTicket(ticketGrantingTicket);
|
||||
String ticket=casTicketGrantingTicketServices.createTicket(ticketGrantingTicket);
|
||||
String location = applicationConfig.getServerPrefix()+"/authz/cas/v1/tickets/" + ticket;
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("location", location);
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package org.maxkey.authz.cas.endpoint.ticket.service;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.ehcache.UserManagedCache;
|
||||
import org.ehcache.config.builders.ExpiryPolicyBuilder;
|
||||
import org.ehcache.config.builders.UserManagedCacheBuilder;
|
||||
import org.maxkey.authz.cas.endpoint.ticket.Ticket;
|
||||
|
||||
|
||||
public class InMemoryTicketGrantingTicketServices extends RandomServiceTicketServices {
|
||||
|
||||
protected final static UserManagedCache<String, Ticket> casTicketGrantingTicketStore =
|
||||
UserManagedCacheBuilder.newUserManagedCacheBuilder(String.class, Ticket.class)
|
||||
.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofDays(2)))
|
||||
.build(true);
|
||||
|
||||
|
||||
@Override
|
||||
public void store(String ticketId, Ticket ticket) {
|
||||
casTicketGrantingTicketStore.put(ticketId, ticket);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ticket remove(String ticketId) {
|
||||
Ticket ticket=casTicketGrantingTicketStore.get(ticketId);
|
||||
casTicketGrantingTicketStore.remove(ticketId);
|
||||
return ticket;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ticket get(String ticketId) {
|
||||
Ticket ticket=casTicketGrantingTicketStore.get(ticketId);
|
||||
return ticket;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -34,7 +34,7 @@ public class InMemoryTicketServices extends RandomServiceTicketServices {
|
||||
|
||||
|
||||
@Override
|
||||
protected void store(String ticketId, Ticket ticket) {
|
||||
public void store(String ticketId, Ticket ticket) {
|
||||
casTicketStore.put(ticketId, ticket);
|
||||
}
|
||||
|
||||
@@ -45,4 +45,10 @@ public class InMemoryTicketServices extends RandomServiceTicketServices {
|
||||
return ticket;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ticket get(String ticket) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ public class JdbcTicketServices extends RandomServiceTicketServices {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void store(String ticketId, Ticket ticket) {
|
||||
public void store(String ticketId, Ticket ticket) {
|
||||
jdbcTemplate.update(insertAuthenticationSql,
|
||||
new Object[] { ticket, new SqlLobValue(SerializationUtils.serialize(ticket)) }, new int[] {
|
||||
Types.VARCHAR, Types.BLOB });
|
||||
@@ -86,4 +86,10 @@ public class JdbcTicketServices extends RandomServiceTicketServices {
|
||||
public void setDeleteAuthenticationSql(String deleteAuthenticationSql) {
|
||||
this.deleteAuthenticationSql = deleteAuthenticationSql;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ticket get(String ticketId) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,11 +28,6 @@ public abstract class RandomServiceTicketServices implements TicketServices {
|
||||
//private RandomValueStringGenerator generator = new RandomValueStringGenerator();
|
||||
|
||||
private DefaultUniqueTicketIdGenerator generator=new DefaultUniqueTicketIdGenerator();
|
||||
|
||||
|
||||
protected abstract void store(String ticketId, Ticket ticket);
|
||||
|
||||
protected abstract Ticket remove(String ticket);
|
||||
|
||||
public String createTicket(Ticket ticket) {
|
||||
//String code = generator.generate();
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package org.maxkey.authz.cas.endpoint.ticket.service;
|
||||
|
||||
import org.maxkey.authz.cas.endpoint.ticket.Ticket;
|
||||
import org.maxkey.persistence.redis.RedisConnection;
|
||||
import org.maxkey.persistence.redis.RedisConnectionFactory;
|
||||
|
||||
|
||||
public class RedisTicketGrantingTicketServices extends RandomServiceTicketServices {
|
||||
|
||||
protected int serviceTicketValiditySeconds = 60 * 60 * 24 * 2; //default 2 day.
|
||||
|
||||
RedisConnectionFactory connectionFactory;
|
||||
|
||||
public static String PREFIX="REDIS_CAS_TICKET_TGT_";
|
||||
/**
|
||||
* @param connectionFactory
|
||||
*/
|
||||
public RedisTicketGrantingTicketServices(RedisConnectionFactory connectionFactory) {
|
||||
super();
|
||||
this.connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public RedisTicketGrantingTicketServices() {
|
||||
|
||||
}
|
||||
|
||||
public void setConnectionFactory(RedisConnectionFactory connectionFactory) {
|
||||
this.connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void store(String ticketId, Ticket ticket) {
|
||||
RedisConnection conn=connectionFactory.getConnection();
|
||||
conn.setexObject(PREFIX+ticketId, serviceTicketValiditySeconds, ticket);
|
||||
conn.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ticket remove(String ticketId) {
|
||||
RedisConnection conn=connectionFactory.getConnection();
|
||||
Ticket ticket = conn.getObject(PREFIX+ticketId);
|
||||
conn.delete(PREFIX+ticketId);
|
||||
conn.close();
|
||||
return ticket;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ticket get(String ticketId) {
|
||||
RedisConnection conn=connectionFactory.getConnection();
|
||||
Ticket ticket = conn.getObject(PREFIX+ticketId);
|
||||
conn.close();
|
||||
return ticket;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -24,11 +24,12 @@ import org.maxkey.persistence.redis.RedisConnectionFactory;
|
||||
|
||||
public class RedisTicketServices extends RandomServiceTicketServices {
|
||||
|
||||
|
||||
protected int serviceTicketValiditySeconds = 60 * 10; //default 10 minutes.
|
||||
|
||||
RedisConnectionFactory connectionFactory;
|
||||
|
||||
public static String PREFIX="REDIS_CAS_TICKET_";
|
||||
public static String PREFIX="REDIS_CAS_TICKET_ST_";
|
||||
/**
|
||||
* @param connectionFactory
|
||||
*/
|
||||
@@ -49,7 +50,7 @@ public class RedisTicketServices extends RandomServiceTicketServices {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void store(String ticketId, Ticket ticket) {
|
||||
public void store(String ticketId, Ticket ticket) {
|
||||
RedisConnection conn=connectionFactory.getConnection();
|
||||
conn.setexObject(PREFIX+ticketId, serviceTicketValiditySeconds, ticket);
|
||||
conn.close();
|
||||
@@ -64,5 +65,13 @@ public class RedisTicketServices extends RandomServiceTicketServices {
|
||||
return ticket;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ticket get(String ticketId) {
|
||||
RedisConnection conn=connectionFactory.getConnection();
|
||||
Ticket ticket = conn.getObject(PREFIX+ticketId);
|
||||
conn.close();
|
||||
return ticket;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -38,5 +38,11 @@ public interface TicketServices {
|
||||
*/
|
||||
Ticket consumeTicket(String ticketId)
|
||||
throws Exception;
|
||||
|
||||
public void store(String ticketId, Ticket ticket);
|
||||
|
||||
public Ticket remove(String ticket);
|
||||
|
||||
public Ticket get(String ticketId);
|
||||
|
||||
}
|
||||
|
||||
@@ -47,6 +47,6 @@ public class RestTestClient {
|
||||
for (Map.Entry entry : mapEntries) {
|
||||
System.out.println(entry.getKey() + ":" + entry.getValue());
|
||||
}
|
||||
//client.destroyTicketGrantingTicket(profile,webContext);
|
||||
client.destroyTicketGrantingTicket(profile,webContext);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,10 @@
|
||||
|
||||
package org.maxkey.autoconfigure;
|
||||
|
||||
import org.maxkey.authz.cas.endpoint.ticket.service.InMemoryTicketGrantingTicketServices;
|
||||
import org.maxkey.authz.cas.endpoint.ticket.service.InMemoryTicketServices;
|
||||
import org.maxkey.authz.cas.endpoint.ticket.service.JdbcTicketServices;
|
||||
import org.maxkey.authz.cas.endpoint.ticket.service.RedisTicketGrantingTicketServices;
|
||||
import org.maxkey.authz.cas.endpoint.ticket.service.RedisTicketServices;
|
||||
import org.maxkey.authz.cas.endpoint.ticket.service.TicketServices;
|
||||
import org.maxkey.constants.ConstantsProperties;
|
||||
@@ -67,7 +69,33 @@ public class CasAutoConfiguration implements InitializingBean {
|
||||
return casTicketServices;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* TicketServices.
|
||||
* @param persistence int
|
||||
* @param validity int
|
||||
* @return casTicketServices
|
||||
*/
|
||||
@Bean(name = "casTicketGrantingTicketServices")
|
||||
public TicketServices casTicketGrantingTicketServices(
|
||||
@Value("${config.server.persistence}") int persistence,
|
||||
@Value("${config.login.remeberme.validity}") int validity,
|
||||
JdbcTemplate jdbcTemplate,
|
||||
RedisConnectionFactory jedisConnectionFactory) {
|
||||
TicketServices casTicketServices = null;
|
||||
if (persistence == 0) {
|
||||
casTicketServices = new InMemoryTicketGrantingTicketServices();
|
||||
_logger.debug("InMemoryTicketServices");
|
||||
} else if (persistence == 1) {
|
||||
//
|
||||
//casTicketServices = new JdbcTicketServices(jdbcTemplate);
|
||||
_logger.debug("JdbcTicketServices not support ");
|
||||
} else if (persistence == 2) {
|
||||
casTicketServices = new RedisTicketGrantingTicketServices(jedisConnectionFactory);
|
||||
_logger.debug("RedisTicketServices");
|
||||
}
|
||||
return casTicketServices;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
Reference in New Issue
Block a user