1、新增MybatisPlus类型处理器

2、新增client缓存
This commit is contained in:
zhuyijun 2022-11-15 20:58:07 +08:00
parent 028152a201
commit 0cbea4c4c6
17 changed files with 654 additions and 342 deletions

57
pom.xml
View File

@ -92,8 +92,7 @@
<commons-io.version>2.11.0</commons-io.version> <commons-io.version>2.11.0</commons-io.version>
<commons-crypto.version>1.1.0</commons-crypto.version> <commons-crypto.version>1.1.0</commons-crypto.version>
<hutool.version>5.8.8</hutool.version> <hutool.version>5.8.8</hutool.version>
<qrcode-zxing.version>3.3.3</qrcode-zxing.version> <qrcode-zxing.version>3.5.0</qrcode-zxing.version>
<zxing.version>3.5.0</zxing.version>
<okhttp3.version>4.10.0</okhttp3.version> <okhttp3.version>4.10.0</okhttp3.version>
<!-- 文档--> <!-- 文档-->
<knife4j.version>3.0.3</knife4j.version> <knife4j.version>3.0.3</knife4j.version>
@ -135,44 +134,32 @@
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>${lombok.version}</version> <version>${lombok.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId> <artifactId>jackson-core</artifactId>
<version>${jackson.version}</version> <version>${jackson.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId> <artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version> <version>${jackson.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.datatype</groupId> <groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId> <artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version> <version>${jackson.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version> <version>${jackson.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<!-- ORM --> <!-- ORM -->
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
<version>${mysql-jdbc.version}</version> <version>${mysql-jdbc.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.xiaoymin</groupId> <groupId>com.github.xiaoymin</groupId>
@ -199,29 +186,21 @@
<groupId>org.bouncycastle</groupId> <groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId> <artifactId>bcprov-jdk15on</artifactId>
<version>${bcprov-jdk15on.version}</version> <version>${bcprov-jdk15on.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.aliyun</groupId> <groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId> <artifactId>aliyun-java-sdk-core</artifactId>
<version>${aliyun.version}</version> <version>${aliyun.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.github.openfeign</groupId> <groupId>io.github.openfeign</groupId>
<artifactId>feign-hystrix</artifactId> <artifactId>feign-hystrix</artifactId>
<version>${feign-hystrix.version}</version> <version>${feign-hystrix.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId> <artifactId>nacos-client</artifactId>
<version>${nacos-client.version}</version> <version>${nacos-client.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>com.baomidou</groupId>
@ -232,8 +211,6 @@
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId> <artifactId>commons-pool2</artifactId>
<version>2.7.0</version> <version>2.7.0</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
@ -259,8 +236,6 @@
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version> <version>${commons-lang3.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
@ -271,8 +246,6 @@
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
<version>${commons-io.version}</version> <version>${commons-io.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
@ -284,15 +257,11 @@
<groupId>org.postgresql</groupId> <groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId> <artifactId>postgresql</artifactId>
<version>${postgresql.version}</version> <version>${postgresql.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
<version>${compler.maven.plugin.version}</version> <version>${compler.maven.plugin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-oauth2-authorization-server --> <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-oauth2-authorization-server -->
@ -311,21 +280,12 @@
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId> <artifactId>hutool-all</artifactId>
<version>${hutool.version}</version> <version>${hutool.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.zxing</groupId> <groupId>com.google.zxing</groupId>
<artifactId>core</artifactId> <artifactId>core</artifactId>
<version>${qrcode-zxing.version}</version> <version>${qrcode-zxing.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>${zxing.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency> <dependency>
<groupId>io.minio</groupId> <groupId>io.minio</groupId>
<artifactId>minio</artifactId> <artifactId>minio</artifactId>
@ -335,15 +295,11 @@
<groupId>com.squareup.okhttp3</groupId> <groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId> <artifactId>okhttp</artifactId>
<version>${okhttp3.version}</version> <version>${okhttp3.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.aliyun</groupId> <groupId>com.aliyun</groupId>
<artifactId>alibabacloud-dysmsapi20170525</artifactId> <artifactId>alibabacloud-dysmsapi20170525</artifactId>
<version>${alibabacloud-dysmsapi20170525.version}</version> <version>${alibabacloud-dysmsapi20170525.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.tencentcloudapi</groupId> <groupId>com.tencentcloudapi</groupId>
@ -356,38 +312,28 @@
<groupId>com.aliyun</groupId> <groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId> <artifactId>dysmsapi20170525</artifactId>
<version>${dysmsapi20170525.version}</version> <version>${dysmsapi20170525.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<!-- 打包--> <!-- 打包-->
<dependency> <dependency>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId> <artifactId>maven-assembly-plugin</artifactId>
<version>${maven-assembly-plugin.version}</version> <version>${maven-assembly-plugin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-plugin --> <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-plugin -->
<dependency> <dependency>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version> <version>${maven-jar-plugin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.sun.xml.bind</groupId> <groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId> <artifactId>jaxb-impl</artifactId>
<version>${jaxb-impl.version}</version> <version>${jaxb-impl.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.sun.xml.messaging.saaj</groupId> <groupId>com.sun.xml.messaging.saaj</groupId>
<artifactId>saaj-impl</artifactId> <artifactId>saaj-impl</artifactId>
<version>${saaj-impl.version}</version> <version>${saaj-impl.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba.fastjson2</groupId> <groupId>com.alibaba.fastjson2</groupId>
@ -400,6 +346,7 @@
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<optional>true</optional>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -148,6 +148,14 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>15</source>
<target>15</target>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@ -0,0 +1,9 @@
________ ___ ___ ___ ________ ___ ________ ________ ________
|\_____ \ |\ \ / /||\ \|\ __ \|\ \ |\ __ \|\ ____\|\ ____\
\|___/ /| \ \ \/ / /\ \ \ \ \|\ /\ \ \ \ \ \|\ \ \ \___|\ \ \___|_
/ / / \ \ / /_ \ \ \ \ __ \ \ \ \ \ \\\ \ \ \ __\ \_____ \
/ /_/__ \/ / /\ \\_\ \ \ \|\ \ \ \____\ \ \\\ \ \ \|\ \|____|\ \
|\________\__/ / / \ \________\ \_______\ \_______\ \_______\ \_______\____\_\ \
\|_______|\___/ / \|________|\|_______|\|_______|\|_______|\|_______|\_________\
\|___|/ \|_________|
Release 1.0.0. Powered by zyjblogs-framework 1.0.0.

View File

@ -11,15 +11,18 @@
<!-- 彩色日志 --> <!-- 彩色日志 -->
<!-- 彩色日志依赖的渲染类 --> <!-- 彩色日志依赖的渲染类 -->
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/> <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" /> <conversionRule conversionWord="wex"
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" /> converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
<conversionRule conversionWord="wEx"
converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
<!-- 彩色日志格式 --> <!-- 彩色日志格式 -->
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr([%X{traceId}]) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> <property name="CONSOLE_LOG_PATTERN"
value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr([%X{traceId}]) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<!-- 输出到控制台 --> <!-- 输出到控制台 -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>info</level> <level>INFO</level>
</filter> </filter>
<encoder> <encoder>
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern> <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>

View File

@ -22,7 +22,6 @@ import org.springframework.security.oauth2.provider.CompositeTokenGranter;
import org.springframework.security.oauth2.provider.TokenGranter; import org.springframework.security.oauth2.provider.TokenGranter;
import org.springframework.security.oauth2.provider.TokenRequest; import org.springframework.security.oauth2.provider.TokenRequest;
import org.springframework.security.oauth2.provider.client.ClientCredentialsTokenGranter; import org.springframework.security.oauth2.provider.client.ClientCredentialsTokenGranter;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices; import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeTokenGranter; import org.springframework.security.oauth2.provider.code.AuthorizationCodeTokenGranter;
import org.springframework.security.oauth2.provider.implicit.ImplicitTokenGranter; import org.springframework.security.oauth2.provider.implicit.ImplicitTokenGranter;
@ -57,6 +56,7 @@ public class AuthorizationServerConfiguration extends AuthorizationServerConfigu
private final RedisTemplateHandler redisTemplateHandler; private final RedisTemplateHandler redisTemplateHandler;
private final UserDetailsService userDetailsService; private final UserDetailsService userDetailsService;
private final ClientDetailsService oauthClientDetailsService;
/** /**
* 令牌端点的安全约束 * 令牌端点的安全约束
@ -174,8 +174,8 @@ public class AuthorizationServerConfiguration extends AuthorizationServerConfigu
@Bean @Bean
public ClientDetailsService clientDetails(DataSource dataSource) { public ClientDetailsService clientDetails(DataSource dataSource) {
JdbcClientDetailsService jdbcClientDetailsService = new JdbcClientDetailsService(dataSource); // JdbcClientDetailsService jdbcClientDetailsService = new JdbcClientDetailsService(dataSource);
return jdbcClientDetailsService; return oauthClientDetailsService;
} }
@Bean @Bean

View File

@ -1,258 +1,177 @@
package cn.zyjblogs.server.client.po; package cn.zyjblogs.server.client.po;
import cn.zyjblogs.starter.common.utils.bean.BeanUtils; import cn.zyjblogs.starter.mybatisplus.config.ListToStringHandler;
import com.alibaba.fastjson2.JSON; import cn.zyjblogs.starter.mybatisplus.config.MapToStringHandler;
import com.alibaba.fastjson2.annotation.JSONField; import cn.zyjblogs.starter.mybatisplus.config.SetToStringHandler;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat; import org.apache.ibatis.type.JdbcType;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.ClientDetails; import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.security.oauth2.provider.client.Jackson2ArrayOrStringDeserializer;
import org.springframework.security.oauth2.provider.client.JacksonArrayOrStringDeserializer;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.io.Serializable; import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.*; import java.util.*;
/** /**
* @author zhuyijun * @author zhuyijun
*/ */
@org.codehaus.jackson.map.annotate.JsonSerialize( @SuppressWarnings("serial")
include = org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.NON_DEFAULT @org.codehaus.jackson.map.annotate.JsonSerialize(include = org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.NON_DEFAULT)
) //@org.codehaus.jackson.annotate.JsonIgnoreProperties(ignoreUnknown = true)
@JsonIgnoreProperties( @com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_DEFAULT)
ignoreUnknown = true //@com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown = true)
) @TableName(value = "oauth_client_details", autoResultMap = true)
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
@com.fasterxml.jackson.annotation.JsonIgnoreProperties(
ignoreUnknown = true
)
@TableName("oauth_client_details")
public class OauthClientDetail implements ClientDetails, Serializable { public class OauthClientDetail implements ClientDetails, Serializable {
@org.codehaus.jackson.annotate.JsonProperty("clientId")
@com.fasterxml.jackson.annotation.JsonProperty("clientId")
@TableId("client_id") @TableId("client_id")
private String clientId; private String clientId;
@org.codehaus.jackson.annotate.JsonProperty("clientSecret")
@com.fasterxml.jackson.annotation.JsonProperty("clientSecret")
@TableField("client_secret") @TableField("client_secret")
private String clientSecret; private String clientSecret;
@TableField("resource_ids") @com.fasterxml.jackson.annotation.JsonProperty("scope")
@JsonIgnore // @org.codehaus.jackson.map.annotate.JsonDeserialize(using = JacksonArrayOrStringDeserializer.class)
@com.fasterxml.jackson.annotation.JsonIgnore @org.codehaus.jackson.annotate.JsonProperty("scope")
private String resourceIds; // @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = Jackson2ArrayOrStringDeserializer.class)
@TableField(value = "scope", jdbcType = JdbcType.VARCHAR, typeHandler = SetToStringHandler.class)
private Set<String> scope = Collections.emptySet();
@TableField("scope") @org.codehaus.jackson.annotate.JsonProperty("resourceIds")
@JsonIgnore // @org.codehaus.jackson.map.annotate.JsonDeserialize(using = JacksonArrayOrStringDeserializer.class)
@com.fasterxml.jackson.annotation.JsonIgnore @com.fasterxml.jackson.annotation.JsonProperty("resourceIds")
private String scope; // @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = Jackson2ArrayOrStringDeserializer.class)
@TableField(value = "resource_ids", jdbcType = JdbcType.VARCHAR, typeHandler = SetToStringHandler.class)
private Set<String> resourceIds = Collections.emptySet();
@TableField("authorized_grant_types") @org.codehaus.jackson.annotate.JsonProperty("authorizedGrantTypes")
@JsonIgnore // @org.codehaus.jackson.map.annotate.JsonDeserialize(using = JacksonArrayOrStringDeserializer.class)
@com.fasterxml.jackson.annotation.JsonIgnore @com.fasterxml.jackson.annotation.JsonProperty("authorizedGrantTypes")
private String authorizedGrantTypes; // @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = Jackson2ArrayOrStringDeserializer.class)
@TableField(value = "authorized_grant_types", jdbcType = JdbcType.VARCHAR, typeHandler = SetToStringHandler.class)
private Set<String> authorizedGrantTypes = Collections.emptySet();
@TableField("web_server_redirect_uri") @org.codehaus.jackson.annotate.JsonProperty("registeredRedirectUris")
@JsonIgnore // @org.codehaus.jackson.map.annotate.JsonDeserialize(using = JacksonArrayOrStringDeserializer.class)
@com.fasterxml.jackson.annotation.JsonIgnore @com.fasterxml.jackson.annotation.JsonProperty("registeredRedirectUris")
private String webServerRedirectUri; // @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = Jackson2ArrayOrStringDeserializer.class)
@TableField(value = "web_server_redirect_uri", jdbcType = JdbcType.VARCHAR, typeHandler = SetToStringHandler.class)
private Set<String> registeredRedirectUris;
@TableField("authorities") @org.codehaus.jackson.annotate.JsonProperty("autoApproveScopes")
@JsonIgnore // @org.codehaus.jackson.map.annotate.JsonDeserialize(using = JacksonArrayOrStringDeserializer.class)
@com.fasterxml.jackson.annotation.JsonIgnore @com.fasterxml.jackson.annotation.JsonProperty("autoApproveScopes")
private String authorities; // @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = Jackson2ArrayOrStringDeserializer.class)
@TableField(value = "autoapprove", jdbcType = JdbcType.VARCHAR, typeHandler = SetToStringHandler.class)
private Set<String> autoApproveScopes;
@TableField(value = "authorities", jdbcType = JdbcType.VARCHAR, typeHandler = ListToStringHandler.class)
private List<GrantedAuthority> authorities = Collections.emptyList();
@org.codehaus.jackson.annotate.JsonProperty("accessTokenValiditySeconds")
@com.fasterxml.jackson.annotation.JsonProperty("accessTokenValiditySeconds")
@TableField("access_token_validity") @TableField("access_token_validity")
private String accessTokenValidity;
@TableField("refresh_token_validity")
private String refreshTokenValidity;
@TableField("additional_information")
@JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
private String additionalInformation;
@TableField("create_time")
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
@TableField("archived")
@JsonProperty("archived")
@com.fasterxml.jackson.annotation.JsonProperty("archived")
private Integer archived;
@TableField("trusted")
@JsonProperty("trusted")
@com.fasterxml.jackson.annotation.JsonProperty("trusted")
private Integer trusted;
@TableField("autoapprove")
private String autoapprove;
@TableField(exist = false)
@JsonProperty("access_token_validity")
@com.fasterxml.jackson.annotation.JsonProperty("access_token_validity")
private Integer accessTokenValiditySeconds; private Integer accessTokenValiditySeconds;
@TableField(exist = false)
@JsonProperty("access_token_validity") @org.codehaus.jackson.annotate.JsonProperty("refreshTokenValiditySeconds")
@com.fasterxml.jackson.annotation.JsonProperty("refresh_token_validity") @com.fasterxml.jackson.annotation.JsonProperty("refreshTokenValiditySeconds")
@TableField("refresh_token_validity")
private Integer refreshTokenValiditySeconds; private Integer refreshTokenValiditySeconds;
@org.codehaus.jackson.annotate.JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
@TableField(value = "additional_information", jdbcType = JdbcType.VARCHAR, typeHandler = MapToStringHandler.class)
private Map<String, Object> additionalInformation = new LinkedHashMap<String, Object>();
public OauthClientDetail() { public OauthClientDetail() {
} }
public OauthClientDetail(ClientDetails prototype) { public OauthClientDetail(ClientDetails prototype) {
this(); this();
this.setAccessTokenValiditySeconds(prototype.getAccessTokenValiditySeconds()); setAccessTokenValiditySeconds(prototype.getAccessTokenValiditySeconds());
this.setRefreshTokenValiditySeconds(prototype.getRefreshTokenValiditySeconds()); setRefreshTokenValiditySeconds(prototype
Collection<GrantedAuthority> authorities = prototype.getAuthorities(); .getRefreshTokenValiditySeconds());
this.setAuthorities(JSON.toJSONString(authorities)); setAuthorities(prototype.getAuthorities());
this.setAuthorizedGrantTypes(String.join(",", prototype.getAuthorizedGrantTypes())); setAuthorizedGrantTypes(prototype.getAuthorizedGrantTypes());
this.setClientId(prototype.getClientId()); setClientId(prototype.getClientId());
this.setClientSecret(prototype.getClientSecret()); setClientSecret(prototype.getClientSecret());
this.setWebServerRedirectUri(String.join(",", prototype.getRegisteredRedirectUri())); setRegisteredRedirectUri(prototype.getRegisteredRedirectUri());
this.setScope(String.join(",", prototype.getScope())); setScope(prototype.getScope());
this.setResourceIds(String.join(",", prototype.getResourceIds())); setResourceIds(prototype.getResourceIds());
} }
@Override public OauthClientDetail(String clientId, String resourceIds,
String scopes, String grantTypes, String authorities) {
this(clientId, resourceIds, scopes, grantTypes, authorities, null);
}
public OauthClientDetail(String clientId, String resourceIds,
String scopes, String grantTypes, String authorities,
String redirectUris) {
this.clientId = clientId;
if (StringUtils.hasText(resourceIds)) {
Set<String> resources = StringUtils
.commaDelimitedListToSet(resourceIds);
if (!resources.isEmpty()) {
this.resourceIds = resources;
}
}
if (StringUtils.hasText(scopes)) {
Set<String> scopeList = StringUtils.commaDelimitedListToSet(scopes);
if (!scopeList.isEmpty()) {
this.scope = scopeList;
}
}
if (StringUtils.hasText(grantTypes)) {
this.authorizedGrantTypes = StringUtils
.commaDelimitedListToSet(grantTypes);
} else {
this.authorizedGrantTypes = new HashSet<String>(Arrays.asList(
"authorization_code", "refresh_token"));
}
if (StringUtils.hasText(authorities)) {
this.authorities = AuthorityUtils
.commaSeparatedStringToAuthorityList(authorities);
}
if (StringUtils.hasText(redirectUris)) {
this.registeredRedirectUris = StringUtils
.commaDelimitedListToSet(redirectUris);
}
}
@org.codehaus.jackson.annotate.JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public String getClientId() { public String getClientId() {
return clientId; return clientId;
} }
@Override
@JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public Set<String> getResourceIds() {
return Set.of(resourceIds.split(","));
}
public void setClientId(String clientId) { public void setClientId(String clientId) {
this.clientId = clientId; this.clientId = clientId;
} }
public void setClientSecret(String clientSecret) { public void setAutoApproveScopes(Collection<String> autoApproveScopes) {
this.clientSecret = clientSecret; this.autoApproveScopes = new HashSet<String>(autoApproveScopes);
}
public void setResourceIds(String resourceIds) {
this.resourceIds = resourceIds;
}
public void setScope(String scope) {
this.scope = scope;
}
public void setAuthorizedGrantTypes(String authorizedGrantTypes) {
this.authorizedGrantTypes = authorizedGrantTypes;
}
public void setWebServerRedirectUri(String webServerRedirectUri) {
this.webServerRedirectUri = webServerRedirectUri;
}
public void setAuthorities(String authorities) {
this.authorities = authorities;
}
public void setAccessTokenValiditySeconds(Integer accessTokenValiditySeconds) {
this.accessTokenValiditySeconds = accessTokenValiditySeconds;
}
public void setRefreshTokenValiditySeconds(Integer refreshTokenValiditySeconds) {
this.refreshTokenValiditySeconds = refreshTokenValiditySeconds;
} }
@Override @Override
@JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public boolean isSecretRequired() {
return this.clientSecret != null;
}
@Override
@JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public String getClientSecret() {
return clientSecret;
}
@Override
@JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public boolean isScoped() {
return this.scope != null && !this.scope.isEmpty();
}
@Override
@JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public Set<String> getScope() {
return Set.of(scope.split(","));
}
@Override
@JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public Set<String> getAuthorizedGrantTypes() {
if (authorizedGrantTypes == null) {
return new HashSet<>(0);
}
return Set.of(authorizedGrantTypes.split(","));
}
@Override
@JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public Set<String> getRegisteredRedirectUri() {
if (webServerRedirectUri == null) {
return new HashSet<>(0);
}
return Set.of(webServerRedirectUri.split(","));
}
@Override
@JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public Collection<GrantedAuthority> getAuthorities() {
if (StringUtils.isEmpty(authorities)) {
return Collections.emptyList();
}
return JSON.parseArray(authorities, GrantedAuthority.class);
}
@Override
@JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public Integer getAccessTokenValiditySeconds() {
return accessTokenValiditySeconds;
}
@Override
@JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public Integer getRefreshTokenValiditySeconds() {
return refreshTokenValiditySeconds;
}
@Override
@JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public boolean isAutoApprove(String scope) { public boolean isAutoApprove(String scope) {
if (scope == null) { if (autoApproveScopes == null) {
return false; return false;
} }
for (String auto : getScope()) { for (String auto : autoApproveScopes) {
if (auto.equals("true") || scope.matches(auto)) { if (auto.equals("true") || scope.matches(auto)) {
return true; return true;
} }
@ -260,13 +179,249 @@ public class OauthClientDetail implements ClientDetails, Serializable {
return false; return false;
} }
@Override @org.codehaus.jackson.annotate.JsonIgnore
@JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore @com.fasterxml.jackson.annotation.JsonIgnore
public Set<String> getAutoApproveScopes() {
return autoApproveScopes;
}
@org.codehaus.jackson.annotate.JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public boolean isSecretRequired() {
return this.clientSecret != null;
}
@org.codehaus.jackson.annotate.JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public String getClientSecret() {
return clientSecret;
}
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}
@org.codehaus.jackson.annotate.JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public boolean isScoped() {
return this.scope != null && !this.scope.isEmpty();
}
public Set<String> getScope() {
return scope;
}
public void setScope(Collection<String> scope) {
this.scope = scope == null ? Collections.<String>emptySet()
: new LinkedHashSet<String>(scope);
}
@org.codehaus.jackson.annotate.JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public Set<String> getResourceIds() {
return resourceIds;
}
public void setResourceIds(Collection<String> resourceIds) {
this.resourceIds = resourceIds == null ? Collections
.<String>emptySet() : new LinkedHashSet<String>(resourceIds);
}
@org.codehaus.jackson.annotate.JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public Set<String> getAuthorizedGrantTypes() {
return authorizedGrantTypes;
}
public void setAuthorizedGrantTypes(Collection<String> authorizedGrantTypes) {
this.authorizedGrantTypes = new LinkedHashSet<String>(
authorizedGrantTypes);
}
@org.codehaus.jackson.annotate.JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public Set<String> getRegisteredRedirectUri() {
return registeredRedirectUris;
}
public void setRegisteredRedirectUri(Set<String> registeredRedirectUris) {
this.registeredRedirectUris = registeredRedirectUris == null ? null
: new LinkedHashSet<String>(registeredRedirectUris);
}
@org.codehaus.jackson.annotate.JsonProperty("authorities")
@com.fasterxml.jackson.annotation.JsonProperty("authorities")
private List<String> getAuthoritiesAsStrings() {
return new ArrayList<String>(
AuthorityUtils.authorityListToSet(authorities));
}
@org.codehaus.jackson.annotate.JsonProperty("authorities")
@org.codehaus.jackson.map.annotate.JsonDeserialize(using = JacksonArrayOrStringDeserializer.class)
@com.fasterxml.jackson.annotation.JsonProperty("authorities")
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = Jackson2ArrayOrStringDeserializer.class)
private void setAuthoritiesAsStrings(Set<String> values) {
setAuthorities(AuthorityUtils.createAuthorityList(values
.toArray(new String[values.size()])));
}
@org.codehaus.jackson.annotate.JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public Collection<GrantedAuthority> getAuthorities() {
return authorities;
}
@org.codehaus.jackson.annotate.JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public void setAuthorities(
Collection<? extends GrantedAuthority> authorities) {
this.authorities = new ArrayList<GrantedAuthority>(authorities);
}
@org.codehaus.jackson.annotate.JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public Integer getAccessTokenValiditySeconds() {
return accessTokenValiditySeconds;
}
public void setAccessTokenValiditySeconds(Integer accessTokenValiditySeconds) {
this.accessTokenValiditySeconds = accessTokenValiditySeconds;
}
@org.codehaus.jackson.annotate.JsonIgnore
@com.fasterxml.jackson.annotation.JsonIgnore
public Integer getRefreshTokenValiditySeconds() {
return refreshTokenValiditySeconds;
}
public void setRefreshTokenValiditySeconds(
Integer refreshTokenValiditySeconds) {
this.refreshTokenValiditySeconds = refreshTokenValiditySeconds;
}
public void setAdditionalInformation(Map<String, ?> additionalInformation) {
this.additionalInformation = new LinkedHashMap<String, Object>(
additionalInformation);
}
@org.codehaus.jackson.annotate.JsonAnyGetter
@com.fasterxml.jackson.annotation.JsonAnyGetter
public Map<String, Object> getAdditionalInformation() { public Map<String, Object> getAdditionalInformation() {
return Collections.unmodifiableMap(this.additionalInformation);
}
@org.codehaus.jackson.annotate.JsonAnySetter
@com.fasterxml.jackson.annotation.JsonAnySetter
public void addAdditionalInformation(String key, Object value) {
this.additionalInformation.put(key, value);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime
* result
+ ((accessTokenValiditySeconds == null) ? 0
: accessTokenValiditySeconds);
result = prime
* result
+ ((refreshTokenValiditySeconds == null) ? 0
: refreshTokenValiditySeconds);
result = prime * result
+ ((authorities == null) ? 0 : authorities.hashCode());
result = prime
* result
+ ((authorizedGrantTypes == null) ? 0 : authorizedGrantTypes
.hashCode());
result = prime * result
+ ((clientId == null) ? 0 : clientId.hashCode());
result = prime * result
+ ((clientSecret == null) ? 0 : clientSecret.hashCode());
result = prime
* result
+ ((registeredRedirectUris == null) ? 0
: registeredRedirectUris.hashCode());
result = prime * result
+ ((resourceIds == null) ? 0 : resourceIds.hashCode());
result = prime * result + ((scope == null) ? 0 : scope.hashCode());
result = prime * result + ((additionalInformation == null) ? 0 : additionalInformation.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BaseClientDetails other = (BaseClientDetails) obj;
if (accessTokenValiditySeconds == null) {
if (other.getAccessTokenValiditySeconds() != null)
return false;
} else if (!accessTokenValiditySeconds.equals(other.getAccessTokenValiditySeconds()))
return false;
if (refreshTokenValiditySeconds == null) {
if (other.getRefreshTokenValiditySeconds() != null)
return false;
} else if (!refreshTokenValiditySeconds.equals(other.getRefreshTokenValiditySeconds()))
return false;
if (authorities == null) {
if (other.getAuthorities() != null)
return false;
} else if (!authorities.equals(other.getAuthorities()))
return false;
if (authorizedGrantTypes == null) {
if (other.getAuthorizedGrantTypes() != null)
return false;
} else if (!authorizedGrantTypes.equals(other.getAuthorizedGrantTypes()))
return false;
if (clientId == null) {
if (other.getClientId() != null)
return false;
} else if (!clientId.equals(other.getClientId()))
return false;
if (clientSecret == null) {
if (other.getClientSecret() != null)
return false;
} else if (!clientSecret.equals(other.getClientSecret()))
return false;
if (registeredRedirectUris == null) {
if (other.getRegisteredRedirectUri() != null)
return false;
} else if (!registeredRedirectUris.equals(other.getRegisteredRedirectUri()))
return false;
if (resourceIds == null) {
if (other.getResourceIds() != null)
return false;
} else if (!resourceIds.equals(other.getResourceIds()))
return false;
if (scope == null) {
if (other.getScope() != null)
return false;
} else if (!scope.equals(other.getScope()))
return false;
if (additionalInformation == null) { if (additionalInformation == null) {
return new HashMap<>(0); if (other.getAdditionalInformation() != null)
return false;
} else if (!additionalInformation.equals(other.getAdditionalInformation()))
return false;
return true;
} }
return BeanUtils.beanToMap(additionalInformation);
@Override
public String toString() {
return "BaseClientDetails [clientId=" + clientId + ", clientSecret="
+ clientSecret + ", scope=" + scope + ", resourceIds="
+ resourceIds + ", authorizedGrantTypes="
+ authorizedGrantTypes + ", registeredRedirectUris="
+ registeredRedirectUris + ", authorities=" + authorities
+ ", accessTokenValiditySeconds=" + accessTokenValiditySeconds
+ ", refreshTokenValiditySeconds="
+ refreshTokenValiditySeconds + ", additionalInformation="
+ additionalInformation + "]";
} }
} }

View File

@ -3,12 +3,14 @@ package cn.zyjblogs.server.client.service.impl;
import cn.zyjblogs.server.client.mapper.OauthClientDetailsMapper; import cn.zyjblogs.server.client.mapper.OauthClientDetailsMapper;
import cn.zyjblogs.server.client.po.OauthClientDetail; import cn.zyjblogs.server.client.po.OauthClientDetail;
import cn.zyjblogs.starter.common.utils.bean.BeanUtils; import cn.zyjblogs.starter.common.utils.bean.BeanUtils;
import cn.zyjblogs.starter.redis.utils.RedisTemplateHandler;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.*; import org.springframework.security.oauth2.provider.*;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -23,12 +25,22 @@ import java.util.List;
public class OauthClientDetailsServiceImpl implements ClientDetailsService, ClientRegistrationService { public class OauthClientDetailsServiceImpl implements ClientDetailsService, ClientRegistrationService {
private final OauthClientDetailsMapper oauthClientDetailsMapper; private final OauthClientDetailsMapper oauthClientDetailsMapper;
private final PasswordEncoder passwordEncoder; private final PasswordEncoder passwordEncoder;
private final RedisTemplateHandler<String, BaseClientDetails> redisTemplateHandler;
private static final String CLIENT = "OAUTH:CLIENT";
@Override @Override
public OauthClientDetail loadClientByClientId(String clientId) throws ClientRegistrationException { public OauthClientDetail loadClientByClientId(String clientId) throws ClientRegistrationException {
OauthClientDetail baseClientDetails1 = redisTemplateHandler.<String, OauthClientDetail>hGet(CLIENT, clientId);
if (baseClientDetails1 != null) {
return baseClientDetails1;
}
LambdaQueryWrapper<OauthClientDetail> wrapper = Wrappers.lambdaQuery(); LambdaQueryWrapper<OauthClientDetail> wrapper = Wrappers.lambdaQuery();
wrapper.eq(OauthClientDetail::getClientId, clientId); wrapper.eq(OauthClientDetail::getClientId, clientId);
return oauthClientDetailsMapper.selectOne(wrapper); OauthClientDetail baseClientDetails = oauthClientDetailsMapper.selectOne(wrapper);
if (baseClientDetails != null) {
redisTemplateHandler.hPut(CLIENT, clientId, baseClientDetails);
}
return baseClientDetails;
} }
@Override @Override
@ -37,7 +49,10 @@ public class OauthClientDetailsServiceImpl implements ClientDetailsService, Clie
if (oauthClientDetail.getClientSecret() != null) { if (oauthClientDetail.getClientSecret() != null) {
oauthClientDetail.setClientSecret(passwordEncoder.encode(oauthClientDetail.getClientSecret())); oauthClientDetail.setClientSecret(passwordEncoder.encode(oauthClientDetail.getClientSecret()));
} }
oauthClientDetailsMapper.insert(oauthClientDetail); int insert = oauthClientDetailsMapper.insert(oauthClientDetail);
if (insert > 0) {
redisTemplateHandler.hPut(CLIENT, clientDetails.getClientId(), clientDetails);
}
} }
@Override @Override
@ -46,6 +61,7 @@ public class OauthClientDetailsServiceImpl implements ClientDetailsService, Clie
updateWrapper.eq(OauthClientDetail::getClientId, clientDetails.getClientId()); updateWrapper.eq(OauthClientDetail::getClientId, clientDetails.getClientId());
OauthClientDetail oauthClientDetail = new OauthClientDetail(clientDetails); OauthClientDetail oauthClientDetail = new OauthClientDetail(clientDetails);
oauthClientDetailsMapper.update(oauthClientDetail, updateWrapper); oauthClientDetailsMapper.update(oauthClientDetail, updateWrapper);
redisTemplateHandler.hDelete(CLIENT, clientDetails.getClientId());
} }
@Override @Override
@ -53,6 +69,7 @@ public class OauthClientDetailsServiceImpl implements ClientDetailsService, Clie
LambdaUpdateWrapper<OauthClientDetail> updateWrapper = Wrappers.lambdaUpdate(); LambdaUpdateWrapper<OauthClientDetail> updateWrapper = Wrappers.lambdaUpdate();
updateWrapper.eq(OauthClientDetail::getClientId, clientId).set(OauthClientDetail::getClientSecret, passwordEncoder.encode(clientSecret)); updateWrapper.eq(OauthClientDetail::getClientId, clientId).set(OauthClientDetail::getClientSecret, passwordEncoder.encode(clientSecret));
oauthClientDetailsMapper.update(null, updateWrapper); oauthClientDetailsMapper.update(null, updateWrapper);
redisTemplateHandler.hDelete(CLIENT, clientId);
} }
@Override @Override
@ -60,6 +77,7 @@ public class OauthClientDetailsServiceImpl implements ClientDetailsService, Clie
LambdaUpdateWrapper<OauthClientDetail> updateWrapper = Wrappers.lambdaUpdate(); LambdaUpdateWrapper<OauthClientDetail> updateWrapper = Wrappers.lambdaUpdate();
updateWrapper.eq(OauthClientDetail::getClientId, clientId); updateWrapper.eq(OauthClientDetail::getClientId, clientId);
oauthClientDetailsMapper.delete(updateWrapper); oauthClientDetailsMapper.delete(updateWrapper);
redisTemplateHandler.hDelete(CLIENT, clientId);
} }
@Override @Override

View File

@ -1,15 +1,9 @@
________ ___ ___ ___ ________ ___ ________ ________ ________
// |\_____ \ |\ \ / /||\ \|\ __ \|\ \ |\ __ \|\ ____\|\ ____\
\\ // \|___/ /| \ \ \/ / /\ \ \ \ \|\ /\ \ \ \ \ \|\ \ \ \___|\ \ \___|_
\\ // / / / \ \ / /_ \ \ \ \ __ \ \ \ \ \ \\\ \ \ \ __\ \_____ \
##DDDDDDDDDDDDDDDDDDDDDD## / /_/__ \/ / /\ \\_\ \ \ \|\ \ \ \____\ \ \\\ \ \ \|\ \|____|\ \
## DDDDDDDDDDDDDDDDDDDD ## ________ ___ ___ ___ ________ ___ ___ ___ |\________\__/ / / \ \________\ \_______\ \_______\ \_______\ \_______\____\_\ \
## hh hh ## |\ __ \ |\ \ |\ \ |\ \ |\ __ \ |\ \ |\ \ |\ \ \|_______|\___/ / \|________|\|_______|\|_______|\|_______|\|_______|\_________\
## hh // \\ hh ## \ \ \|\ /_\ \ \\ \ \ \ \ \\ \ \|\ /_\ \ \\ \ \ \ \ \ \|___|/ \|_________|
## hh // \\ hh ## \ \ __ \\ \ \\ \ \ \ \ \\ \ __ \\ \ \\ \ \ \ \ \ Release 1.0.0. Powered by zyjblogs-framework 1.0.0.
## hh hh ## \ \ \|\ \\ \ \\ \ \____ \ \ \\ \ \|\ \\ \ \\ \ \____ \ \ \
## hh wwww hh ## \ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\
## hh hh ## \|_______| \|__| \|_______| \|__| \|_______| \|__| \|_______| \|__|
## MMMMMMMMMMMMMMMMMMMM ##
##MMMMMMMMMMMMMMMMMMMMMM## Release 1.6.11. Powered by jinkela-core 2.8.9.
\/ \/

View File

@ -116,3 +116,5 @@ sms:
endpoint: dysmsapi.aliyuncs.com endpoint: dysmsapi.aliyuncs.com
region: cn-hangzhou region: cn-hangzhou
max-size: 200 max-size: 200
#mybatis-plus:
# type-handlers-package: cn.zyjblogs.starter.mybatisplus.config

View File

@ -0,0 +1,9 @@
________ ___ ___ ___ ________ ___ ________ ________ ________
|\_____ \ |\ \ / /||\ \|\ __ \|\ \ |\ __ \|\ ____\|\ ____\
\|___/ /| \ \ \/ / /\ \ \ \ \|\ /\ \ \ \ \ \|\ \ \ \___|\ \ \___|_
/ / / \ \ / /_ \ \ \ \ __ \ \ \ \ \ \\\ \ \ \ __\ \_____ \
/ /_/__ \/ / /\ \\_\ \ \ \|\ \ \ \____\ \ \\\ \ \ \|\ \|____|\ \
|\________\__/ / / \ \________\ \_______\ \_______\ \_______\ \_______\____\_\ \
\|_______|\___/ / \|________|\|_______|\|_______|\|_______|\|_______|\_________\
\|___|/ \|_________|
Release 1.0.0. Powered by zyjblogs-framework 1.0.0.

View File

@ -4,30 +4,28 @@ import cn.zyjblogs.starter.common.utils.jackson.JacksonHolder;
import cn.zyjblogs.starter.common.utils.jackson.exception.JsonConvertException; import cn.zyjblogs.starter.common.utils.jackson.exception.JsonConvertException;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import ma.glasnost.orika.DefaultFieldMapper; import ma.glasnost.orika.DefaultFieldMapper;
import ma.glasnost.orika.MapperFacade; import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory; import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory; import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.metadata.ClassMapBuilder; import ma.glasnost.orika.metadata.ClassMapBuilder;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;
public class BeanUtils { public class BeanUtils {
private static final Logger log = LoggerFactory.getLogger(BeanUtils.class);
private static final MapperFacade DEFAULT_MAPPER_FACADE = (new DefaultMapperFactory.Builder()).build().getMapperFacade(); private static final MapperFacade DEFAULT_MAPPER_FACADE = (new DefaultMapperFactory.Builder()).build().getMapperFacade();
private static final Map<String, String> MYSQL_ESCAPE_MAP = new LinkedHashMap<>(); private static final Map<String, String> MYSQL_ESCAPE_MAP = new LinkedHashMap<>() {{
put("\\", "\\\\");
put("%", "\\%");
put("'", "\\'");
put("_", "\\_");
}};
private BeanUtils() { private BeanUtils() {
} }
@ -68,7 +66,6 @@ public class BeanUtils {
T result = JacksonHolder.MAPPER.readValue(JacksonHolder.MAPPER.writeValueAsString(content), valueTypeRef); T result = JacksonHolder.MAPPER.readValue(JacksonHolder.MAPPER.writeValueAsString(content), valueTypeRef);
return result; return result;
} catch (JsonProcessingException var4) { } catch (JsonProcessingException var4) {
log.error("json转换异常", var4);
throw new JsonConvertException("json转换异常", var4); throw new JsonConvertException("json转换异常", var4);
} }
} else { } else {
@ -161,10 +158,5 @@ public class BeanUtils {
return mapperFactory.getMapperFacade(); return mapperFactory.getMapperFacade();
} }
static {
MYSQL_ESCAPE_MAP.put("\\", "\\\\");
MYSQL_ESCAPE_MAP.put("%", "\\%");
MYSQL_ESCAPE_MAP.put("'", "\\'");
MYSQL_ESCAPE_MAP.put("_", "\\_");
}
} }

View File

@ -44,6 +44,10 @@
<groupId>com.fasterxml.jackson.datatype</groupId> <groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId> <artifactId>jackson-datatype-jsr310</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.postgresql</groupId> <groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId> <artifactId>postgresql</artifactId>

View File

@ -1,5 +1,7 @@
package cn.zyjblogs.starter.mybatisplus.autoconfigure; package cn.zyjblogs.starter.mybatisplus.autoconfigure;
import cn.zyjblogs.starter.mybatisplus.generator.GlobalIdGenerator;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
@ -7,6 +9,7 @@ import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider; import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import java.util.Properties; import java.util.Properties;
@ -29,6 +32,16 @@ public class MyBatisPlusConfig {
return interceptor; return interceptor;
} }
@Bean
public MybatisPlusPropertiesCustomizer plusPropertiesCustomizer() {
return plusProperties -> {
if (StringUtils.isEmpty(plusProperties.getTypeHandlersPackage())) {
plusProperties.setTypeHandlersPackage("cn.zyjblogs.starter.mybatisplus.config");
}
plusProperties.getGlobalConfig().setIdentifierGenerator(new GlobalIdGenerator());
};
}
/** /**
* 数据库兼容,在mybatis里设置相应的databaseId即可 * 数据库兼容,在mybatis里设置相应的databaseId即可
*/ */

View File

@ -0,0 +1,49 @@
package cn.zyjblogs.starter.mybatisplus.config;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* List<Integer> ==> string
*
* @Date 2022-10-08
*/
public class ListToStringHandler extends BaseTypeHandler<List> {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, List list, JdbcType jdbcType) throws SQLException {
if (!CollectionUtils.isEmpty(list)) {
preparedStatement.setString(i, String.join(",", list));
} else {
preparedStatement.setString(i, null);
}
}
@Override
public List getNullableResult(ResultSet resultSet, String s) throws SQLException {
String result = resultSet.getString(s);
return result == null ? null : List.of(StringUtils.commaDelimitedListToStringArray(result));
}
@Override
public List getNullableResult(ResultSet resultSet, int i) throws SQLException {
String result = resultSet.getString(i);
return result == null ? null : List.of(StringUtils.commaDelimitedListToStringArray(result));
}
@Override
public List getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
String result = callableStatement.getString(i);
return result == null ? null : List.of(StringUtils.commaDelimitedListToStringArray(result));
}
}

View File

@ -0,0 +1,43 @@
package cn.zyjblogs.starter.mybatisplus.config;
import com.alibaba.fastjson2.JSON;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.springframework.util.CollectionUtils;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
public class MapToStringHandler<K, V> extends BaseTypeHandler<Map<K, V>> {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, Map map, JdbcType jdbcType) throws SQLException {
if (!CollectionUtils.isEmpty(map)) {
preparedStatement.setString(i, JSON.toJSONString(map));
} else {
preparedStatement.setString(i, null);
}
}
@Override
public Map getNullableResult(ResultSet resultSet, String s) throws SQLException {
String result = resultSet.getString(s);
return result == null ? null : JSON.parseObject(result, Map.class);
}
@Override
public Map getNullableResult(ResultSet resultSet, int i) throws SQLException {
String result = resultSet.getString(i);
return result == null ? null : JSON.parseObject(result, Map.class);
}
@Override
public Map getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
String result = callableStatement.getString(i);
return result == null ? null : JSON.parseObject(result, Map.class);
}
}

View File

@ -0,0 +1,49 @@
package cn.zyjblogs.starter.mybatisplus.config;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Set;
/**
* Set<Integer> ==> string
*
* @Date 2022-10-08
*/
public class SetToStringHandler extends BaseTypeHandler<Set> {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, Set list, JdbcType jdbcType) throws SQLException {
if (!CollectionUtils.isEmpty(list)) {
preparedStatement.setString(i, String.join(",", list));
} else {
preparedStatement.setString(i, null);
}
}
@Override
public Set getNullableResult(ResultSet resultSet, String s) throws SQLException {
String result = resultSet.getString(s);
return result == null ? null : Set.of(StringUtils.commaDelimitedListToStringArray(result));
}
@Override
public Set getNullableResult(ResultSet resultSet, int i) throws SQLException {
String result = resultSet.getString(i);
return result == null ? null : Set.of(StringUtils.commaDelimitedListToStringArray(result));
}
@Override
public Set getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
String result = callableStatement.getString(i);
return result == null ? null : Set.of(StringUtils.commaDelimitedListToStringArray(result));
}
}

View File

@ -0,0 +1,17 @@
package cn.zyjblogs.starter.mybatisplus.generator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
public class GlobalIdGenerator implements IdentifierGenerator {
@Override
public Long nextId(Object entity) {
return null;
}
@Override
public String nextUUID(Object entity) {
return IdWorker.get32UUID();
}
}