├── .gitignore ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main ├── java │ └── yukinami │ │ └── example │ │ └── saas │ │ ├── Application.java │ │ ├── DbConfig.java │ │ ├── WebConfig.java │ │ ├── annotation │ │ ├── RootResource.java │ │ └── TenantResource.java │ │ ├── db │ │ └── TenantRoutingDataSource.java │ │ ├── domain │ │ ├── Inventory.java │ │ ├── InventoryRepository.java │ │ ├── Tenant.java │ │ └── TenantRepository.java │ │ ├── interceptor │ │ ├── RoutingDataSourceInterceptor.java │ │ └── TenantResolveInterceptor.java │ │ ├── util │ │ └── TenantContextHolder.java │ │ └── web │ │ ├── RootInventoryController.java │ │ └── TenantInventoryController.java └── resources │ ├── config │ └── application.yml │ ├── db │ └── migration │ │ └── V1__init.sql │ └── logback.xml └── test └── java └── yukinami └── example └── saas ├── RootInventoryControllerInIntegrationTests.java └── TenantInventoryControllerIntegrationTests.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.sw[op] 2 | .DS_Store 3 | 4 | # IDEA 5 | .idea 6 | *.iml 7 | *.ipr 8 | *.iws 9 | out 10 | 11 | # Gradle 12 | .gradle 13 | build 14 | 15 | # Maven 16 | target 17 | dist 18 | node 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Spring Boot SaaS Demo 2 | ======================== 3 | 4 | This application demonstrates a simple saas web application built on spring boot. 5 | 6 | To run the applicaiton, simply run this command: 7 | 8 | ./gradlew bootRun 9 | 10 | Then go to: 11 | 12 | * http://localhost:8080/b1/tenant_inventories/ to show inventories belongs to tenant b1 13 | * http://localhost:8080/root_inventories/ to match root resource (not belongs to any tenant) 14 | * http://localhost:8080/b1/root_inventories/ show the usage of @TenantResource annotation which canbe used at method level to override the class level annotation 15 | * http://localhost:8080/b1/tenant_inventories/root show the usage of @RootResource annotation which is opposite to @TenantResource 16 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | maven { url "http://repo.spring.io/snapshot" } 5 | maven { url "http://repo.spring.io/milestone" } 6 | } 7 | dependencies { 8 | classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.6.RELEASE") 9 | } 10 | } 11 | 12 | apply plugin: 'idea' 13 | apply plugin: 'java' 14 | apply plugin: 'spring-boot' 15 | 16 | sourceCompatibility = 1.8 17 | targetCompatibility = 1.8 18 | version = '1.0' 19 | 20 | task wrapper(type: Wrapper) { 21 | gradleVersion = '1.9' 22 | distributionUrl = 'http://services.gradle.org/distributions/gradle-1.9-all.zip' 23 | } 24 | 25 | repositories { 26 | mavenCentral() 27 | maven { url "http://repo.spring.io/snapshot" } 28 | maven { url "http://repo.spring.io/milestone" } 29 | } 30 | 31 | dependencies { 32 | compile("org.springframework.boot:spring-boot-starter-web") 33 | compile("org.springframework.boot:spring-boot-starter-logging") 34 | compile("org.springframework.boot:spring-boot-starter-data-jpa") 35 | runtime("org.flywaydb:flyway-core") 36 | runtime("org.hsqldb:hsqldb") 37 | runtime("org.apache.tomcat:tomcat-jdbc") 38 | testCompile("org.springframework.boot:spring-boot-starter-test") 39 | testCompile(group: 'junit', name: 'junit', version: '4.11') 40 | } -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yukinami/spring-boot-saas/29574bca71b89e7473102edda6294cb0b4b4d80b/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Nov 06 16:56:57 CST 2014 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-1.12-bin.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'spring-boot-saas' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/yukinami/example/saas/Application.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.boot.builder.SpringApplicationBuilder; 6 | import org.springframework.boot.context.web.SpringBootServletInitializer; 7 | import org.springframework.context.annotation.ComponentScan; 8 | import org.springframework.context.annotation.Configuration; 9 | import org.springframework.web.context.WebApplicationContext; 10 | 11 | /** 12 | * Created by snowblink on 14/11/6. 13 | */ 14 | @Configuration 15 | @EnableAutoConfiguration 16 | @ComponentScan 17 | public class Application extends SpringBootServletInitializer { 18 | 19 | 20 | public static void main(String[] args) { 21 | SpringApplication.run(Application.class, args); 22 | } 23 | 24 | @Override 25 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 26 | return application.sources(Application.class, DbConfig.class); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/yukinami/example/saas/DbConfig.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas; 2 | 3 | import javax.sql.DataSource; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 9 | import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 10 | import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; 11 | import org.springframework.boot.context.properties.ConfigurationProperties; 12 | import org.springframework.context.annotation.*; 13 | import yukinami.example.saas.db.TenantRoutingDataSource; 14 | import yukinami.example.saas.domain.Tenant; 15 | import yukinami.example.saas.domain.TenantRepository; 16 | 17 | /** 18 | * Created by snowblink on 14/11/7. 19 | */ 20 | @Configuration 21 | public class DbConfig { 22 | 23 | @Autowired 24 | private DataSourceProperties properties; 25 | 26 | public DataSource defaultDataSource(){ 27 | return DataSourceBuilder 28 | .create(this.properties.getClassLoader()) 29 | .url(this.properties.getUrl()) 30 | .username(this.properties.getUsername()) 31 | .password(this.properties.getPassword()).build(); 32 | } 33 | 34 | @Bean 35 | public DataSource dataSource() { 36 | TenantRoutingDataSource routingDataSource = new TenantRoutingDataSource(); 37 | routingDataSource.setDefaultTargetDataSource(defaultDataSource()); 38 | routingDataSource.setTargetDataSources(new HashMap()); 39 | return routingDataSource; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/yukinami/example/saas/WebConfig.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas; 2 | 3 | import javax.servlet.MultipartConfigElement; 4 | import javax.sql.DataSource; 5 | import java.util.ArrayList; 6 | import java.util.HashMap; 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 12 | import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; 13 | import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; 14 | import org.springframework.boot.context.embedded.ServletRegistrationBean; 15 | import org.springframework.context.annotation.Bean; 16 | import org.springframework.context.annotation.Configuration; 17 | import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; 18 | import org.springframework.web.servlet.DispatcherServlet; 19 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; 20 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 21 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 22 | import yukinami.example.saas.domain.Tenant; 23 | import yukinami.example.saas.domain.TenantRepository; 24 | import yukinami.example.saas.interceptor.RoutingDataSourceInterceptor; 25 | import yukinami.example.saas.interceptor.TenantResolveInterceptor; 26 | 27 | /** 28 | * Created by snowblink on 14/11/6. 29 | */ 30 | @EnableWebMvc 31 | @Configuration 32 | public class WebConfig extends WebMvcConfigurerAdapter { 33 | 34 | @Autowired 35 | private DataSource dataSource; 36 | 37 | @Autowired 38 | private DataSourceProperties properties; 39 | 40 | @Autowired 41 | private TenantRepository tenantRepository; 42 | 43 | 44 | @Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME) 45 | public DispatcherServlet dispatcherServlet() { 46 | return new DispatcherServlet(); 47 | } 48 | 49 | @Autowired(required = false) 50 | private MultipartConfigElement multipartConfig; 51 | 52 | @Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME) 53 | public ServletRegistrationBean dispatcherServletRegistration() { 54 | Iterable tenants = this.tenantRepository.findAll(); 55 | 56 | ServletRegistrationBean registration = new ServletRegistrationBean( 57 | dispatcherServlet(), getServletMappings(tenants)); 58 | registration.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); 59 | if (this.multipartConfig != null) { 60 | registration.setMultipartConfig(this.multipartConfig); 61 | } 62 | 63 | registerDataSource(tenants); 64 | 65 | return registration; 66 | } 67 | 68 | 69 | protected void registerDataSource(Iterable tenants) { 70 | Map targetDataSources = new HashMap<>(); 71 | for (Tenant tenant : tenants) { 72 | 73 | DataSourceBuilder factory = DataSourceBuilder 74 | .create(this.properties.getClassLoader()) 75 | .url(this.properties.getUrl()) 76 | .username(tenant.getDbu()) 77 | .password(tenant.getEdbpwd()); 78 | 79 | 80 | targetDataSources.put(tenant.getBusinessName(), factory.build()); 81 | } 82 | 83 | ((AbstractRoutingDataSource) this.dataSource).setTargetDataSources(targetDataSources); 84 | ((AbstractRoutingDataSource) this.dataSource).afterPropertiesSet(); 85 | } 86 | 87 | protected String[] getServletMappings(Iterable tenants) { 88 | List mappings = new ArrayList<>(); 89 | for (Tenant tenant : tenants) { 90 | mappings.add("/" + tenant.getBusinessName() + "/*"); 91 | } 92 | 93 | mappings.add("/*"); 94 | 95 | return mappings.toArray(new String[0]); 96 | } 97 | 98 | /** 99 | * Encrypt the business name to prevent from abusing accessing. 100 | * Need to resolve to the actual business name in {@link yukinami.example.saas.interceptor.TenantResolveInterceptor#resolve(String)} 101 | * 102 | * @param businessName 103 | * @return 104 | */ 105 | protected String encryptBusinessName(String businessName) { 106 | return businessName; 107 | } 108 | 109 | @Override 110 | public void addInterceptors(InterceptorRegistry registry) { 111 | registry.addInterceptor(new TenantResolveInterceptor()); 112 | registry.addInterceptor(new RoutingDataSourceInterceptor()); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/main/java/yukinami/example/saas/annotation/RootResource.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas.annotation; 2 | 3 | import java.lang.annotation.Retention; 4 | import java.lang.annotation.Target; 5 | 6 | import static java.lang.annotation.ElementType.METHOD; 7 | import static java.lang.annotation.ElementType.TYPE; 8 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 9 | 10 | /** 11 | * specify that the resource should be accessed by root 12 | * 13 | */ 14 | @Target({METHOD, TYPE}) 15 | @Retention(RUNTIME) 16 | public @interface RootResource { 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/yukinami/example/saas/annotation/TenantResource.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas.annotation; 2 | 3 | import java.lang.annotation.Retention; 4 | import java.lang.annotation.Target; 5 | 6 | import static java.lang.annotation.ElementType.METHOD; 7 | import static java.lang.annotation.ElementType.TYPE; 8 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 9 | 10 | /** 11 | * specify that the resource should be accessed by tenant 12 | * 13 | */ 14 | @Target({METHOD, TYPE}) 15 | @Retention(RUNTIME) 16 | public @interface TenantResource { 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/yukinami/example/saas/db/TenantRoutingDataSource.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas.db; 2 | 3 | import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; 4 | import yukinami.example.saas.util.TenantContextHolder; 5 | 6 | /** 7 | * Created by snowblink on 14/11/7. 8 | */ 9 | public class TenantRoutingDataSource extends AbstractRoutingDataSource { 10 | 11 | @Override 12 | protected Object determineCurrentLookupKey() { 13 | return TenantContextHolder.getBusinessName(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/yukinami/example/saas/domain/Inventory.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas.domain; 2 | 3 | import javax.persistence.*; 4 | 5 | /** 6 | * Created by snowblink on 14/11/10. 7 | */ 8 | @Entity 9 | @Table(name="inventory_vw") 10 | public class Inventory { 11 | 12 | @Id 13 | @GeneratedValue 14 | private Long id; 15 | 16 | @Column 17 | private String name; 18 | 19 | public Long getId() { 20 | return id; 21 | } 22 | 23 | public void setId(Long id) { 24 | this.id = id; 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/yukinami/example/saas/domain/InventoryRepository.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas.domain; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | 5 | /** 6 | * Created by snowblink on 14/11/10. 7 | */ 8 | public interface InventoryRepository extends CrudRepository { 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/yukinami/example/saas/domain/Tenant.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas.domain; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.Id; 7 | 8 | /** 9 | * Created by snowblink on 14/11/6. 10 | */ 11 | @Entity 12 | public class Tenant { 13 | 14 | @Id 15 | @GeneratedValue 16 | private Long id; 17 | 18 | @Column 19 | private String dbu; 20 | 21 | @Column 22 | private String edbpwd; 23 | 24 | private String businessName; 25 | 26 | public Long getId() { 27 | return id; 28 | } 29 | 30 | public void setId(Long id) { 31 | this.id = id; 32 | } 33 | 34 | public String getDbu() { 35 | return dbu; 36 | } 37 | 38 | public void setDbu(String dbu) { 39 | this.dbu = dbu; 40 | } 41 | 42 | public String getEdbpwd() { 43 | return edbpwd; 44 | } 45 | 46 | public void setEdbpwd(String edbpwd) { 47 | this.edbpwd = edbpwd; 48 | } 49 | 50 | public String getBusinessName() { 51 | return businessName; 52 | } 53 | 54 | public void setBusinessName(String businessName) { 55 | this.businessName = businessName; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/yukinami/example/saas/domain/TenantRepository.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas.domain; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | 5 | /** 6 | * Created by snowblink on 14/11/6. 7 | */ 8 | public interface TenantRepository extends CrudRepository { 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/yukinami/example/saas/interceptor/RoutingDataSourceInterceptor.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas.interceptor; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | import javax.servlet.http.HttpServletResponse; 5 | 6 | import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; 7 | import yukinami.example.saas.util.TenantContextHolder; 8 | 9 | /** 10 | * Created by snowblink on 14/11/7. 11 | */ 12 | public class RoutingDataSourceInterceptor extends HandlerInterceptorAdapter { 13 | 14 | @Override 15 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 16 | String businessName = (String)request.getAttribute(TenantResolveInterceptor.TENANT_BUSINESS_NAME_KEY); 17 | TenantContextHolder.setBusinessName(businessName); 18 | return true; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/yukinami/example/saas/interceptor/TenantResolveInterceptor.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas.interceptor; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | import javax.servlet.http.HttpServletResponse; 5 | 6 | import org.springframework.core.annotation.AnnotationUtils; 7 | import org.springframework.util.StringUtils; 8 | import org.springframework.web.method.HandlerMethod; 9 | import org.springframework.web.servlet.NoHandlerFoundException; 10 | import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; 11 | import org.springframework.web.util.UrlPathHelper; 12 | import yukinami.example.saas.annotation.RootResource; 13 | import yukinami.example.saas.annotation.TenantResource; 14 | 15 | /** 16 | * Created by snowblink on 14/11/6. 17 | */ 18 | public class TenantResolveInterceptor extends HandlerInterceptorAdapter { 19 | 20 | public static final String TENANT_BUSINESS_NAME_KEY = "businessName"; 21 | 22 | private UrlPathHelper urlPathHelper = new UrlPathHelper(); 23 | 24 | @Override 25 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 26 | String businessName = resolve(urlPathHelper.getServletPath(request)); 27 | request.setAttribute(TENANT_BUSINESS_NAME_KEY, businessName); 28 | 29 | // restrict the access 30 | HandlerMethod method = (HandlerMethod) handler; 31 | TenantResource tenantResource = method.getMethodAnnotation(TenantResource.class); 32 | RootResource rootResource = method.getMethodAnnotation(RootResource.class); 33 | 34 | boolean isRootResource = false; 35 | 36 | // get annotation from class when no annotation is specified 37 | if (tenantResource == null && rootResource == null) { 38 | tenantResource = AnnotationUtils.findAnnotation(method.getBeanType(), TenantResource.class); 39 | rootResource = AnnotationUtils.findAnnotation(method.getBeanType(), RootResource.class); 40 | } 41 | 42 | // still with no annotation, set default 43 | if (tenantResource == null && rootResource == null) { 44 | isRootResource = true; 45 | } 46 | 47 | // tenant resource 48 | if (tenantResource != null && StringUtils.isEmpty(businessName)) { 49 | throw new NoHandlerFoundException(request.getMethod(), request.getRequestURI(), null); 50 | } 51 | 52 | // root resource 53 | if ((rootResource != null || isRootResource) && !StringUtils.isEmpty(businessName)) { 54 | throw new NoHandlerFoundException(request.getMethod(), request.getRequestURI(), null); 55 | } 56 | 57 | return super.preHandle(request, response, handler); 58 | } 59 | 60 | /** 61 | * resolve to the actual business name 62 | * 63 | * @param servletPath 64 | * @return 65 | */ 66 | protected String resolve(String servletPath) { 67 | if (servletPath.length() > 0 ) { 68 | return servletPath.substring(1); 69 | } 70 | 71 | return ""; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/yukinami/example/saas/util/TenantContextHolder.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas.util; 2 | 3 | import org.springframework.util.Assert; 4 | 5 | /** 6 | * Created by snowblink on 14/11/7. 7 | */ 8 | public class TenantContextHolder { 9 | 10 | private static final ThreadLocal contextHolder = 11 | new ThreadLocal(); 12 | 13 | public static void setBusinessName(String businessName) { 14 | Assert.notNull(businessName, "businessName cannot be null"); 15 | contextHolder.set(businessName); 16 | } 17 | 18 | public static String getBusinessName() { 19 | return contextHolder.get(); 20 | } 21 | 22 | public static void clearBusinessName() { 23 | contextHolder.remove(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/yukinami/example/saas/web/RootInventoryController.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas.web; 2 | 3 | import java.io.PrintWriter; 4 | import java.io.StringWriter; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | import yukinami.example.saas.annotation.RootResource; 10 | import yukinami.example.saas.annotation.TenantResource; 11 | import yukinami.example.saas.domain.Inventory; 12 | import yukinami.example.saas.domain.InventoryRepository; 13 | 14 | /** 15 | * Created by snowblink on 14-10-14. 16 | */ 17 | @RestController 18 | @RequestMapping("/root_inventories") 19 | @RootResource 20 | public class RootInventoryController { 21 | 22 | @Autowired 23 | private InventoryRepository inventoryRepository; 24 | 25 | @RequestMapping("/") 26 | public String list() { 27 | 28 | StringWriter string = new StringWriter(); 29 | PrintWriter writer = new PrintWriter(string); 30 | Iterable inventories = inventoryRepository.findAll(); 31 | 32 | for (Inventory inventory : inventories) { 33 | writer.println(inventory.getName()); 34 | } 35 | 36 | return string.toString(); 37 | } 38 | 39 | @RequestMapping("/tenant") 40 | @TenantResource 41 | public String tenantList() { 42 | 43 | StringWriter string = new StringWriter(); 44 | PrintWriter writer = new PrintWriter(string); 45 | Iterable inventories = inventoryRepository.findAll(); 46 | 47 | for (Inventory inventory : inventories) { 48 | writer.println(inventory.getName()); 49 | } 50 | 51 | return string.toString(); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/yukinami/example/saas/web/TenantInventoryController.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas.web; 2 | 3 | import java.io.PrintWriter; 4 | import java.io.StringWriter; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | import yukinami.example.saas.annotation.RootResource; 10 | import yukinami.example.saas.annotation.TenantResource; 11 | import yukinami.example.saas.domain.Inventory; 12 | import yukinami.example.saas.domain.InventoryRepository; 13 | import yukinami.example.saas.domain.TenantRepository; 14 | 15 | /** 16 | * Created by snowblink on 14-10-14. 17 | */ 18 | @RestController 19 | @RequestMapping("/tenant_inventories") 20 | @TenantResource 21 | public class TenantInventoryController { 22 | 23 | @Autowired 24 | private InventoryRepository inventoryRepository; 25 | 26 | @RequestMapping("/") 27 | public String list() { 28 | 29 | StringWriter string = new StringWriter(); 30 | PrintWriter writer = new PrintWriter(string); 31 | Iterable inventories = inventoryRepository.findAll(); 32 | 33 | for (Inventory inventory : inventories) { 34 | writer.println(inventory.getName()); 35 | } 36 | 37 | return string.toString(); 38 | } 39 | 40 | @RequestMapping("/root") 41 | @RootResource 42 | public String rootList() { 43 | 44 | StringWriter string = new StringWriter(); 45 | PrintWriter writer = new PrintWriter(string); 46 | Iterable inventories = inventoryRepository.findAll(); 47 | 48 | for (Inventory inventory : inventories) { 49 | writer.println(inventory.getName()); 50 | } 51 | 52 | return string.toString(); 53 | } 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/resources/config/application.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | spring: 4 | jpa: 5 | hibernate: 6 | ddl-auto: validate -------------------------------------------------------------------------------- /src/main/resources/db/migration/V1__init.sql: -------------------------------------------------------------------------------- 1 | CREATE USER "tom" PASSWORD 'tom' ADMIN; 2 | CREATE USER "jack" PASSWORD 'jack' ADMIN; 3 | 4 | CREATE TABLE tenant ( 5 | id BIGINT IDENTITY PRIMARY KEY, 6 | dbu VARCHAR(25) NOT NULL, 7 | edbpwd VARCHAR(255), 8 | business_name VARCHAR(255) NOT NULL 9 | ); 10 | 11 | INSERT INTO tenant (dbu, edbpwd, business_name) VALUES ('tom', 'tom', 'b1'); 12 | INSERT INTO tenant (dbu, edbpwd, business_name) VALUES ('jack', 'jack', 'b2'); 13 | 14 | 15 | CREATE TABLE inventory ( 16 | id BIGINT IDENTITY PRIMARY KEY, 17 | name VARCHAR(55), 18 | tenant_dbu VARCHAR(16) NOT NULL , 19 | tenant_id BIGINT NOT NULL 20 | ); 21 | 22 | 23 | INSERT INTO inventory (name, tenant_dbu, tenant_id) VALUES('tom''s item', 'tom', 1); 24 | INSERT INTO inventory (name, tenant_dbu, tenant_id) VALUES('jack''s item', 'jack', 2); 25 | 26 | CREATE VIEW inventory_vw AS 27 | SELECT id, name 28 | FROM inventory 29 | WHERE tenant_dbu = CURRENT_USER; 30 | 31 | CREATE TRIGGER tr_inventory_before_insert 32 | BEFORE INSERT ON inventory 33 | REFERENCING NEW AS newrow FOR EACH ROW 34 | BEGIN ATOMIC 35 | IF (CURRENT_USER = 'root') THEN 36 | SET newrow.tenant_dbu = CURRENT_USER; 37 | END IF; 38 | END -------------------------------------------------------------------------------- /src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /src/test/java/yukinami/example/saas/RootInventoryControllerInIntegrationTests.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.IntegrationTest; 8 | import org.springframework.boot.test.SpringApplicationConfiguration; 9 | import org.springframework.http.MediaType; 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 | import org.springframework.test.context.web.WebAppConfiguration; 12 | import org.springframework.test.web.servlet.MockMvc; 13 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 14 | import org.springframework.web.context.WebApplicationContext; 15 | 16 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 17 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 18 | 19 | /** 20 | * Created by snowblink on 14/11/11. 21 | */ 22 | @RunWith(SpringJUnit4ClassRunner.class) 23 | @SpringApplicationConfiguration(classes = Application.class) 24 | @WebAppConfiguration 25 | public class RootInventoryControllerInIntegrationTests { 26 | 27 | @Autowired 28 | private WebApplicationContext wac; 29 | 30 | private MockMvc mockMvc; 31 | 32 | @Before 33 | public void setup() { 34 | this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); 35 | } 36 | 37 | @Test 38 | public void testList() throws Exception { 39 | this.mockMvc.perform(get("/root_inventories/")).andExpect(status().isOk()); 40 | this.mockMvc.perform(get("/b1/root_inventories/").servletPath("/b1")).andExpect(status().isNotFound()); 41 | } 42 | 43 | @Test 44 | public void testTenantList() throws Exception { 45 | this.mockMvc.perform(get("/b2/root_inventories/tenant").servletPath("/b2")).andExpect(status().isOk()); 46 | this.mockMvc.perform(get("/root_inventories/tenant")).andExpect(status().isNotFound()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/yukinami/example/saas/TenantInventoryControllerIntegrationTests.java: -------------------------------------------------------------------------------- 1 | package yukinami.example.saas; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.IntegrationTest; 8 | import org.springframework.boot.test.SpringApplicationConfiguration; 9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 | import org.springframework.test.context.web.WebAppConfiguration; 11 | import org.springframework.test.web.servlet.MockMvc; 12 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 13 | import org.springframework.web.context.WebApplicationContext; 14 | 15 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 16 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 17 | 18 | /** 19 | * Created by snowblink on 14/11/11. 20 | */ 21 | @RunWith(SpringJUnit4ClassRunner.class) 22 | @SpringApplicationConfiguration(classes = Application.class) 23 | @WebAppConfiguration 24 | public class TenantInventoryControllerIntegrationTests { 25 | 26 | @Autowired 27 | private WebApplicationContext wac; 28 | 29 | private MockMvc mockMvc; 30 | 31 | @Before 32 | public void setup() { 33 | this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); 34 | } 35 | 36 | @Test 37 | public void testList() throws Exception { 38 | this.mockMvc.perform(get("/b1/tenant_inventories/").servletPath("/b1")).andExpect(status().isOk()); 39 | this.mockMvc.perform(get("/tenant_inventories/")).andExpect(status().isNotFound()); 40 | } 41 | 42 | @Test 43 | public void estRootList() throws Exception { 44 | this.mockMvc.perform(get("/tenant_inventories/root")).andExpect(status().isOk()); 45 | this.mockMvc.perform(get("/b1/tenant_inventories/root").servletPath("/b1")).andExpect(status().isNotFound()); 46 | } 47 | } 48 | --------------------------------------------------------------------------------