Merge branch 'dev' of https://gitee.ltd/pig/pigx into dev

# Conflicts:
#	pigx-config/src/main/resources/config/pigx-codegen-dev.yml
This commit is contained in:
冷冷 committed 2018-10-24 21:31:48 +08:00
commit 58d8f245e5
25 files changed
+178 -83

No files matched your search

+1 -1
View File
@@ -4,7 +4,7 @@
Source Server : RDS本地使用
Source Server Type : MySQL
Source Server Version : 50721
Source Host : 127.0.0.1
Source Host : lengleng
Source Database : pigxx
Target Server Type : MySQL
@@ -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())
@@ -91,8 +91,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;
}
@@ -32,7 +32,7 @@ import org.springframework.stereotype.Component;
public class PigxAuthenticationFailureEvenHandler extends AuthenticationFailureEvenHandler {
/**
* 处理登录成功方法
* 处理登录失败方法
* <p>
*
* @param authenticationException 登录的authentication 对象
+1 -1
View File
@@ -20,4 +20,4 @@ eureka:
prefer-ip-address: true
client:
service-url:
defaultZone: http://pig:pig@localhost:1025/eureka/
defaultZone: http://pig:pig@lengleng:1025/eureka/
+10 -4
View File
@@ -70,12 +70,18 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!--jacksonJSR310规范-->
<!--jackson模块-->
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-modules-java8</artifactId>
<version>${jackson.modules}</version>
<type>pom</type>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<!--TTL-->
<dependency>
@@ -1,56 +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.jsr310.JavaTimeModule;
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 特殊处理
* .registerModule(new ParameterNamesModule())
* .registerModule(new Jdk8Module())
* .registerModule(new JavaTimeModule()); // new module, NOT JSR310Module
* 针对JDK 1.8的日期时间格式特殊处理
*
* @return
* @return ObjectMapper
*/
@Bean
public ObjectMapper getObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.findAndRegisterModules();
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);
// 忽略无法转换的对象
@@ -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";
}
@@ -99,6 +99,7 @@ public class MobileLoginSuccessHandler implements AuthenticationSuccessHandler {
throw new BadCredentialsException(
"Failed to decode basic authentication token");
}
}
@@ -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) {
}
}
}
@@ -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<DefaultSecurityFilterChain, HttpSecurity> {
@Autowired
private AuthenticationEventPublisher defaultAuthenticationEventPublisher;
private AuthenticationSuccessHandler mobileLoginSuccessHandler;
private PigxUserDetailsService userDetailsService;
@@ -43,6 +49,7 @@ public class MobileSecurityConfigurer extends SecurityConfigurerAdapter<DefaultS
MobileAuthenticationFilter mobileAuthenticationFilter = new MobileAuthenticationFilter();
mobileAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
mobileAuthenticationFilter.setAuthenticationSuccessHandler(mobileLoginSuccessHandler);
mobileAuthenticationFilter.setEventPublisher(defaultAuthenticationEventPublisher);
MobileAuthenticationProvider mobileAuthenticationProvider = new MobileAuthenticationProvider();
mobileAuthenticationProvider.setUserDetailsService(userDetailsService);
@@ -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
* <p>
* 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);
}
}
+1 -1
View File
@@ -19,7 +19,7 @@ eureka:
prefer-ip-address: true
client:
service-url:
defaultZone: http://pig:pig@localhost:1025/eureka/
defaultZone: http://pig:pig@lengleng:1025/eureka/
# 暴露监控端点
management:
@@ -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:
@@ -94,7 +98,7 @@ swagger:
## spring security 配置
security:
auth:
server: http://localhost:9999/auth/oauth
server: http://lengleng:9999/auth/oauth
oauth2:
resource:
token-info-uri: ${security.auth.server}/check_token
@@ -13,9 +13,6 @@ spring:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://mysql:3306/pigxx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
url: jdbc:mysql://mysql:3306/pigx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
resources:
static-locations: classpath:/static/,classpath:/views/
+1 -1
View File
@@ -13,7 +13,7 @@ spring:
# 类似的 redis 使用pigx-redis ,gateway 换成 pigx-gateway
eureka:
instance:
hostname: localhost
hostname: lengleng
prefer-ip-address: true
client:
register-with-eureka: false
@@ -22,6 +22,6 @@ eureka:
prefer-ip-address: true
client:
service-url:
defaultZone: http://pig:pig@localhost:1025/eureka/
defaultZone: http://pig:pig@lengleng:1025/eureka/
@@ -22,6 +22,7 @@ package com.pig4cloud.pigx.admin;
import com.pig4cloud.pigx.common.security.feign.EnablePigxFeignClients;
import com.pig4cloud.pigx.common.swagger.annotation.EnablePigxSwagger2;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
@@ -37,4 +38,5 @@ public class PigxAdminApplication {
public static void main(String[] args) {
SpringApplication.run(PigxAdminApplication.class, args);
}
}
@@ -16,6 +16,9 @@ spring:
discovery:
enabled: true
service-id: pigx-config
datasource:
url:
type: com.zaxxer.hikari.HikariDataSource
# 注册中心配置
eureka:
@@ -23,4 +26,4 @@ eureka:
prefer-ip-address: true
client:
service-url:
defaultZone: http://pig:pig@localhost:1025/eureka/
defaultZone: http://pig:pig@lengleng:1025/eureka/
@@ -14,7 +14,7 @@ spring:
enabled: true
service-id: pigx-config
datasource:
url: jdbc:mysql://127.0.0.1:3306/ac?characterEncoding=UTF-8&useSSL=false
url: jdbc:mysql://lengleng:3306/ac?characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
@@ -27,7 +27,7 @@ eureka:
prefer-ip-address: true
client:
service-url:
defaultZone: http://pig:pig@localhost:1025/eureka/
defaultZone: http://pig:pig@lengleng:1025/eureka/
## spring security 配置
security:
@@ -39,7 +39,7 @@ security:
resource:
token-info-uri: ${security.auth.server}/check_token
auth:
server: http://localhost:9999/auth/oauth
server: http://lengleng:9999/auth/oauth
# 加解密根密码
jasypt:
@@ -20,4 +20,4 @@ eureka:
prefer-ip-address: true
client:
service-url:
defaultZone: http://pig:pig@localhost:1025/eureka/
defaultZone: http://pig:pig@lengleng:1025/eureka/
@@ -20,4 +20,4 @@ eureka:
prefer-ip-address: true
client:
service-url:
defaultZone: http://pig:pig@localhost:1025/eureka/
defaultZone: http://pig:pig@lengleng:1025/eureka/
@@ -20,4 +20,4 @@ eureka:
prefer-ip-address: true
client:
service-url:
defaultZone: http://pig:pig@localhost:1025/eureka/
defaultZone: http://pig:pig@lengleng:1025/eureka/
@@ -20,5 +20,5 @@ eureka:
prefer-ip-address: true
client:
service-url:
defaultZone: http://pig:pig@localhost:1025/eureka/
defaultZone: http://pig:pig@lengleng:1025/eureka/
+8
View File
@@ -136,6 +136,14 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<!--jackson模块-->
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-modules-java8</artifactId>
<version>${jackson.modules}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>