🎨 改进结构和代码格式。formatter code

This commit is contained in:
冷冷 committed 2018-07-29 09:57:41 +08:00
1 parent 71f2e69ece
commit 8fa791ac17
21 files changed
+141 -152

No files matched your search

@@ -82,7 +82,6 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
}
@Bean
public TokenStore tokenStore() {
RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory);
@@ -55,9 +55,9 @@ public class JacksonConfig {
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.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")));
objectMapper.registerModule(javaTimeModule);
return objectMapper;
}
@@ -19,10 +19,10 @@
package com.pig4cloud.pigx.common.log.aspect;
import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.log.event.SysLogEvent;
import com.pig4cloud.pigx.common.log.util.SysLogUtils;
import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
@@ -75,7 +75,7 @@ public class PigxWebResponseExceptionTranslator implements WebResponseExceptionT
return handleOAuth2Exception(new MethodNotAllowed(ase.getMessage(), ase));
}
ase = (OAuth2Exception) throwableAnalyzer.getFirstThrowableOfType(
ase = (OAuth2Exception) throwableAnalyzer.getFirstThrowableOfType(
OAuth2Exception.class, causeChain);
if (ase != null) {
@@ -19,8 +19,6 @@ package com.pig4cloud.pigx.common.security.exception;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import java.util.Map;
/**
* @author lengleng
* @date 2018/7/8
@@ -46,16 +46,15 @@ import java.util.List;
@EnableAutoConfiguration
public class SwaggerAutoConfiguration {
private static final String DEFAULT_EXCLUDE_PATH = "/error";
private static final String BASE_PATH = "/**";
@Bean
@ConditionalOnMissingBean
public SwaggerProperties swaggerProperties() {
return new SwaggerProperties();
}
private static final String DEFAULT_EXCLUDE_PATH = "/error";
private static final String BASE_PATH = "/**";
@Bean
public Docket api(SwaggerProperties swaggerProperties) {
// base-path处理
@@ -120,7 +120,7 @@ public class SwaggerProperties {
*/
private List<AuthorizationScope> authorizationScopeList = new ArrayList<>();
private List<String> tokenUrlList =new ArrayList<>();
private List<String> tokenUrlList = new ArrayList<>();
}
@Data
@@ -1 +0,0 @@
restart.include.pigx-common-security=/pigx-common-swagger[\\w-]+\.jar
@@ -1,2 +0,0 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.pig4cloud.pigx.common.swagger.config.SwaggerAutoConfiguration
@@ -45,98 +45,12 @@ import java.io.IOException;
public class ImageCodeGatewayFilter extends AbstractGatewayFilterFactory {
public static final String DEFAULT_CODE_KEY = "DEFAULT_CODE_KEY";
public static final String OAUTH_TOKEN_URL = "/oauth/token";
private static final String BASIC_ = "Basic ";
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private FilterIgnorePropertiesConfig filterIgnorePropertiesConfig;
@Override
public GatewayFilter apply(Object config) {
return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest();
// 不是登录请求,直接向下执行
if (!StrUtil.containsAnyIgnoreCase(request.getURI().getPath(), OAUTH_TOKEN_URL)) {
return chain.filter(exchange);
}
// 终端设置不校验, 直接向下执行(1. 从请求参数中获取 2.从header取)
String clientId = request.getQueryParams().getFirst("client_id");
if (StrUtil.isNotBlank(clientId)) {
if (filterIgnorePropertiesConfig.getClients().contains(clientId)) {
return chain.filter(exchange);
}
}
try {
String[] clientInfos = extractAndDecodeHeader(request);
if (filterIgnorePropertiesConfig.getClients().contains(clientInfos[0])) {
return chain.filter(exchange);
}
} catch (Exception e) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.PRECONDITION_REQUIRED);
return response.setComplete();
}
//校验验证码合法性
try {
checkCode(request);
} catch (ValidateCodeException e) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.PRECONDITION_REQUIRED);
return response.setComplete();
}
return chain.filter(exchange);
};
}
/**
* 检查code
*
* @param request
* @throws ValidateCodeException 校验异常
*/
private void checkCode(ServerHttpRequest request) throws ValidateCodeException {
String code = request.getQueryParams().getFirst("code");
if (StrUtil.isBlank(code)) {
throw new ValidateCodeException();
}
String randomStr = request.getQueryParams().getFirst("randomStr");
if (StrUtil.isBlank(randomStr)) {
throw new ValidateCodeException();
}
String key = DEFAULT_CODE_KEY + randomStr;
if (!redisTemplate.hasKey(key)) {
throw new ValidateCodeException();
}
Object codeObj = redisTemplate.opsForValue().get(key);
if (codeObj == null) {
throw new ValidateCodeException();
}
String saveCode = codeObj.toString();
if (StrUtil.isBlank(saveCode)) {
redisTemplate.delete(key);
throw new ValidateCodeException();
}
if (!StrUtil.equals(saveCode, code)) {
redisTemplate.delete(key);
throw new ValidateCodeException();
}
redisTemplate.delete(key);
}
private static final String BASIC_ = "Basic ";
/**
* 从header 请求中的clientId/clientsecect
*
@@ -183,4 +97,88 @@ public class ImageCodeGatewayFilter extends AbstractGatewayFilterFactory {
return extractAndDecodeHeader(header);
}
@Override
public GatewayFilter apply(Object config) {
return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest();
// 不是登录请求,直接向下执行
if (!StrUtil.containsAnyIgnoreCase(request.getURI().getPath(), OAUTH_TOKEN_URL)) {
return chain.filter(exchange);
}
// 终端设置不校验, 直接向下执行(1. 从请求参数中获取 2.从header取)
String clientId = request.getQueryParams().getFirst("client_id");
if (StrUtil.isNotBlank(clientId)) {
if (filterIgnorePropertiesConfig.getClients().contains(clientId)) {
return chain.filter(exchange);
}
}
try {
String[] clientInfos = extractAndDecodeHeader(request);
if (filterIgnorePropertiesConfig.getClients().contains(clientInfos[0])) {
return chain.filter(exchange);
}
} catch (Exception e) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.PRECONDITION_REQUIRED);
return response.setComplete();
}
//校验验证码合法性
try {
checkCode(request);
} catch (ValidateCodeException e) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.PRECONDITION_REQUIRED);
return response.setComplete();
}
return chain.filter(exchange);
};
}
/**
* 检查code
*
* @param request
* @throws ValidateCodeException 校验异常
*/
private void checkCode(ServerHttpRequest request) throws ValidateCodeException {
String code = request.getQueryParams().getFirst("code");
if (StrUtil.isBlank(code)) {
throw new ValidateCodeException();
}
String randomStr = request.getQueryParams().getFirst("randomStr");
if (StrUtil.isBlank(randomStr)) {
throw new ValidateCodeException();
}
String key = DEFAULT_CODE_KEY + randomStr;
if (!redisTemplate.hasKey(key)) {
throw new ValidateCodeException();
}
Object codeObj = redisTemplate.opsForValue().get(key);
if (codeObj == null) {
throw new ValidateCodeException();
}
String saveCode = codeObj.toString();
if (StrUtil.isBlank(saveCode)) {
redisTemplate.delete(key);
throw new ValidateCodeException();
}
if (!StrUtil.equals(saveCode, code)) {
redisTemplate.delete(key);
throw new ValidateCodeException();
}
redisTemplate.delete(key);
}
}
@@ -50,6 +50,15 @@ public class PasswordDecoderFilter extends AbstractGatewayFilterFactory {
@Value("${security.encode.key:1234567812345678}")
private String encodeKey;
private static String decryptAES(String data, String pass) throws Exception {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
SecretKeySpec keyspec = new SecretKeySpec(pass.getBytes(), KEY_ALGORITHM);
IvParameterSpec ivspec = new IvParameterSpec(pass.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] result = cipher.doFinal(Base64.decode(data.getBytes(CharsetUtil.UTF_8)));
return new String(result, CharsetUtil.UTF_8);
}
@Override
public GatewayFilter apply(Object config) {
return (exchange, chain) -> {
@@ -84,14 +93,4 @@ public class PasswordDecoderFilter extends AbstractGatewayFilterFactory {
return chain.filter(exchange.mutate().request(newRequest).build());
};
}
private static String decryptAES(String data, String pass) throws Exception {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
SecretKeySpec keyspec = new SecretKeySpec(pass.getBytes(), KEY_ALGORITHM);
IvParameterSpec ivspec = new IvParameterSpec(pass.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] result = cipher.doFinal(Base64.decode(data.getBytes(CharsetUtil.UTF_8)));
return new String(result, CharsetUtil.UTF_8);
}
}
@@ -26,7 +26,6 @@ import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
/**
* <p>
@@ -28,7 +28,6 @@ import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
/**
* <p>
@@ -29,19 +29,19 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
/**
* @author lengleng
* @date 2018/6/22
*/
@FeignClient(value = ServiceNameConstant.UMPS_SERVICE, fallback = RemoteUserServiceFallbackImpl.class)
public interface RemoteUserService {
/**
* 通过用户名查询用户、角色信息
*
* @param username 用户名
* @param from 调用标志
* @return R
*/
@GetMapping("/user/info/{username}")
R<UserInfo> info(@PathVariable("username") String username
, @RequestHeader("from") String from);
}
* @author lengleng
* @date 2018/6/22
*/
@FeignClient(value = ServiceNameConstant.UMPS_SERVICE, fallback = RemoteUserServiceFallbackImpl.class)
public interface RemoteUserService {
/**
* 通过用户名查询用户、角色信息
*
* @param username 用户名
* @param from 调用标志
* @return R
*/
@GetMapping("/user/info/{username}")
R<UserInfo> info(@PathVariable("username") String username
, @RequestHeader("from") String from);
}
@@ -44,7 +44,7 @@ public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/actuator/**", "/user/info/*", "/log/**","/v2/api-docs").permitAll()
.antMatchers("/actuator/**", "/user/info/*", "/log/**", "/v2/api-docs").permitAll()
.anyRequest().authenticated()
.and().csrf().disable();
}
@@ -99,7 +99,7 @@ public class UserController {
@DeleteMapping("/{id}")
@PreAuthorize("@pms.hasPermission('sys_user_del')")
@ApiOperation(value = "删除用户", notes = "根据ID删除用户")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int", paramType = "path",example = "1000")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int", paramType = "path", example = "1000")
public R<Boolean> userDel(@PathVariable Integer id) {
SysUser sysUser = userService.selectById(id);
return new R<>(userService.deleteUserById(sysUser));
@@ -35,7 +35,6 @@ import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
@@ -19,7 +19,6 @@
package com.pig4cloud.pigx.admin.service.impl;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.pig4cloud.pigx.admin.api.entity.SysMenu;
@@ -50,7 +50,10 @@ import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author lengleng
@@ -28,13 +28,13 @@ import java.util.List;
*/
public class PigxDataflowJob implements DataflowJob<Integer> {
@Override
public List<Integer> fetchData(ShardingContext shardingContext) {
return null;
}
@Override
public List<Integer> fetchData(ShardingContext shardingContext) {
return null;
}
@Override
public void processData(ShardingContext shardingContext, List<Integer> list) {
@Override
public void processData(ShardingContext shardingContext, List<Integer> list) {
}
}
}
@@ -28,13 +28,13 @@ import lombok.extern.slf4j.Slf4j;
*/
@Slf4j
public class PigxSimpleJob implements SimpleJob {
/**
* 业务执行逻辑
*
* @param shardingContext 分片信息
*/
@Override
public void execute(ShardingContext shardingContext) {
log.info("shardingContext:{}", shardingContext);
}
/**
* 业务执行逻辑
*
* @param shardingContext 分片信息
*/
@Override
public void execute(ShardingContext shardingContext) {
log.info("shardingContext:{}", shardingContext);
}
}