From efb81270f26620ecc43b9b1a5a541539b2b2d9bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9A=96=E6=9A=96?= Date: Mon, 22 Oct 2018 14:16:29 +0800 Subject: [PATCH 1/5] =?UTF-8?q?:zap:=20=E4=BC=98=E5=8C=96=E6=80=A7?= =?UTF-8?q?=E8=83=BD:=E4=BC=98=E5=8C=96jdk1.8=E4=B8=8Bjackson=E7=9A=84?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=96=E8=A1=A8=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pigx-common/pigx-common-core/pom.xml | 13 +++++++++---- .../pigx/common/core/config/JacksonConfig.java | 11 +++++++---- .../src/main/resources/config/application-dev.yml | 4 ++++ .../src/main/resources/config/pigx-codegen-dev.yml | 3 --- pom.xml | 8 ++++++++ 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/pigx-common/pigx-common-core/pom.xml b/pigx-common/pigx-common-core/pom.xml index d95b0daec..6006be1af 100644 --- a/pigx-common/pigx-common-core/pom.xml +++ b/pigx-common/pigx-common-core/pom.xml @@ -70,12 +70,17 @@ org.springframework.boot spring-boot-starter-validation - com.fasterxml.jackson.module - jackson-modules-java8 - ${jackson.modules} - pom + jackson-module-parameter-names + + + com.fasterxml.jackson.datatype + jackson-datatype-jdk8 + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/JacksonConfig.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/JacksonConfig.java index f828857dd..eb2401d08 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/JacksonConfig.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/JacksonConfig.java @@ -22,6 +22,9 @@ package com.pig4cloud.pigx.common.core.config; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; @@ -41,16 +44,16 @@ import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; public class JacksonConfig { /** * 针对JDK 1.8 特殊处理 - * .registerModule(new ParameterNamesModule()) - * .registerModule(new Jdk8Module()) - * .registerModule(new JavaTimeModule()); // new module, NOT JSR310Module * * @return */ @Bean public ObjectMapper getObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); - objectMapper.findAndRegisterModules(); + ObjectMapper mapper = new ObjectMapper() + .registerModule(new ParameterNamesModule()) + .registerModule(new Jdk8Module()) + .registerModule(new JavaTimeModule()); // new module, NOT JSR310Module // 忽略json字符串中不识别的属性 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // 忽略无法转换的对象 diff --git a/pigx-config/src/main/resources/config/application-dev.yml b/pigx-config/src/main/resources/config/application-dev.yml index 9f1e379b9..6ffd458a0 100644 --- a/pigx-config/src/main/resources/config/application-dev.yml +++ b/pigx-config/src/main/resources/config/application-dev.yml @@ -7,6 +7,10 @@ spring: redis: password: host: redis + # Jackson配置 + jackson: + time-zone: GMT+8 + date-format: yyyy-MM-dd HH:mm:ss # 暴露监控端点 management: endpoints: diff --git a/pigx-config/src/main/resources/config/pigx-codegen-dev.yml b/pigx-config/src/main/resources/config/pigx-codegen-dev.yml index 133c73ab7..923770e6a 100644 --- a/pigx-config/src/main/resources/config/pigx-codegen-dev.yml +++ b/pigx-config/src/main/resources/config/pigx-codegen-dev.yml @@ -14,8 +14,5 @@ spring: username: root password: 123456 url: jdbc:mysql://mysql:3306/pigx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false - jackson: - time-zone: GMT+8 - date-format: yyyy-MM-dd HH:mm:ss resources: static-locations: classpath:/static/,classpath:/views/ diff --git a/pom.xml b/pom.xml index cfec5bc70..8f5ab5fcb 100644 --- a/pom.xml +++ b/pom.xml @@ -134,6 +134,14 @@ pom import + + + com.fasterxml.jackson.module + jackson-modules-java8 + ${jackson.modules} + pom + import + From fe9726cad4c65464056c0ac41a969e9b76eb0865 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9A=96=E6=9A=96?= Date: Mon, 22 Oct 2018 19:08:03 +0800 Subject: [PATCH 2/5] =?UTF-8?q?:zap:=20=E4=BC=98=E5=8C=96=E6=80=A7?= =?UTF-8?q?=E8=83=BD:=E4=BC=98=E5=8C=96jdk1.8=E4=B8=8Bjackson=E7=9A=84?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=96=E8=A1=A8=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pigx-common/pigx-common-core/pom.xml | 1 + .../common/core/config/JacksonConfig.java | 57 +++++++++---------- pom.xml | 2 +- 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/pigx-common/pigx-common-core/pom.xml b/pigx-common/pigx-common-core/pom.xml index 6006be1af..eb4dd7005 100644 --- a/pigx-common/pigx-common-core/pom.xml +++ b/pigx-common/pigx-common-core/pom.xml @@ -70,6 +70,7 @@ org.springframework.boot spring-boot-starter-validation + com.fasterxml.jackson.module jackson-module-parameter-names diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/JacksonConfig.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/JacksonConfig.java index eb2401d08..678cfde84 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/JacksonConfig.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/JacksonConfig.java @@ -1,59 +1,54 @@ -/* - * - * Copyright (c) 2018-2025, lengleng All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * Neither the name of the pig4cloud.com developer nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * Author: lengleng (wangiegie@gmail.com) - * - */ - package com.pig4cloud.pigx.common.core.config; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; -import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; -import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; /** * JacksonConfig * * @author: lengleng - * @date: 2018/7/28 12:29 + * @author: lishangbu + * @date: 2018/10/22 */ @Configuration @ConditionalOnClass(ObjectMapper.class) @AutoConfigureBefore(JacksonAutoConfiguration.class) public class JacksonConfig { /** - * 针对JDK 1.8 特殊处理 + * 针对JDK 1.8的日期时间格式特殊处理 * - * @return + * @return ObjectMapper */ @Bean - public ObjectMapper getObjectMapper(Jackson2ObjectMapperBuilder builder) { - ObjectMapper objectMapper = builder.createXmlMapper(false).build(); - ObjectMapper mapper = new ObjectMapper() - .registerModule(new ParameterNamesModule()) - .registerModule(new Jdk8Module()) - .registerModule(new JavaTimeModule()); // new module, NOT JSR310Module + public ObjectMapper getObjectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + JavaTimeModule javaTimeModule = new JavaTimeModule(); + javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); + javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); + javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); + javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); + javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); + javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); + // javaTimeModule只能手动注册,参考https://github.com/FasterXML/jackson-modules-java8 + objectMapper.registerModule(javaTimeModule); // 忽略json字符串中不识别的属性 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // 忽略无法转换的对象 diff --git a/pom.xml b/pom.xml index 8f5ab5fcb..ec6cf3b23 100644 --- a/pom.xml +++ b/pom.xml @@ -134,7 +134,7 @@ pom import - + com.fasterxml.jackson.module jackson-modules-java8 From 30dd728b6eaf5da3a92798852d4f133fea38516d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Wed, 24 Oct 2018 14:52:49 +0800 Subject: [PATCH 3/5] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=E7=A4=BE?= =?UTF-8?q?=E4=BA=A4=E7=99=BB=E5=BD=95=E3=80=81=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E7=99=BB=E5=BD=95=20=E6=88=90=E5=8A=9F=E3=80=81=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E4=BA=8B=E4=BB=B6=E4=B8=8D=E9=80=9A=E7=9F=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/AuthorizationServerConfig.java | 8 +++--- .../auth/config/WebSecurityConfigurer.java | 6 +++-- .../PigxAuthenticationFailureEvenHandler.java | 2 +- .../mobile/MobileAuthenticationFilter.java | 25 +++++++++---------- .../mobile/MobileSecurityConfigurer.java | 11 ++++++-- 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/AuthorizationServerConfig.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/AuthorizationServerConfig.java index e08d45e56..01fa8afbc 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/AuthorizationServerConfig.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/AuthorizationServerConfig.java @@ -21,6 +21,7 @@ package com.pig4cloud.pigx.auth.config; import com.pig4cloud.pigx.common.core.constant.SecurityConstants; import com.pig4cloud.pigx.common.security.component.PigxWebResponseExceptionTranslator; +import com.pig4cloud.pigx.common.security.service.PigxClientDetailsService; import com.pig4cloud.pigx.common.security.service.PigxUserDetailsService; import lombok.AllArgsConstructor; import org.springframework.context.annotation.Bean; @@ -34,7 +35,6 @@ import org.springframework.security.oauth2.config.annotation.web.configuration.A import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer; -import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService; import org.springframework.security.oauth2.provider.token.TokenEnhancer; import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore; @@ -59,21 +59,21 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { - JdbcClientDetailsService clientDetailsService = new JdbcClientDetailsService(dataSource); + PigxClientDetailsService clientDetailsService = new PigxClientDetailsService(dataSource); clientDetailsService.setSelectClientDetailsSql(SecurityConstants.DEFAULT_SELECT_STATEMENT); clientDetailsService.setFindClientDetailsSql(SecurityConstants.DEFAULT_FIND_STATEMENT); clients.withClientDetails(clientDetailsService); } @Override - public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { + public void configure(AuthorizationServerSecurityConfigurer oauthServer) { oauthServer .allowFormAuthenticationForClients() .checkTokenAccess("permitAll()"); } @Override - public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { + public void configure(AuthorizationServerEndpointsConfigurer endpoints) { endpoints .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST) .tokenStore(tokenStore()) 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 2935ee747..b68310693 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 @@ -89,8 +89,10 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { @Bean public MobileSecurityConfigurer mobileSecurityConfigurer() { - return new MobileSecurityConfigurer(mobileLoginSuccessHandler() - , userDetailsService); + MobileSecurityConfigurer mobileSecurityConfigurer = new MobileSecurityConfigurer(); + mobileSecurityConfigurer.setMobileLoginSuccessHandler(mobileLoginSuccessHandler()); + mobileSecurityConfigurer.setUserDetailsService(userDetailsService); + return mobileSecurityConfigurer; } diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationFailureEvenHandler.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationFailureEvenHandler.java index 92e3cdfa3..ceb0321fb 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationFailureEvenHandler.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationFailureEvenHandler.java @@ -32,7 +32,7 @@ import org.springframework.stereotype.Component; public class PigxAuthenticationFailureEvenHandler extends AuthenticationFailureEvenHandler { /** - * 处理登录成功方法 + * 处理登录失败方法 *

