From a06c9bc69a47cbd9bee7f261d9473e668d61abd6 Mon Sep 17 00:00:00 2001 From: qraddx Date: Wed, 19 Jan 2022 15:35:11 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20Refactoring=20code.=20=E5=8F=91?= =?UTF-8?q?=E5=8F=B7=E5=99=A8=20=E4=BC=98=E5=8C=96=20=E9=80=82=E9=85=8DOra?= =?UTF-8?q?cle=20=E3=80=81PG=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/config/WebSecurityConfigurer.java | 33 +++++++++++----- .../sequence/range/impl/db/BaseDbHelper.java | 39 ++++++++++++++++++- .../sequence/range/impl/db/DbSeqRangeMgr.java | 6 ++- .../impl/db/provider/MysqlSqlProvider.java | 8 ++-- .../impl/db/provider/OracleSqlProvider.java | 35 ++++++++++++++++- .../impl/db/provider/PostgreSqlProvider.java | 10 ++++- .../range/impl/db/provider/SqlProvider.java | 20 ++++++---- .../impl/db/provider/SqlProviderFactory.java | 9 +++++ .../db/provider/SqlServerSqlProvider.java | 25 ------------ 9 files changed, 134 insertions(+), 51 deletions(-) delete mode 100644 pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlServerSqlProvider.java diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java index f0efab4e2..276b7313b 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java @@ -19,17 +19,20 @@ package com.pig4cloud.pigx.auth.config; +import com.pig4cloud.pigx.common.security.component.PigxDaoAuthenticationProvider; +import com.pig4cloud.pigx.common.security.grant.CustomAppAuthenticationProvider; import com.pig4cloud.pigx.common.security.handler.FormAuthenticationFailureHandler; import com.pig4cloud.pigx.common.security.handler.MobileLoginSuccessHandler; import com.pig4cloud.pigx.common.security.handler.SsoLogoutSuccessHandler; import com.pig4cloud.pigx.common.security.handler.TenantSavedRequestAwareAuthenticationSuccessHandler; -import com.pig4cloud.pigx.common.security.mobile.MobileSecurityConfigurer; +import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.annotation.Order; import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @@ -46,6 +49,7 @@ import org.springframework.security.web.authentication.logout.LogoutSuccessHandl @Primary @Order(90) @Configuration +@RequiredArgsConstructor public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { @Override @@ -56,7 +60,22 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { .failureHandler(authenticationFailureHandler()).and().logout() .logoutSuccessHandler(logoutSuccessHandler()).deleteCookies("JSESSIONID").invalidateHttpSession(true) .and().authorizeRequests().antMatchers("/token/**", "/actuator/**", "/mobile/**").permitAll() - .anyRequest().authenticated().and().csrf().disable().apply(mobileSecurityConfigurer()); + .anyRequest().authenticated().and().csrf().disable(); + } + + /** + * 自定义 provider 列表注入 + * @param auth AuthenticationManagerBuilder + */ + @Override + protected void configure(AuthenticationManagerBuilder auth) { + PigxDaoAuthenticationProvider daoAuthenticationProvider = new PigxDaoAuthenticationProvider(); + daoAuthenticationProvider.setPasswordEncoder(passwordEncoder()); + + // 处理默认的密码模式认证 + auth.authenticationProvider(daoAuthenticationProvider); + // 自定义的认证模式 + auth.authenticationProvider(new CustomAppAuthenticationProvider()); } /** @@ -99,15 +118,9 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { return new TenantSavedRequestAwareAuthenticationSuccessHandler(); } - @Bean - public MobileSecurityConfigurer mobileSecurityConfigurer() { - return new MobileSecurityConfigurer(); - } - /** - * https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-storage-updated - * Encoded password does not look like BCrypt - * @return PasswordEncoder + * 密码处理器 + * @return 动态密码处理器 {类型}密文 */ @Bean public PasswordEncoder passwordEncoder() { diff --git a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/BaseDbHelper.java b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/BaseDbHelper.java index 3137219f5..e7a8e9a4c 100755 --- a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/BaseDbHelper.java +++ b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/BaseDbHelper.java @@ -1,6 +1,7 @@ package com.pig4cloud.pigx.common.sequence.range.impl.db; import cn.hutool.core.util.RandomUtil; +import cn.hutool.core.util.StrUtil; import cn.hutool.extra.spring.SpringUtil; import com.pig4cloud.pigx.common.sequence.exception.SeqException; import com.pig4cloud.pigx.common.sequence.range.impl.db.provider.SqlProviderFactory; @@ -58,7 +59,7 @@ abstract class BaseDbHelper { try { conn = dataSource.getConnection(); stmt = conn.prepareStatement(SQL_PROVIDER_FACTORY.getInsertRangeSql().replace("#tableName", tableName)); - stmt.setLong(1, RandomUtil.randomLong()); + stmt.setLong(1, RandomUtil.randomLong(12)); stmt.setString(2, name); stmt.setLong(3, stepStart); stmt.setTimestamp(4, new Timestamp(System.currentTimeMillis())); @@ -169,4 +170,40 @@ abstract class BaseDbHelper { } } + public static Boolean existTable(DataSource dataSource, String tableName) { + + String existTableSql = SQL_PROVIDER_FACTORY.getExistTableSql(); + if (StrUtil.isBlank(existTableSql)) { + return true; + } + Connection conn = null; + Statement stmt = null; + + try { + conn = dataSource.getConnection(); + stmt = conn.createStatement(); + ResultSet resultSet = stmt + .executeQuery(SQL_PROVIDER_FACTORY.getExistTableSql().replace("#tableName", tableName)); + + if (!resultSet.next()) { + return false; + } + + int count = resultSet.getInt(1); + + if (0 == count) { + return true; + } + } + catch (SQLException e) { + throw new SeqException(e); + } + finally { + closeQuietly(stmt); + closeQuietly(conn); + } + + return false; + } + } diff --git a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/DbSeqRangeMgr.java b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/DbSeqRangeMgr.java index efcddb55d..17be26438 100755 --- a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/DbSeqRangeMgr.java +++ b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/DbSeqRangeMgr.java @@ -69,7 +69,11 @@ public class DbSeqRangeMgr implements SeqRangeMgr { @Override public void init() { checkParam(); - BaseDbHelper.createTable(getDataSource(), getRealTableName()); + + // 判断是否需要创建表 + if (BaseDbHelper.existTable(getDataSource(), getRealTableName())) { + BaseDbHelper.createTable(getDataSource(), getRealTableName()); + } } private boolean isEmpty(String str) { diff --git a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/MysqlSqlProvider.java b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/MysqlSqlProvider.java index 8f3b44cf2..4492cf71d 100644 --- a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/MysqlSqlProvider.java +++ b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/MysqlSqlProvider.java @@ -13,12 +13,14 @@ import org.springframework.stereotype.Component; public class MysqlSqlProvider implements SqlProvider { /** - * 获取插入范围语句 + * 获取建表语句 * @return */ @Override - public String getInsertRangeSql() { - return "INSERT IGNORE INTO #tableName(id,name,value,gmt_create,gmt_modified)" + " VALUE(?,?,?,?,?)"; + public String getCreateTableSql() { + return "CREATE TABLE IF NOT EXISTS #tableName(" + "id bigint(20) NOT NULL AUTO_INCREMENT," + + "value bigint(20) NOT NULL," + "name varchar(32) NOT NULL," + "gmt_create DATETIME NOT NULL," + + "gmt_modified DATETIME NOT NULL," + "PRIMARY KEY (`id`),UNIQUE uk_name (`name`)" + ")"; } @Override diff --git a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/OracleSqlProvider.java b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/OracleSqlProvider.java index 1b3e5af61..def633309 100644 --- a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/OracleSqlProvider.java +++ b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/OracleSqlProvider.java @@ -12,9 +12,40 @@ import org.springframework.stereotype.Component; @Component public class OracleSqlProvider implements SqlProvider { + /** + * 获取表是否存在 + * @return + */ @Override - public String getInsertRangeSql() { - return "INSERT INTO #tableName(id,name,value,gmt_create,gmt_modified)" + " VALUE(?,?,?,?,?)"; + public String getExistTableSql() { + return "select count(*) from user_tables where table_name =upper('#tableName')"; + } + + /** + * 获取建表语句 + * @return SQL + */ + @Override + public String getCreateTableSql() { + return "CREATE TABLE #tableName " + "(id NUMBER (20,0) VISIBLE NOT NULL,value NUMBER (20,0) VISIBLE NOT NULL" + + ",name VARCHAR2 (96 BYTE) VISIBLE,gmt_create DATE VISIBLE NOT NULL" + + ",gmt_modified DATE VISIBLE NOT NULL)"; + } + + /** + * 获取更新范围语句 + * @return + */ + public String getUpdateRangeSql() { + return "UPDATE #tableName SET value=?,gmt_modified=? WHERE name=? AND value=?"; + } + + /** + * 获取查询范围语句 + * @return + */ + public String getSelectRangeSql() { + return "SELECT value FROM #tableName WHERE name=?"; } @Override diff --git a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/PostgreSqlProvider.java b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/PostgreSqlProvider.java index ae67cbab9..12a27df31 100644 --- a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/PostgreSqlProvider.java +++ b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/PostgreSqlProvider.java @@ -12,9 +12,15 @@ import org.springframework.stereotype.Component; @Component public class PostgreSqlProvider implements SqlProvider { + /** + * 获取建表语句 + * @return + */ @Override - public String getInsertRangeSql() { - return "INSERT INTO #tableName(id,name,value,gmt_create,gmt_modified)" + " VALUE(?,?,?,?,?)"; + public String getCreateTableSql() { + return "CREATE TABLE IF NOT EXISTS #tableName (ID int8 PRIMARY KEY NOT NULL" + + ",VALUE int8 NOT NULL,NAME VARCHAR (266) NOT NULL,gmt_create TIMESTAMP (6) NOT NULL" + + ",gmt_modified TIMESTAMP (6) NOT NULL)"; } @Override diff --git a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlProvider.java b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlProvider.java index 3c28e52c6..88d899dce 100644 --- a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlProvider.java +++ b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlProvider.java @@ -10,28 +10,34 @@ import com.baomidou.mybatisplus.annotation.DbType; */ public interface SqlProvider { + /** + * 获取表是否存在 + * @return + */ + default String getExistTableSql() { + return null; + } + /** * 获取建表语句 * @return */ - default String getCreateTableSql() { - return "CREATE TABLE IF NOT EXISTS #tableName(" + "id bigint(20) NOT NULL AUTO_INCREMENT," - + "value bigint(20) NOT NULL," + "name varchar(32) NOT NULL," + "gmt_create DATETIME NOT NULL," - + "gmt_modified DATETIME NOT NULL," + "PRIMARY KEY (`id`),UNIQUE uk_name (`name`)" + ")"; - } + String getCreateTableSql(); /** * 获取插入范围语句 * @return */ - String getInsertRangeSql(); + default String getInsertRangeSql() { + return "INSERT INTO #tableName (id,name,value,gmt_create,gmt_modified)" + " VALUES(?,?,?,?,?)"; + } /** * 获取更新范围语句 * @return */ default String getUpdateRangeSql() { - return "UPDATE #tableName SET value=?,gmt_modified=? WHERE name=? AND " + "value=?"; + return "UPDATE #tableName SET value=?,gmt_modified=? WHERE name=? AND value=?"; } /** diff --git a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlProviderFactory.java b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlProviderFactory.java index 9a3541e0a..21f8e867a 100644 --- a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlProviderFactory.java +++ b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlProviderFactory.java @@ -71,4 +71,13 @@ public class SqlProviderFactory { return sqlProvider.getUpdateRangeSql(); } + /** + * 获取是否创建表语句 + * @return SQL语句 + */ + public String getExistTableSql() { + SqlProvider sqlProvider = getSqlProvider(); + return sqlProvider.getExistTableSql(); + } + } diff --git a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlServerSqlProvider.java b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlServerSqlProvider.java deleted file mode 100644 index 81d0e1f83..000000000 --- a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlServerSqlProvider.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.pig4cloud.pigx.common.sequence.range.impl.db.provider; - -import com.baomidou.mybatisplus.annotation.DbType; -import org.springframework.stereotype.Component; - -/** - * SQL server 数据源提供者 - * - * @author lengleng - * @date 2022-01-13 - */ -@Component -public class SqlServerSqlProvider implements SqlProvider { - - @Override - public String getInsertRangeSql() { - return "INSERT INTO #tableName(id,name,value,gmt_create,gmt_modified)" + " VALUE(?,?,?,?,?)"; - } - - @Override - public Boolean support(DbType dbType) { - return DbType.SQL_SERVER.equals(dbType); - } - -}