From ed8a351a312f22f3e2558b9d1f42b70649e0a23b Mon Sep 17 00:00:00 2001 From: "Crystal.Sea" Date: Wed, 9 Sep 2020 23:51:49 +0800 Subject: [PATCH] TicketGrantingTicketServices --- .../endpoint/CasBaseAuthorizeEndpoint.java | 5 ++ .../authz/cas/endpoint/CasRestV1Endpoint.java | 10 +-- .../InMemoryTicketGrantingTicketServices.java | 54 +++++++++++++ .../service/InMemoryTicketServices.java | 8 +- .../ticket/service/JdbcTicketServices.java | 8 +- .../service/RandomServiceTicketServices.java | 5 -- .../RedisTicketGrantingTicketServices.java | 76 +++++++++++++++++++ .../ticket/service/RedisTicketServices.java | 13 +++- .../ticket/service/TicketServices.java | 6 ++ .../authorize/endpoint/RestTestClient.java | 2 +- .../autoconfigure/CasAutoConfiguration.java | 30 +++++++- 11 files changed, 201 insertions(+), 16 deletions(-) create mode 100644 maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/InMemoryTicketGrantingTicketServices.java create mode 100644 maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/RedisTicketGrantingTicketServices.java diff --git a/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/CasBaseAuthorizeEndpoint.java b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/CasBaseAuthorizeEndpoint.java index 60607d22..10d14c92 100644 --- a/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/CasBaseAuthorizeEndpoint.java +++ b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/CasBaseAuthorizeEndpoint.java @@ -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, diff --git a/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/CasRestV1Endpoint.java b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/CasRestV1Endpoint.java index 2da3dfe0..b651fb11 100644 --- a/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/CasRestV1Endpoint.java +++ b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/CasRestV1Endpoint.java @@ -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); diff --git a/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/InMemoryTicketGrantingTicketServices.java b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/InMemoryTicketGrantingTicketServices.java new file mode 100644 index 00000000..21c80f9d --- /dev/null +++ b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/InMemoryTicketGrantingTicketServices.java @@ -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 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; + } + +} diff --git a/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/InMemoryTicketServices.java b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/InMemoryTicketServices.java index 6651d584..5cd898ca 100644 --- a/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/InMemoryTicketServices.java +++ b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/InMemoryTicketServices.java @@ -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; + } + } diff --git a/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/JdbcTicketServices.java b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/JdbcTicketServices.java index ce7ee841..0c83a6c3 100644 --- a/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/JdbcTicketServices.java +++ b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/JdbcTicketServices.java @@ -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; + } } diff --git a/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/RandomServiceTicketServices.java b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/RandomServiceTicketServices.java index 67c59ded..e2d4f7de 100644 --- a/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/RandomServiceTicketServices.java +++ b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/RandomServiceTicketServices.java @@ -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(); diff --git a/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/RedisTicketGrantingTicketServices.java b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/RedisTicketGrantingTicketServices.java new file mode 100644 index 00000000..ec5154d5 --- /dev/null +++ b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/RedisTicketGrantingTicketServices.java @@ -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; + } + + +} diff --git a/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/RedisTicketServices.java b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/RedisTicketServices.java index 2a8a31bb..cf91e307 100644 --- a/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/RedisTicketServices.java +++ b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/RedisTicketServices.java @@ -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; + } + } diff --git a/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/TicketServices.java b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/TicketServices.java index 6bf46fce..c4155758 100644 --- a/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/TicketServices.java +++ b/maxkey-protocols/maxkey-protocol-cas/src/main/java/org/maxkey/authz/cas/endpoint/ticket/service/TicketServices.java @@ -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); } diff --git a/maxkey-protocols/maxkey-protocol-cas/src/test/java/org/maxkey/web/authorize/endpoint/RestTestClient.java b/maxkey-protocols/maxkey-protocol-cas/src/test/java/org/maxkey/web/authorize/endpoint/RestTestClient.java index c536085a..695fed6c 100644 --- a/maxkey-protocols/maxkey-protocol-cas/src/test/java/org/maxkey/web/authorize/endpoint/RestTestClient.java +++ b/maxkey-protocols/maxkey-protocol-cas/src/test/java/org/maxkey/web/authorize/endpoint/RestTestClient.java @@ -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); } } diff --git a/maxkey-web-maxkey/src/main/java/org/maxkey/autoconfigure/CasAutoConfiguration.java b/maxkey-web-maxkey/src/main/java/org/maxkey/autoconfigure/CasAutoConfiguration.java index cdf1d91c..0c3a5a72 100644 --- a/maxkey-web-maxkey/src/main/java/org/maxkey/autoconfigure/CasAutoConfiguration.java +++ b/maxkey-web-maxkey/src/main/java/org/maxkey/autoconfigure/CasAutoConfiguration.java @@ -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