Skip to content

Commit

Permalink
table prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
vdiskg committed Dec 1, 2023
1 parent 6812a1a commit 72f014f
Show file tree
Hide file tree
Showing 16 changed files with 1,153 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
Expand All @@ -29,7 +30,9 @@
@EnableAspectJAutoProxy
@Configuration
@PropertySource(value = {"classpath:adminservice.properties"})
@EnableAutoConfiguration
@EnableAutoConfiguration(exclude = {
UserDetailsServiceAutoConfiguration.class,
})
@EnableTransactionManagement
@ComponentScan(basePackageClasses = {ApolloCommonConfig.class,
ApolloBizConfig.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class AdminServiceAutoConfiguration {
Expand All @@ -45,4 +48,18 @@ public FilterRegistrationBean<AdminServiceAuthenticationFilter> adminServiceAuth

return filterRegistrationBean;
}

/**
* for apollo-assembly
*/
@Order(99)
@Configuration
static class AdminServiceSecurityConfigurer extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.httpBasic();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
import org.slf4j.LoggerFactory;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class, ApolloAuditAutoConfiguration.class})
@SpringBootApplication(exclude = {
HibernateJpaAutoConfiguration.class,
ApolloAuditAutoConfiguration.class,
})
public class ApolloApplication {

private static final Logger logger = LoggerFactory.getLogger(ApolloApplication.class);
Expand All @@ -43,37 +44,31 @@ public static void main(String[] args) throws Exception {
*/
ConfigurableApplicationContext commonContext =
new SpringApplicationBuilder(ApolloApplication.class).web(WebApplicationType.NONE).run(args);
logger.info(commonContext.getId() + " isActive: " + commonContext.isActive());
logger.info("commonContext [{}] isActive: {}", commonContext.getId(), commonContext.isActive());

/**
* ConfigService
*/
if (commonContext.getEnvironment().containsProperty("configservice")) {
ConfigurableApplicationContext configContext =
new SpringApplicationBuilder(ConfigServiceApplication.class).parent(commonContext)
.sources(RefreshScope.class).run(args);
logger.info(configContext.getId() + " isActive: " + configContext.isActive());
}
ConfigurableApplicationContext configContext =
new SpringApplicationBuilder(ConfigServiceApplication.class).parent(commonContext)
.sources(RefreshScope.class).run(args);
logger.info("configContext [{}] isActive: {}", configContext.getId(), configContext.isActive());

/**
* AdminService
*/
if (commonContext.getEnvironment().containsProperty("adminservice")) {
ConfigurableApplicationContext adminContext =
new SpringApplicationBuilder(AdminServiceApplication.class).parent(commonContext)
.sources(RefreshScope.class).run(args);
logger.info(adminContext.getId() + " isActive: " + adminContext.isActive());
}
ConfigurableApplicationContext adminContext =
new SpringApplicationBuilder(AdminServiceApplication.class).parent(commonContext)
.sources(RefreshScope.class).run(args);
logger.info("adminContext [{}] isActive: {}" , adminContext.getId(), adminContext.isActive());

/**
* Portal
*/
if (commonContext.getEnvironment().containsProperty("portal")) {
ConfigurableApplicationContext portalContext =
new SpringApplicationBuilder(PortalApplication.class).parent(commonContext)
.sources(RefreshScope.class).run(args);
logger.info(portalContext.getId() + " isActive: " + portalContext.isActive());
}
ConfigurableApplicationContext portalContext =
new SpringApplicationBuilder(PortalApplication.class).parent(commonContext)
.sources(RefreshScope.class).run(args);
logger.info("portalContext [{}] isActive: {}", portalContext.getId(), portalContext.isActive());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
*/
package com.ctrip.framework.apollo.biz;

import com.ctrip.framework.apollo.common.jpa.TablePrefixNamingStrategy;
import com.ctrip.framework.apollo.common.jpa.TablePrefixProperties;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

Expand All @@ -25,4 +28,8 @@
@ComponentScan(basePackageClasses = ApolloBizConfig.class)
public class ApolloBizConfig {

@Bean
public static TablePrefixNamingStrategy tablePrefixNamingStrategy(TablePrefixProperties tablePrefixProperties) {
return new TablePrefixNamingStrategy(tablePrefixProperties.getConfigPrefix());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
*/
package com.ctrip.framework.apollo.common;

import com.ctrip.framework.apollo.common.jpa.TablePrefixProperties;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.security.web.firewall.HttpStatusRequestRejectedHandler;
import org.springframework.security.web.firewall.RequestRejectedHandler;

@EnableConfigurationProperties(TablePrefixProperties.class)
@EnableAutoConfiguration
@Configuration
@ComponentScan(basePackageClasses = ApolloCommonConfig.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2023 Apollo Authors
*
* 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 com.ctrip.framework.apollo.common.jpa;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;

public class TablePrefixNamingStrategy extends PhysicalNamingStrategyStandardImpl implements
PhysicalNamingStrategy {

private static final long serialVersionUID = -5268252502936563292L;

private final String tablePrefix;

public TablePrefixNamingStrategy(String tablePrefix) {
this.tablePrefix = tablePrefix;
}

@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
String tablePrefix = this.tablePrefix;
if (StringUtils.isEmpty(tablePrefix)) {
return name;
}
String entityTableName = name.getText();
String physicalTableName = tablePrefix + entityTableName;
return new Identifier(physicalTableName, name.isQuoted());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2023 Apollo Authors
*
* 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 com.ctrip.framework.apollo.common.jpa;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "spring.jpa.table-prefix")
public class TablePrefixProperties {

/**
* The table name prefix of config module
*/
private String configPrefix = "";

/**
* The table name prefix of portal module
*/
private String portalPrefix = "";

public String getConfigPrefix() {
return this.configPrefix;
}

public void setConfigPrefix(String configPrefix) {
this.configPrefix = configPrefix;
}

public String getPortalPrefix() {
return this.portalPrefix;
}

public void setPortalPrefix(String portalPrefix) {
this.portalPrefix = portalPrefix;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
Expand All @@ -35,7 +36,9 @@
*/

@EnableAspectJAutoProxy
@EnableAutoConfiguration
@EnableAutoConfiguration(exclude = {
UserDetailsServiceAutoConfiguration.class,
})
@Configuration
@EnableTransactionManagement
@PropertySource(value = {"classpath:configservice.properties"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableAspectJAutoProxy
@Configuration
@PropertySource(value = {"classpath:portal.properties"})
@EnableAutoConfiguration(exclude = {LdapAutoConfiguration.class})
@EnableTransactionManagement
@ComponentScan(basePackageClasses = {ApolloCommonConfig.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2023 Apollo Authors
*
* 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 com.ctrip.framework.apollo.portal;

import com.ctrip.framework.apollo.common.jpa.TablePrefixNamingStrategy;
import com.ctrip.framework.apollo.common.jpa.TablePrefixProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PortalApplicationConfig {

@Bean
public static TablePrefixNamingStrategy tablePrefixNamingStrategy(
TablePrefixProperties tablePrefixProperties) {
return new TablePrefixNamingStrategy(tablePrefixProperties.getPortalPrefix());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.net.SocketTimeoutException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.PostConstruct;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.HttpHostConnectException;
Expand Down Expand Up @@ -300,7 +301,9 @@ private <T> T doExecute(HttpMethod method, HttpHeaders extraHeaders, ServiceDTO
}

private String parseHost(ServiceDTO serviceAddress) {
return serviceAddress.getHomepageUrl() + "/";
String homepageUrl = serviceAddress.getHomepageUrl();
Objects.requireNonNull(homepageUrl, "homepageUrl");
return homepageUrl.endsWith("/") ? homepageUrl : homepageUrl + "/";
}

//post,delete,put请求在admin server处理超时情况下不重试
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.ctrip.framework.apollo.portal.spi.configuration;

import com.ctrip.framework.apollo.common.condition.ConditionalOnMissingProfile;
import com.ctrip.framework.apollo.common.jpa.TablePrefixProperties;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.portal.repository.AuthorityRepository;
import com.ctrip.framework.apollo.portal.repository.UserRepository;
Expand Down Expand Up @@ -121,7 +122,8 @@ public static JdbcUserDetailsManager jdbcUserDetailsManager(
PasswordEncoder passwordEncoder,
AuthenticationManagerBuilder auth,
DataSource datasource,
EntityManagerFactory entityManagerFactory) throws Exception {
EntityManagerFactory entityManagerFactory,
TablePrefixProperties tablePrefixProperties) throws Exception {
char openQuote = '`';
char closeQuote = '`';
try {
Expand All @@ -133,19 +135,20 @@ public static JdbcUserDetailsManager jdbcUserDetailsManager(
} catch (Throwable ex) {
//ignore
}
String portalPrefix = tablePrefixProperties.getPortalPrefix();
JdbcUserDetailsManager jdbcUserDetailsManager = auth.jdbcAuthentication()
.passwordEncoder(passwordEncoder).dataSource(datasource)
.usersByUsernameQuery(MessageFormat.format("SELECT {0}Username{1}, {0}Password{1}, {0}Enabled{1} FROM {0}Users{1} WHERE {0}Username{1} = ?", openQuote, closeQuote))
.authoritiesByUsernameQuery(MessageFormat.format("SELECT {0}Username{1}, {0}Authority{1} FROM {0}Authorities{1} WHERE {0}Username{1} = ?", openQuote, closeQuote))
.usersByUsernameQuery(MessageFormat.format("SELECT {0}Username{1}, {0}Password{1}, {0}Enabled{1} FROM {0}{2}Users{1} WHERE {0}Username{1} = ?", openQuote, closeQuote, portalPrefix))
.authoritiesByUsernameQuery(MessageFormat.format("SELECT {0}Username{1}, {0}Authority{1} FROM {0}{2}Authorities{1} WHERE {0}Username{1} = ?", openQuote, closeQuote, portalPrefix))
.getUserDetailsService();

jdbcUserDetailsManager.setUserExistsSql(MessageFormat.format("SELECT {0}Username{1} FROM {0}Users{1} WHERE {0}Username{1} = ?", openQuote, closeQuote));
jdbcUserDetailsManager.setCreateUserSql(MessageFormat.format("INSERT INTO {0}Users{1} ({0}Username{1}, {0}Password{1}, {0}Enabled{1}) values (?,?,?)", openQuote, closeQuote));
jdbcUserDetailsManager.setUpdateUserSql(MessageFormat.format("UPDATE {0}Users{1} SET {0}Password{1} = ?, {0}Enabled{1} = ? WHERE {0}Id{1} = (SELECT u.{0}Id{1} FROM (SELECT {0}Id{1} FROM {0}Users{1} WHERE {0}Username{1} = ?) AS u)", openQuote, closeQuote));
jdbcUserDetailsManager.setDeleteUserSql(MessageFormat.format("DELETE FROM {0}Users{1} WHERE {0}Id{1} = (SELECT u.{0}Id{1} FROM (SELECT {0}Id{1} FROM {0}Users{1} WHERE {0}Username{1} = ?) AS u)", openQuote, closeQuote));
jdbcUserDetailsManager.setCreateAuthoritySql(MessageFormat.format("INSERT INTO {0}Authorities{1} ({0}Username{1}, {0}Authority{1}) values (?,?)", openQuote, closeQuote));
jdbcUserDetailsManager.setDeleteUserAuthoritiesSql(MessageFormat.format("DELETE FROM {0}Authorities{1} WHERE {0}Id{1} in (SELECT a.{0}Id{1} FROM (SELECT {0}Id{1} FROM {0}Authorities{1} WHERE {0}Username{1} = ?) AS a)", openQuote, closeQuote));
jdbcUserDetailsManager.setChangePasswordSql(MessageFormat.format("UPDATE {0}Users{1} SET {0}Password{1} = ? WHERE {0}Id{1} = (SELECT u.{0}Id{1} FROM (SELECT {0}Id{1} FROM {0}Users{1} WHERE {0}Username{1} = ?) AS u)", openQuote, closeQuote));
jdbcUserDetailsManager.setUserExistsSql(MessageFormat.format("SELECT {0}Username{1} FROM {0}{2}Users{1} WHERE {0}Username{1} = ?", openQuote, closeQuote, portalPrefix));
jdbcUserDetailsManager.setCreateUserSql(MessageFormat.format("INSERT INTO {0}{2}Users{1} ({0}Username{1}, {0}Password{1}, {0}Enabled{1}) values (?,?,?)", openQuote, closeQuote, portalPrefix));
jdbcUserDetailsManager.setUpdateUserSql(MessageFormat.format("UPDATE {0}{2}Users{1} SET {0}Password{1} = ?, {0}Enabled{1} = ? WHERE {0}Id{1} = (SELECT u.{0}Id{1} FROM (SELECT {0}Id{1} FROM {0}{2}Users{1} WHERE {0}Username{1} = ?) AS u)", openQuote, closeQuote, portalPrefix));
jdbcUserDetailsManager.setDeleteUserSql(MessageFormat.format("DELETE FROM {0}{2}Users{1} WHERE {0}Id{1} = (SELECT u.{0}Id{1} FROM (SELECT {0}Id{1} FROM {0}{2}Users{1} WHERE {0}Username{1} = ?) AS u)", openQuote, closeQuote, portalPrefix));
jdbcUserDetailsManager.setCreateAuthoritySql(MessageFormat.format("INSERT INTO {0}{2}Authorities{1} ({0}Username{1}, {0}Authority{1}) values (?,?)", openQuote, closeQuote, portalPrefix));
jdbcUserDetailsManager.setDeleteUserAuthoritiesSql(MessageFormat.format("DELETE FROM {0}{2}Authorities{1} WHERE {0}Id{1} in (SELECT a.{0}Id{1} FROM (SELECT {0}Id{1} FROM {0}{2}Authorities{1} WHERE {0}Username{1} = ?) AS a)", openQuote, closeQuote, portalPrefix));
jdbcUserDetailsManager.setChangePasswordSql(MessageFormat.format("UPDATE {0}{2}Users{1} SET {0}Password{1} = ? WHERE {0}Id{1} = (SELECT u.{0}Id{1} FROM (SELECT {0}Id{1} FROM {0}{2}Users{1} WHERE {0}Username{1} = ?) AS u)", openQuote, closeQuote, portalPrefix));

return jdbcUserDetailsManager;
}
Expand Down Expand Up @@ -364,9 +367,10 @@ public JdbcUserDetailsManager jdbcUserDetailsManager(
PasswordEncoder passwordEncoder,
AuthenticationManagerBuilder auth,
DataSource datasource,
EntityManagerFactory entityManagerFactory) throws Exception {
EntityManagerFactory entityManagerFactory,
TablePrefixProperties tablePrefixProperties) throws Exception {
return SpringSecurityAuthAutoConfiguration
.jdbcUserDetailsManager(passwordEncoder, auth, datasource, entityManagerFactory);
.jdbcUserDetailsManager(passwordEncoder, auth, datasource, entityManagerFactory, tablePrefixProperties);
}

@Bean
Expand Down
Loading

0 comments on commit 72f014f

Please sign in to comment.