Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
10 changes: 5 additions & 5 deletions EbfEmployees/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.flywaydb</groupId>-->
<!-- <artifactId>flyway-core</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down Expand Up @@ -106,7 +106,7 @@
<configuration>
<forkCount>3</forkCount>
<reuseForks>true</reuseForks>
<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
<argLine>-Xmx1024m</argLine>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.itekako.EbfEmployees;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.transaction.annotation.EnableTransactionManagement;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.itekako.EbfEmployees.auth;

import org.springframework.core.annotation.Order;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Order(10000)
public class DatabaseFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String database = request.getHeader("Selected-Database");
if(database == null){
filterChain.doFilter (request,response);
return;
}
TenantAuthentificationToken authentication = (TenantAuthentificationToken) SecurityContextHolder.getContext().getAuthentication();
authentication.setDatabase(database);
filterChain.doFilter (request,response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
roles.add(new SimpleGrantedAuthority("ROLE_" + role.asString()));
}
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(subject,null, roles));
new TenantAuthentificationToken(subject, roles));
super.doFilterInternal(request, response, chain);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.itekako.EbfEmployees.auth;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

public class TenantAuthentificationToken extends UsernamePasswordAuthenticationToken {

private final String userId;
private String database;

public TenantAuthentificationToken(String userId,Collection<? extends GrantedAuthority> authorities) {
super(userId,null,authorities);
this.userId = userId;
}


public String getDatabase(){
return database;
}

public void setDatabase(String database){
this.database = database;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.itekako.EbfEmployees.configurations;

import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SessionConfiguration {


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.itekako.EbfEmployees.configurations;

import com.itekako.EbfEmployees.auth.DatabaseFilter;
import com.itekako.EbfEmployees.auth.JwtFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
Expand All @@ -23,9 +24,9 @@ public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/swagger-ui.html", "/api-docs/**", "/webjars/**", "/swagger-ui/**", "/v3/api-docs/**").permitAll() //allow swagger
.antMatchers("/api/**").hasRole("admin").anyRequest().authenticated()
.antMatchers("/api/**").authenticated()
.and().csrf().disable().cors()
.and().addFilter(new JwtFilter(authenticationManager(), authConfiguration));
.and().addFilter(new JwtFilter(authenticationManager(), authConfiguration)).addFilterAfter(new DatabaseFilter(),JwtFilter.class);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ public ResponseEntity generateEmployees(@PathVariable Long id) throws ResourceNo
employeeService.generateEmployees(id);
return ResponseEntity.noContent().build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.itekako.EbfEmployees.database;

import com.itekako.EbfEmployees.auth.TenantAuthentificationToken;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class CurrentTenantIdentifierResolverImpl implements CurrentTenantIdentifierResolver {


@Override
public String resolveCurrentTenantIdentifier() {
SecurityContext context = SecurityContextHolder.getContext();
if(context.getAuthentication() == null)return "admin";
Authentication authentication = context.getAuthentication();
if(authentication instanceof AnonymousAuthenticationToken){
return "admin";
}
return ((TenantAuthentificationToken) authentication).getDatabase();
}

@Override
public boolean validateExistingCurrentSessions() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.itekako.EbfEmployees.database;

import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl;
import org.hibernate.engine.jdbc.connections.spi.AbstractMultiTenantConnectionProvider;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Component
public class MapMultiTenantConnectionProvider extends AbstractMultiTenantConnectionProvider {

private Map<String, ConnectionProvider> connectionProviderMap
= new HashMap<>();

public MapMultiTenantConnectionProvider() throws IOException {
initConnectionProviderForTenant("user");
initConnectionProviderForTenant("admin");
}

@Override
protected ConnectionProvider getAnyConnectionProvider() {
return connectionProviderMap.values()
.iterator()
.next();
}

@Override
protected ConnectionProvider selectConnectionProvider(String s) {
return connectionProviderMap.get(s);
}

private void initConnectionProviderForTenant(String tenantId)
throws IOException {
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream(
String.format("/hibernate-database-%s.properties", tenantId)));
DriverManagerConnectionProviderImpl connectionProvider
= new DriverManagerConnectionProviderImpl();
connectionProvider.configure(properties);
this.connectionProviderMap.put(tenantId, connectionProvider);
}
}
13 changes: 3 additions & 10 deletions EbfEmployees/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
logging.level.root=debug
spring.datasource.initialization-mode=always
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/ebf_employees
spring.datasource.username=ebf_db_user
spring.datasource.password=ebf_db_password

flyway.user=ebf_db_user
flyway.password=ebf_db_password
flyway.schemas=public
flyway.url=jdbc:postgresql://localhost:5432/ebf_employees
spring.jpa.properties.hibernate.multiTenancy=DATABASE
spring.jpa.properties.hibernate.tenant_identifier_resolver=com.itekako.EbfEmployees.database.CurrentTenantIdentifierResolverImpl
spring.jpa.properties.hibernate.multi_tenant_connection_provider=com.itekako.EbfEmployees.database.MapMultiTenantConnectionProvider
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
log4j.logger.org.springframework.transaction=INFO

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
hibernate.connection.driver_class=org.postgresql.Driver
hibernate.connection.url=jdbc:postgresql://localhost:5432/ebf_employees2
hibernate.connection.username=ebf_db_user2
hibernate.connection.password=ebf_db_password2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
hibernate.connection.driver_class=org.postgresql.Driver
hibernate.connection.url=jdbc:postgresql://localhost:54322/ebf_employees1
hibernate.connection.username=ebf_db_user1
hibernate.connection.password=ebf_db_password1
12 changes: 0 additions & 12 deletions EbfEmployees/src/test/resources/application.properties

This file was deleted.

Loading