♻️ Refactoring code. 发号器 优化 适配Oracle 、PG数据库

This commit is contained in:
qraddx committed 2022-01-19 15:35:11 +08:00
1 parent b533e6441b
commit a06c9bc69a
9 files changed
+134 -51

No files matched your search

@@ -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() {
@@ -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;
}
}
@@ -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) {
@@ -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
@@ -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
@@ -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
@@ -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=?";
}
/**
@@ -71,4 +71,13 @@ public class SqlProviderFactory {
return sqlProvider.getUpdateRangeSql();
}
/**
* 获取是否创建表语句
* @return SQL语句
*/
public String getExistTableSql() {
SqlProvider sqlProvider = getSqlProvider();
return sqlProvider.getExistTableSql();
}
}
@@ -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);
}
}