* * @param authenticationException 登录的authentication 对象 diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/mobile/MobileAuthenticationFilter.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/mobile/MobileAuthenticationFilter.java index 7eb4fdb62..98494a522 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/mobile/MobileAuthenticationFilter.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/mobile/MobileAuthenticationFilter.java @@ -24,6 +24,7 @@ import org.springframework.http.HttpMethod; import org.springframework.security.authentication.AuthenticationEventPublisher; import org.springframework.security.authentication.AuthenticationServiceException; import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.authentication.DefaultAuthenticationEventPublisher; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.context.SecurityContextHolder; @@ -43,7 +44,6 @@ import javax.servlet.http.HttpServletResponse; */ public class MobileAuthenticationFilter extends AbstractAuthenticationProcessingFilter { private static final String SPRING_SECURITY_FORM_MOBILE_KEY = "mobile"; - private AuthenticationEventPublisher eventPublisher = new MobileAuthenticationFilter.NullEventPublisher(); private AuthenticationEntryPoint authenticationEntryPoint = new MobileAuthenticationEntryPoint(); @Getter @Setter @@ -51,6 +51,9 @@ public class MobileAuthenticationFilter extends AbstractAuthenticationProcessing @Getter @Setter private boolean postOnly = true; + @Getter + @Setter + private AuthenticationEventPublisher eventPublisher; public MobileAuthenticationFilter() { super(new AntPathRequestMatcher(SecurityConstants.MOBILE_TOKEN_URL, "POST")); @@ -76,8 +79,13 @@ public class MobileAuthenticationFilter extends AbstractAuthenticationProcessing setDetails(request, mobileAuthenticationToken); + Authentication authResult = null; try { - return this.getAuthenticationManager().authenticate(mobileAuthenticationToken); + authResult = this.getAuthenticationManager().authenticate(mobileAuthenticationToken); + + logger.debug("Authentication success: " + authResult); + eventPublisher.publishAuthenticationSuccess(authResult); + SecurityContextHolder.getContext().setAuthentication(authResult); } catch (Exception failed) { SecurityContextHolder.clearContext(); @@ -93,7 +101,8 @@ public class MobileAuthenticationFilter extends AbstractAuthenticationProcessing logger.error("authenticationEntryPoint handle error:{}", failed); } } - return null; + + return authResult; } private String obtainMobile(HttpServletRequest request) { @@ -104,15 +113,5 @@ public class MobileAuthenticationFilter extends AbstractAuthenticationProcessing MobileAuthenticationToken authRequest) { authRequest.setDetails(authenticationDetailsSource.buildDetails(request)); } - - private static final class NullEventPublisher implements AuthenticationEventPublisher { - @Override - public void publishAuthenticationFailure(AuthenticationException exception, Authentication authentication) { - } - - @Override - public void publishAuthenticationSuccess(Authentication authentication) { - } - } } diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/mobile/MobileSecurityConfigurer.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/mobile/MobileSecurityConfigurer.java index f71672931..25a67aaed 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/mobile/MobileSecurityConfigurer.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/mobile/MobileSecurityConfigurer.java @@ -18,7 +18,10 @@ package com.pig4cloud.pigx.common.security.mobile; import com.pig4cloud.pigx.common.security.service.PigxUserDetailsService; -import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.authentication.AuthenticationEventPublisher; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.SecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.builders.HttpSecurity; @@ -32,9 +35,12 @@ import org.springframework.stereotype.Component; * @date 2018/8/5 * 手机号登录配置入口 */ +@Getter +@Setter @Component -@AllArgsConstructor public class MobileSecurityConfigurer extends SecurityConfigurerAdapter { + @Autowired + private AuthenticationEventPublisher defaultAuthenticationEventPublisher; private AuthenticationSuccessHandler mobileLoginSuccessHandler; private PigxUserDetailsService userDetailsService; @@ -43,6 +49,7 @@ public class MobileSecurityConfigurer extends SecurityConfigurerAdapter Date: Wed, 24 Oct 2018 14:57:52 +0800 Subject: [PATCH 4/5] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=E7=A4=BE?= =?UTF-8?q?=E4=BA=A4=E7=99=BB=E5=BD=95=E3=80=81=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E7=99=BB=E5=BD=95=20=E6=88=90=E5=8A=9F=E3=80=81=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E4=BA=8B=E4=BB=B6=E4=B8=8D=E9=80=9A=E7=9F=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/PigxClientDetailsService.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxClientDetailsService.java diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxClientDetailsService.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxClientDetailsService.java new file mode 100644 index 000000000..6a6007b38 --- /dev/null +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxClientDetailsService.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.security.service; + +import com.pig4cloud.pigx.common.core.constant.SecurityConstants; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.security.oauth2.common.exceptions.InvalidClientException; +import org.springframework.security.oauth2.provider.ClientDetails; +import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService; + +import javax.sql.DataSource; + +/** + * @author lengleng + * @date 2018/10/22 + *

+ * see JdbcClientDetailsService + */ +public class PigxClientDetailsService extends JdbcClientDetailsService { + + public PigxClientDetailsService(DataSource dataSource) { + super(dataSource); + } + + /** + * 重写原生方法支持redis缓存 + * + * @param clientId + * @return + * @throws InvalidClientException + */ + @Override + @Cacheable(value = SecurityConstants.CLIENT_DETAILS_KEY, key = "#clientId", unless = "#result == null") + public ClientDetails loadClientByClientId(String clientId) throws InvalidClientException { + return super.loadClientByClientId(clientId); + } +} From c545cd9a1d005d622f974d101f07e47ca47144a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Wed, 24 Oct 2018 15:05:22 +0800 Subject: [PATCH 5/5] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=E7=A4=BE?= =?UTF-8?q?=E4=BA=A4=E7=99=BB=E5=BD=95=E3=80=81=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E7=99=BB=E5=BD=95=20=E6=88=90=E5=8A=9F=E3=80=81=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E4=BA=8B=E4=BB=B6=E4=B8=8D=E9=80=9A=E7=9F=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/constant/SecurityConstants.java | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/SecurityConstants.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/SecurityConstants.java index 8a9158b21..8d2fcd944 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/SecurityConstants.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/SecurityConstants.java @@ -52,6 +52,32 @@ public interface SecurityConstants { */ String FROM = "from"; + /** + * 手机号登录URL + */ + String MOBILE_TOKEN_URL = "/mobile/token"; + + /** + * 默认登录URL + */ + String OAUTH_TOKEN_URL = "/oauth/token"; + + /** + * grant_type + */ + String REFRESH_TOKEN = "refresh_token"; + + /** + * oauth 客户端信息 + */ + String CLIENT_DETAILS_KEY = "pigx_oauth:client:details"; + + /** + * 微信获取OPENID + */ + String WX_AUTHORIZATION_CODE_URL = "https://api.weixin.qq.com/sns/oauth2/access_token" + + "?appid=%s&secret=%s&code=%s&grant_type=authorization_code"; + /** * {bcrypt} 加密的特征码 */ @@ -79,14 +105,4 @@ public interface SecurityConstants { */ String DEFAULT_SELECT_STATEMENT = BASE_FIND_STATEMENT + " where client_id = ?"; - /** - * 手机号登录URL - */ - String MOBILE_TOKEN_URL = "/mobile/token"; - - /** - * 微信获取OPENID - */ - String WX_AUTHORIZATION_CODE_URL = "https://api.weixin.qq.com/sns/oauth2/access_token" + - "?appid=%s&secret=%s&code=%s&grant_type=authorization_code"; }