1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 module hunt.shiro.session.mgt.eis.SessionIdGenerator; 20 21 import hunt.shiro.session.Session; 22 23 import hunt.util.Common; 24 import std.uuid; 25 26 /** 27 * Interface allowing pluggable session ID generation strategies to be used with various {@link SessionDAO} 28 * implementations. 29 * <h2>Usage</h2> 30 * SessionIdGenerators are usually only used when ID generation is separate from creating the 31 * Session record in the EIS data store. Some EIS data stores, such as relational databases, can generate the id 32 * at the same time the record is created, such as when using auto-generated primary keys. In these cases, a 33 * SessionIdGenerator does not need to be configured. 34 * <p/> 35 * However, if you want to customize how session IDs are created before persisting the Session record into the data 36 * store, you can implement this interface and typically inject it into an {@link AbstractSessionDAO} instance. 37 * 38 * @see hunt.shiro.session.mgt.eis.JavaUuidSessionIdGenerator JavaUuidSessionIdGenerator 39 * @see hunt.shiro.session.mgt.eis.RandomSessionIdGenerator RandomSessionIdGenerator 40 */ 41 interface SessionIdGenerator { 42 43 /** 44 * Generates a new ID to be applied to the specified {@code Session} instance. 45 * 46 * @param session the {@link Session} instance to which the ID will be applied. 47 * @return the id to assign to the specified {@link Session} instance before adding a record to the EIS data store. 48 */ 49 string generateId(Session session); 50 51 } 52 53 54 /** 55 * {@link SessionIdGenerator} that generates string values of UUID as the session IDs. 56 * 57 * @since 1.0 58 */ 59 class UuidSessionIdGenerator : SessionIdGenerator { 60 61 /** 62 * Ignores the method argument and simply returns 63 * {@code UUID}.{@link java.util.UUID#randomUUID() randomUUID()}.{@code toString()}. 64 * 65 * @param session the {@link Session} instance to which the ID will be applied. 66 * @return the string value of the JDK's next {@link UUID#randomUUID() randomUUID()}. 67 */ 68 string generateId(Session session) { 69 return randomUUID().toString(); 70 } 71 }