diff --git a/pom.xml b/pom.xml index cbd722d5..c20bf656 100644 --- a/pom.xml +++ b/pom.xml @@ -16,6 +16,7 @@ pom + survey-checkstyle survey-server survey-common survey-dal @@ -72,6 +73,16 @@ java-jwt ${jwt.version} + + com.alibaba + fastjson + ${fastjson.version} + + + org.apache.commons + commons-lang3 + ${lang3.version} + @@ -123,6 +134,86 @@ + + + org.apache.maven.plugins + maven-pmd-plugin + 3.16.0 + + true + + + rulesets/java/ali-concurrent.xml + rulesets/java/ali-constant.xml + rulesets/java/ali-exception.xml + rulesets/java/ali-flowcontrol.xml + rulesets/java/ali-naming.xml + rulesets/java/ali-oop.xml + rulesets/java/ali-orm.xml + rulesets/java/ali-other.xml + rulesets/java/ali-set.xml + + true + + + + + pmd-check-verify + package + + check + + + + + + com.alibaba.p3c + p3c-pmd + 2.1.1 + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.1.2 + + checkstyle-rule.xml + UTF-8 + true + true + true + + + + validate + validate + + check + + + + + + com.xiaojusurvey.engine + survey-checkstyle + base + + + com.puppycrawl.tools + checkstyle + 8.41.1 + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + false + + diff --git a/survey-checkstyle/pom.xml b/survey-checkstyle/pom.xml new file mode 100644 index 00000000..34ecb2a6 --- /dev/null +++ b/survey-checkstyle/pom.xml @@ -0,0 +1,25 @@ + + + + survey-engine + com.xiaojusurvey.engine + ${revision} + + 4.0.0 + survey-checkstyle + base + jar + + + + + maven-deploy-plugin + + true + + + + + \ No newline at end of file diff --git a/survey-checkstyle/src/main/resources/checkstyle-rule.xml b/survey-checkstyle/src/main/resources/checkstyle-rule.xml new file mode 100644 index 00000000..2e8e570c --- /dev/null +++ b/survey-checkstyle/src/main/resources/checkstyle-rule.xml @@ -0,0 +1,176 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/survey-common/pom.xml b/survey-common/pom.xml index 0adca759..73f63aa2 100644 --- a/survey-common/pom.xml +++ b/survey-common/pom.xml @@ -31,8 +31,6 @@ spring-boot-starter-validation - - com.auth0 diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/Status.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/Status.java index de24fc96..30b850c4 100644 --- a/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/Status.java +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/Status.java @@ -3,7 +3,6 @@ package com.xiaojusurvey.engine.common.entity; import lombok.Data; import java.io.Serializable; -import java.util.Date; @Data public class Status implements Serializable { diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/user/User.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/user/User.java index 7eb2bae4..a1040b85 100644 --- a/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/user/User.java +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/user/User.java @@ -5,11 +5,9 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.ToString; import org.springframework.data.mongodb.core.mapping.Document; + import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.ArrayList; /** * @Author: LYF @@ -20,7 +18,7 @@ import java.util.ArrayList; @EqualsAndHashCode(callSuper = true) @ToString(callSuper = true) @Data -public class User extends BaseEntity{ +public class User extends BaseEntity { /** * 用户名 diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/BaseException.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/BaseException.java index 847d770e..707794f7 100644 --- a/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/BaseException.java +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/BaseException.java @@ -14,15 +14,15 @@ public abstract class BaseException extends RuntimeException { public BaseException(String errorMsg, Integer code) { super(errorMsg); - this.code=code; - this.errorMsg=errorMsg; - this.message=errorMsg; + this.code = code; + this.errorMsg = errorMsg; + this.message = errorMsg; } - public BaseException(String errorMsg, Integer code,Throwable t) { - super(errorMsg,t); - this.code=code; - this.errorMsg=errorMsg; - this.message=errorMsg; + public BaseException(String errorMsg, Integer code, Throwable t) { + super(errorMsg, t); + this.code = code; + this.errorMsg = errorMsg; + this.message = errorMsg; } } diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/DaoException.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/DaoException.java index 169cb251..8e30f92b 100644 --- a/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/DaoException.java +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/DaoException.java @@ -1,6 +1,6 @@ package com.xiaojusurvey.engine.common.exception; -public class DaoException extends BaseException{ +public class DaoException extends BaseException { public DaoException(String errorMsg, Integer code) { super(errorMsg, code); diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/AuthService.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/AuthService.java index ed370e33..9a925e57 100644 --- a/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/AuthService.java +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/AuthService.java @@ -1,8 +1,8 @@ package com.xiaojusurvey.engine.core.auth; -import com.xiaojusurvey.engine.common.entity.user.CaptchaVo; -import com.xiaojusurvey.engine.core.auth.domain.UserParam; -import com.xiaojusurvey.engine.core.auth.domain.UserVo; +import com.xiaojusurvey.engine.core.auth.vo.CaptchaVo; +import com.xiaojusurvey.engine.core.auth.param.UserParam; +import com.xiaojusurvey.engine.core.auth.vo.UserVo; /** * @Author: LYF diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/captcha/CaptchaGenerator.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/captcha/CaptchaGenerator.java index 30817987..ac797d4b 100644 --- a/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/captcha/CaptchaGenerator.java +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/captcha/CaptchaGenerator.java @@ -1,7 +1,7 @@ package com.xiaojusurvey.engine.core.auth.captcha; import com.xiaojusurvey.engine.common.entity.user.Captcha; -import com.xiaojusurvey.engine.common.entity.user.CaptchaVo; +import com.xiaojusurvey.engine.core.auth.vo.CaptchaVo; /** * @Author: LYF @@ -11,6 +11,7 @@ import com.xiaojusurvey.engine.common.entity.user.CaptchaVo; public interface CaptchaGenerator { /** * 根据文本生成SVG + * * @param text * @return */ @@ -19,16 +20,17 @@ public interface CaptchaGenerator { /** * 生成验证码持久对象 + * * @param length * @return */ Captcha generateRandomText(int length); default CaptchaVo generateRandomSvg(Captcha captcha) { - if (captcha == null || captcha.getId() == null){ + if (captcha == null || captcha.getId() == null) { throw new IllegalArgumentException("captcha or id is null"); - }else { + } else { return new CaptchaVo(captcha.getId(), generateRandomSvg(captcha.getText())); } } -} \ No newline at end of file +} diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/captcha/SimpleCaptchaGenerator.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/captcha/SimpleCaptchaGenerator.java index ce0d0fbb..fd520983 100644 --- a/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/captcha/SimpleCaptchaGenerator.java +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/captcha/SimpleCaptchaGenerator.java @@ -11,18 +11,21 @@ import java.util.Random; * @Description: 简单验证码生成工具类 */ @Service("simpleCaptchaGenerator") -public class SimpleCaptchaGenerator implements CaptchaGenerator { +public class SimpleCaptchaGenerator implements CaptchaGenerator { @Override public String generateRandomSvg(String text) { // 生成包含验证码的 SVG 数据 - String svgData = "" + - "" + - "" + text + "" + - ""; + String svgData = "" + + "" + + "" + + text + + "" + + ""; return svgData; } + @Override public Captcha generateRandomText(int length) { String chars = "123456789"; diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/impl/AuthServiceImpl.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/impl/AuthServiceImpl.java index 4260edff..60422eaa 100644 --- a/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/impl/AuthServiceImpl.java +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/impl/AuthServiceImpl.java @@ -3,13 +3,13 @@ package com.xiaojusurvey.engine.core.auth.impl; import com.xiaojusurvey.engine.common.constants.RespErrorCode; import com.xiaojusurvey.engine.common.entity.token.Token; import com.xiaojusurvey.engine.common.entity.user.Captcha; -import com.xiaojusurvey.engine.common.entity.user.CaptchaVo; +import com.xiaojusurvey.engine.core.auth.vo.CaptchaVo; import com.xiaojusurvey.engine.common.entity.user.User; import com.xiaojusurvey.engine.common.exception.ServiceException; import com.xiaojusurvey.engine.core.auth.AuthService; import com.xiaojusurvey.engine.core.auth.captcha.CaptchaGenerator; -import com.xiaojusurvey.engine.core.auth.domain.UserParam; -import com.xiaojusurvey.engine.core.auth.domain.UserVo; +import com.xiaojusurvey.engine.core.auth.param.UserParam; +import com.xiaojusurvey.engine.core.auth.vo.UserVo; import com.xiaojusurvey.engine.core.auth.util.AuthUtil; import com.xiaojusurvey.engine.core.auth.util.JwtTokenUtil; import com.xiaojusurvey.engine.core.user.UserService; diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/domain/UserParam.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/param/UserParam.java similarity index 93% rename from survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/domain/UserParam.java rename to survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/param/UserParam.java index ef10952a..0b4288be 100644 --- a/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/domain/UserParam.java +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/param/UserParam.java @@ -1,4 +1,4 @@ -package com.xiaojusurvey.engine.core.auth.domain; +package com.xiaojusurvey.engine.core.auth.param; import com.xiaojusurvey.engine.common.entity.user.User; import lombok.Data; diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/util/AuthUtil.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/util/AuthUtil.java index fb86328a..8370e59a 100644 --- a/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/util/AuthUtil.java +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/util/AuthUtil.java @@ -1,6 +1,5 @@ package com.xiaojusurvey.engine.core.auth.util; -import com.xiaojusurvey.engine.common.entity.user.User; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -14,9 +13,10 @@ public class AuthUtil { /** * 生成BCryptPasswordEncoder密码 + * * @return 加密字符串 */ - public static String encryptPassword(String password,String username) { + public static String encryptPassword(String password, String username) { String inputWithSalt = password + username; try { MessageDigest md = MessageDigest.getInstance("MD5"); @@ -32,5 +32,4 @@ public class AuthUtil { } - } diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/util/JwtTokenUtil.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/util/JwtTokenUtil.java index c0469fe6..4123b9f3 100644 --- a/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/util/JwtTokenUtil.java +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/util/JwtTokenUtil.java @@ -3,9 +3,7 @@ package com.xiaojusurvey.engine.core.auth.util; import com.auth0.jwt.JWT; import com.auth0.jwt.JWTVerifier; import com.auth0.jwt.algorithms.Algorithm; -import com.auth0.jwt.interfaces.Claim; import com.auth0.jwt.interfaces.DecodedJWT; -import com.sun.javafx.binding.StringConstant; import com.xiaojusurvey.engine.common.constants.RespErrorCode; import com.xiaojusurvey.engine.common.entity.token.Token; import com.xiaojusurvey.engine.common.entity.user.User; @@ -16,7 +14,6 @@ import org.springframework.util.StringUtils; import javax.servlet.http.HttpServletRequest; import java.util.Date; -import java.util.Map; import java.util.UUID; import java.util.regex.Pattern; @@ -36,21 +33,23 @@ public class JwtTokenUtil { /** * 认证头 */ - public static final String AUTHORIZATION_HEADER = "Authorization"; + private static final String AUTHORIZATION_HEADER = "Authorization"; /** * 空格 */ - public static final String SPACE = " "; + private static final String SPACE = " "; /** * 令牌前缀正则表达式 */ - public final static String BEARER_PATTERN = "^Bearer$"; + private static final String BEARER_PATTERN = "^Bearer$"; + private static final int LENGTH = 2; /** * 生成token + * * @param user * @return */ @@ -64,11 +63,12 @@ public class JwtTokenUtil { .withExpiresAt(expiryDate) .withJWTId(UUID.randomUUID().toString()) .sign(Algorithm.HMAC256(secret)); - return new Token(user.getUsername(), token,new Date(now.getTime() + expirationTime * HOUR_SECOND)); + return new Token(user.getUsername(), token, new Date(now.getTime() + expirationTime * HOUR_SECOND)); } /** * 解密访问令牌 + * * @param tokenString 令牌 * @return 密钥内容 */ @@ -77,20 +77,19 @@ public class JwtTokenUtil { JWTVerifier verifier = JWT.require(Algorithm.HMAC256(secret)).build(); DecodedJWT jwt = verifier.verify(tokenString); return jwt; - }catch (Exception e){ + } catch (Exception e) { throw new ServiceException(RespErrorCode.USER_NOT_EXISTS.getMessage(), RespErrorCode.USER_NOT_EXISTS.getCode()); } } - public DecodedJWT getTokenStrByRequest(HttpServletRequest request) { String header = request.getHeader(AUTHORIZATION_HEADER); if (!StringUtils.hasText(header)) { throw new ServiceException(RespErrorCode.USER_CREDENTIALS_ERROR.getMessage(), RespErrorCode.USER_CREDENTIALS_ERROR.getCode()); } String[] splits = header.split(SPACE); - if (splits.length != 2) { + if (splits.length != LENGTH) { throw new ServiceException(RespErrorCode.USER_CREDENTIALS_ERROR.getMessage(), RespErrorCode.USER_CREDENTIALS_ERROR.getCode()); } if (!Pattern.matches(BEARER_PATTERN, splits[0])) { diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/user/CaptchaVo.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/vo/CaptchaVo.java similarity index 79% rename from survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/user/CaptchaVo.java rename to survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/vo/CaptchaVo.java index 6cbcd30c..9ffb53f5 100644 --- a/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/user/CaptchaVo.java +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/vo/CaptchaVo.java @@ -1,4 +1,4 @@ -package com.xiaojusurvey.engine.common.entity.user; +package com.xiaojusurvey.engine.core.auth.vo; import lombok.AllArgsConstructor; import lombok.Data; @@ -12,7 +12,7 @@ import lombok.NoArgsConstructor; @NoArgsConstructor @AllArgsConstructor @Data -public class CaptchaVo{ +public class CaptchaVo { private String id; diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/domain/UserVo.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/vo/UserVo.java similarity index 69% rename from survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/domain/UserVo.java rename to survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/vo/UserVo.java index e39b6c5d..023fb3d6 100644 --- a/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/domain/UserVo.java +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/auth/vo/UserVo.java @@ -1,4 +1,4 @@ -package com.xiaojusurvey.engine.core.auth.domain; +package com.xiaojusurvey.engine.core.auth.vo; import lombok.Data; @@ -8,7 +8,7 @@ import lombok.Data; * @Description: 用户vo */ @Data -public class UserVo{ +public class UserVo { private String token; private String username; diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/user/impl/UserServiceImpl.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/user/impl/UserServiceImpl.java index 59e44ce0..011846e4 100644 --- a/survey-core/src/main/java/com/xiaojusurvey/engine/core/user/impl/UserServiceImpl.java +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/user/impl/UserServiceImpl.java @@ -27,6 +27,7 @@ public class UserServiceImpl implements UserService { /** * 查询用户 + * * @param username * @param password * @return @@ -38,7 +39,7 @@ public class UserServiceImpl implements UserService { query.addCriteria(Criteria.where("username").is(username).and("password").is(encryptPassword)); //查询用户并返回 User user = mongoRepository.findOne(query, User.class); - if (ObjectUtils.isEmpty(user)){ + if (ObjectUtils.isEmpty(user)) { throw new ServiceException(RespErrorCode.USER_PASSWORD_ERROR.getMessage(), RespErrorCode.USER_PASSWORD_ERROR.getCode()); } return user; diff --git a/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/enums/ErrorEnum.java b/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/enums/ErrorEnum.java index 685b73b1..f7eafa6a 100644 --- a/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/enums/ErrorEnum.java +++ b/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/enums/ErrorEnum.java @@ -3,7 +3,7 @@ package com.xiaojusurvey.engine.repository.enums; public enum ErrorEnum { - PARAM_NULL_ERROR("%s is null",1); + PARAM_NULL_ERROR("%s is null", 1); private String errorMsg; diff --git a/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/impl/MongoRepositoryImpl.java b/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/impl/MongoRepositoryImpl.java index ef21a677..a39fa179 100644 --- a/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/impl/MongoRepositoryImpl.java +++ b/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/impl/MongoRepositoryImpl.java @@ -42,7 +42,7 @@ public class MongoRepositoryImpl implements MongoRepository { @Override public T findOne(Query query, Class entityClass) { - return mongoTemplate.findOne(query,entityClass); + return mongoTemplate.findOne(query, entityClass); } @Override diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/message/impl/TestMessageProcessor.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/message/impl/TestMessageProcessor.java index 22da8d96..5cbff9c5 100644 --- a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/message/impl/TestMessageProcessor.java +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/message/impl/TestMessageProcessor.java @@ -9,6 +9,7 @@ import org.springframework.stereotype.Component; @Component public class TestMessageProcessor implements MessageProcessor { + private static final int ORDER = 99; @Override public void before(Invocation invocation) { @@ -19,4 +20,9 @@ public class TestMessageProcessor implements MessageProcessor { public Result after(Result result) { return new Result(); } + + @Override + public int getOrder() { + return ORDER; + } } diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/ExtensionProcessor.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/ExtensionProcessor.java index 09228dee..b10ef411 100644 --- a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/ExtensionProcessor.java +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/ExtensionProcessor.java @@ -5,4 +5,6 @@ public interface ExtensionProcessor { void before(Invocation invocation); Result after(Result result); + + int getOrder(); } diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/aop/ExtensionAspect.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/aop/ExtensionAspect.java index 6245967a..5a5fe691 100644 --- a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/aop/ExtensionAspect.java +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/aop/ExtensionAspect.java @@ -1,33 +1,33 @@ package com.xiaojusurvey.engine.extensions.processor.aop; - import com.xiaojusurvey.engine.extensions.processor.Invocation; import com.xiaojusurvey.engine.extensions.processor.Result; -import com.xiaojusurvey.engine.extensions.processor.annotation.Message; -import com.xiaojusurvey.engine.extensions.processor.annotation.Security; -import com.xiaojusurvey.engine.extensions.router.MessageProcessorRouter; -import com.xiaojusurvey.engine.extensions.router.SecurityProcessorRouter; +import com.xiaojusurvey.engine.extensions.router.BaseRouter; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; import java.lang.reflect.Method; -import java.util.Objects; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; @Aspect @Component -public class ExtensionAspect { +public class ExtensionAspect implements InitializingBean { @Autowired - private MessageProcessorRouter messageProcessorRouter; + private List routerList; + + private Map routerMap = new LinkedHashMap<>(); - @Autowired - private SecurityProcessorRouter securityProcessorRouter; @Pointcut("@annotation(com.xiaojusurvey.engine.extensions.processor.annotation.Message) || @annotation(com.xiaojusurvey.engine.extensions.processor.annotation.Security)") public void pointcut() { @@ -37,24 +37,25 @@ public class ExtensionAspect { public Object doAround(ProceedingJoinPoint pjp) throws Throwable { MethodSignature signature = (MethodSignature) pjp.getSignature(); Method method = signature.getMethod(); - Message message = method.getAnnotation(Message.class); - Security security = method.getAnnotation(Security.class); - if (!Objects.isNull(message)) { - Invocation invocation = new Invocation(method.getName(), method.getParameterTypes(), pjp.getArgs()); - messageProcessorRouter.before(invocation); - } - if (!Objects.isNull(security)) { - Invocation invocation = new Invocation(method.getName(), method.getParameterTypes(), pjp.getArgs()); - securityProcessorRouter.before(invocation); + for (Map.Entry routerEntry : routerMap.entrySet()) { + if (null != method.getAnnotation(routerEntry.getKey())) { + Invocation invocation = new Invocation(method.getName(), method.getParameterTypes(), pjp.getArgs()); + routerEntry.getValue().before(invocation); + } } Result result = new Result(pjp.proceed(pjp.getArgs())); - if (!Objects.isNull(message)) { - result = messageProcessorRouter.after(result); - } - if (!Objects.isNull(security)) { - result = securityProcessorRouter.after(result); + for (Map.Entry routerEntry : routerMap.entrySet()) { + if (null != method.getAnnotation(routerEntry.getKey())) { + result = routerEntry.getValue().after(result); + } } return result.getValue(); } + @Override + public void afterPropertiesSet() throws Exception { + if (!CollectionUtils.isEmpty(routerList)) { + routerList.forEach(e -> routerMap.put(e.annotationClass(), e)); + } + } } diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/BaseRouter.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/BaseRouter.java index 90f913b7..369f8eee 100644 --- a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/BaseRouter.java +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/BaseRouter.java @@ -1,18 +1,48 @@ package com.xiaojusurvey.engine.extensions.router; +import com.xiaojusurvey.engine.extensions.processor.ExtensionProcessor; import com.xiaojusurvey.engine.extensions.processor.Invocation; import com.xiaojusurvey.engine.extensions.processor.Result; import org.springframework.beans.BeansException; +import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; +import org.springframework.util.CollectionUtils; -public abstract class BaseRouter implements ApplicationContextAware { +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Map; + + +public abstract class BaseRouter implements ApplicationContextAware, InitializingBean { protected ApplicationContext applicationContext; - public abstract void before(Invocation invocation); + protected List extensionProcessors = new ArrayList<>(); - public abstract Result after(Result result); + public void before(Invocation invocation) { + for (T extensionProcessor : extensionProcessors) { + extensionProcessor.before(invocation); + } + } + + public Result after(Result result) { + for (T extensionProcessor : extensionProcessors) { + result = extensionProcessor.after(result); + } + return result; + } + + public abstract Class annotationClass(); + + protected void initExtensionProcessors(Class clazz) { + Map messageProcessorMap = applicationContext.getBeansOfType(clazz); + if (!CollectionUtils.isEmpty(messageProcessorMap)) { + messageProcessorMap.entrySet().stream().forEach(e -> extensionProcessors.add(e.getValue())); + extensionProcessors.sort(Comparator.comparing(ExtensionProcessor::getOrder)); + } + } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/MessageProcessorRouter.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/MessageProcessorRouter.java index 8809f96e..16449eda 100644 --- a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/MessageProcessorRouter.java +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/MessageProcessorRouter.java @@ -1,42 +1,21 @@ package com.xiaojusurvey.engine.extensions.router; import com.xiaojusurvey.engine.extensions.message.MessageProcessor; -import com.xiaojusurvey.engine.extensions.processor.Invocation; -import com.xiaojusurvey.engine.extensions.processor.Result; -import org.springframework.beans.factory.InitializingBean; +import com.xiaojusurvey.engine.extensions.processor.annotation.Message; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; -import org.springframework.util.CollectionUtils; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; @Component -public class MessageProcessorRouter extends BaseRouter implements InitializingBean { - - private List messageProcessorList = new ArrayList<>(); - +@Order(0) +public class MessageProcessorRouter extends BaseRouter { @Override public void afterPropertiesSet() throws Exception { - Map messageProcessorMap = applicationContext.getBeansOfType(MessageProcessor.class); - if (!CollectionUtils.isEmpty(messageProcessorMap)) { - messageProcessorMap.entrySet().stream().forEach(e -> messageProcessorList.add(e.getValue())); - } + initExtensionProcessors(MessageProcessor.class); } @Override - public void before(Invocation invocation) { - for (MessageProcessor messageProcessor : messageProcessorList) { - messageProcessor.before(invocation); - } - } - - @Override - public Result after(Result result) { - for (MessageProcessor messageProcessor : messageProcessorList) { - result = messageProcessor.after(result); - } - return result; + public Class annotationClass() { + return Message.class; } } diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/SecurityProcessorRouter.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/SecurityProcessorRouter.java index a678f018..c4753a44 100644 --- a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/SecurityProcessorRouter.java +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/SecurityProcessorRouter.java @@ -1,41 +1,21 @@ package com.xiaojusurvey.engine.extensions.router; -import com.xiaojusurvey.engine.extensions.processor.Invocation; -import com.xiaojusurvey.engine.extensions.processor.Result; +import com.xiaojusurvey.engine.extensions.processor.annotation.Security; import com.xiaojusurvey.engine.extensions.security.SecurityProcessor; -import org.springframework.beans.factory.InitializingBean; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; -import org.springframework.util.CollectionUtils; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; @Component -public class SecurityProcessorRouter extends BaseRouter implements InitializingBean { - - private List securityProcessorList = new ArrayList<>(); +@Order(1) +public class SecurityProcessorRouter extends BaseRouter { @Override public void afterPropertiesSet() throws Exception { - Map messageProcessorMap = applicationContext.getBeansOfType(SecurityProcessor.class); - if (!CollectionUtils.isEmpty(messageProcessorMap)) { - messageProcessorMap.entrySet().stream().forEach(e -> securityProcessorList.add(e.getValue())); - } + initExtensionProcessors(SecurityProcessor.class); } @Override - public void before(Invocation invocation) { - for (SecurityProcessor securityProcessor : securityProcessorList) { - securityProcessor.before(invocation); - } - } - - @Override - public Result after(Result result) { - for (SecurityProcessor securityProcessor : securityProcessorList) { - result= securityProcessor.after(result); - } - return result; + public Class annotationClass() { + return Security.class; } } diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/security/impl/TestSecurityProcessor.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/security/impl/TestSecurityProcessor.java index 4adadf09..ac03bf65 100644 --- a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/security/impl/TestSecurityProcessor.java +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/security/impl/TestSecurityProcessor.java @@ -8,6 +8,7 @@ import org.springframework.stereotype.Component; @Component public class TestSecurityProcessor implements SecurityProcessor { + private static final int ORDER = 100; @Override public void before(Invocation invocation) { @@ -18,4 +19,9 @@ public class TestSecurityProcessor implements SecurityProcessor { public Result after(Result result) { return new Result(); } + + @Override + public int getOrder() { + return ORDER; + } } diff --git a/survey-server/pom.xml b/survey-server/pom.xml index f6737668..4e25905b 100644 --- a/survey-server/pom.xml +++ b/survey-server/pom.xml @@ -35,21 +35,16 @@ com.alibaba fastjson - ${fastjson.version} org.apache.commons commons-lang3 - ${lang3.version} - - junit junit test - diff --git a/survey-server/src/main/java/com/xiaojusurvey/engine/aop/ValidatedExceptionHandler.java b/survey-server/src/main/java/com/xiaojusurvey/engine/aop/ValidatedExceptionHandler.java index fe0b9749..a71a4551 100644 --- a/survey-server/src/main/java/com/xiaojusurvey/engine/aop/ValidatedExceptionHandler.java +++ b/survey-server/src/main/java/com/xiaojusurvey/engine/aop/ValidatedExceptionHandler.java @@ -11,20 +11,21 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; /** * 全局异常处理器 + * * @Author: LYF * @CreateTime: 2024-06-05 * @Description: 全局异常处理器 */ @RestControllerAdvice public class ValidatedExceptionHandler { - private static final Logger log = LoggerFactory.getLogger(ValidatedExceptionHandler.class); + private static final Logger LOG = LoggerFactory.getLogger(ValidatedExceptionHandler.class); /** * 自定义验证异常 */ @ExceptionHandler(BindException.class) public RpcResult handleBindException(BindException e) { - log.error(e.getMessage(), e); + LOG.error(e.getMessage(), e); String message = e.getAllErrors().get(0).getDefaultMessage(); return RpcResultUtil.createFailedResult(RespErrorCode.PARAMETER_ERROR.getCode(), message); } diff --git a/survey-server/src/main/java/com/xiaojusurvey/engine/controller/AuthController.java b/survey-server/src/main/java/com/xiaojusurvey/engine/controller/AuthController.java index bad509c5..53453200 100644 --- a/survey-server/src/main/java/com/xiaojusurvey/engine/controller/AuthController.java +++ b/survey-server/src/main/java/com/xiaojusurvey/engine/controller/AuthController.java @@ -1,8 +1,8 @@ package com.xiaojusurvey.engine.controller; -import com.xiaojusurvey.engine.common.entity.user.CaptchaVo; -import com.xiaojusurvey.engine.core.auth.domain.UserParam; -import com.xiaojusurvey.engine.core.auth.domain.UserVo; +import com.xiaojusurvey.engine.core.auth.vo.CaptchaVo; +import com.xiaojusurvey.engine.core.auth.param.UserParam; +import com.xiaojusurvey.engine.core.auth.vo.UserVo; import com.xiaojusurvey.engine.common.rpc.RpcResult; import com.xiaojusurvey.engine.common.util.RpcResultUtil; import com.xiaojusurvey.engine.core.auth.AuthService; diff --git a/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/LogFilter.java b/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/LogFilter.java deleted file mode 100644 index b15aecea..00000000 --- a/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/LogFilter.java +++ /dev/null @@ -1,138 +0,0 @@ -package com.xiaojusurvey.engine.interceptor; - -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; -import org.springframework.stereotype.Component; -import org.springframework.util.StopWatch; -import org.springframework.web.filter.OncePerRequestFilter; -import org.springframework.web.util.ContentCachingRequestWrapper; -import org.springframework.web.util.ContentCachingResponseWrapper; -import org.springframework.web.util.WebUtils; - -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; -import java.util.Objects; -import java.util.UUID; - -@Component -public class LogFilter extends OncePerRequestFilter { - private final Logger logger = LoggerFactory.getLogger(getClass()); - - public static final String TRACE_ID = "traceId"; - public static final int SLOW_TIME_MILLIS = 5000; - public static final int LOG_MAX_LENGTH = 1024; - - @Override - protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { - String url = null; - //计时器 - StopWatch watch = null; - - try { - // 获取url - url = getCompleteUrl(request); - // 设置traceId - String traceId = request.getHeader(TRACE_ID); - if (StringUtils.isBlank(traceId)) { - traceId = UUID.randomUUID().toString(); - } - MDC.put(TRACE_ID, traceId); - - request = new ContentCachingRequestWrapper(request); - response = new ContentCachingResponseWrapper(response); - - watch = new StopWatch(); - watch.start(); - - logger.info("Start request, ip:{} host:{} url:{} body:{}", getRemoteIP(request), - request.getRemoteHost(), url, getRequestBody(request)); - filterChain.doFilter(request, response); - - } finally { - if (watch != null) { - watch.stop(); - if (watch.getTotalTimeMillis() > SLOW_TIME_MILLIS && !url.endsWith(".js")) { - logger.error("End slow request, ip:{} host:{} url:{} cost:{} ms resp:{}", getRemoteIP(request), - request.getRemoteHost(), url, watch.getTotalTimeMillis(), getResponseBody(response)); - } else { - logger.info("End request, ip:{} host:{} url:{} cost:{} ms resp:{}", getRemoteIP(request), - request.getRemoteHost(), url, watch.getTotalTimeMillis(), getResponseBody(response)); - } - } - MDC.clear(); - updateResponse(response); - } - } - - - private String getRequestBody(HttpServletRequest request) { - ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class); - if (wrapper == null) { - return ""; - } - return getBody(wrapper.getContentAsByteArray(), wrapper.getCharacterEncoding()); - } - - /**获取body 避免过长 - * - * @param contentAsByteArray 请求体或响应体 - * @param characterEncoding 字符集 - * @return - */ - private String getBody(byte[] contentAsByteArray, String characterEncoding) { - if (contentAsByteArray == null) return null; - String body = ""; - try { - body = new String(contentAsByteArray, characterEncoding); - // 最多打印1024字符 - if (body.length() > LOG_MAX_LENGTH) { - body = body.substring(0, LOG_MAX_LENGTH) + "..."; - } - } catch (Exception e) { - // - } - return body; - } - - private String getResponseBody(HttpServletResponse response) { - ContentCachingResponseWrapper responseWrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class); - if (responseWrapper == null) { - return ""; - } - return getBody(responseWrapper.getContentAsByteArray(), "UTF-8"); - } - - private void updateResponse(HttpServletResponse response) throws IOException { - ContentCachingResponseWrapper responseWrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class); - Objects.requireNonNull(responseWrapper).copyBodyToResponse(); - } - - private String getCompleteUrl(HttpServletRequest request) { - String url = request.getRequestURL().toString(); - String queryString = ""; - if (StringUtils.isNotEmpty(request.getQueryString())) { - try { - queryString = URLDecoder.decode(request.getQueryString(), "UTF-8"); - } catch (UnsupportedEncodingException e) { - logger.error("URLDecoder.decode error:" + e.getMessage(), e); - } - url = url + "?" + queryString; - } - return url; - } - - - private String getRemoteIP(HttpServletRequest request) { - if (request.getHeader("x-forwarded-for") == null) { - return request.getRemoteAddr(); - } - return request.getHeader("x-forwarded-for"); - } -} diff --git a/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/SurveyWebMvcConfigurer.java b/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/SurveyWebMvcConfigurer.java index 181daebd..86642998 100644 --- a/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/SurveyWebMvcConfigurer.java +++ b/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/SurveyWebMvcConfigurer.java @@ -8,9 +8,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class SurveyWebMvcConfigurer implements WebMvcConfigurer { - @Bean - public LoginInterceptor loginInterceptor(){ + public LoginInterceptor loginInterceptor() { return new LoginInterceptor(); } diff --git a/survey-server/src/main/resources/application-dev.properties b/survey-server/src/main/resources/application-dev.properties index 97d4926f..632d27bc 100644 --- a/survey-server/src/main/resources/application-dev.properties +++ b/survey-server/src/main/resources/application-dev.properties @@ -1,4 +1,11 @@ server.port=8080 spring.data.mongodb.uri= -spring.data.mongodb.database= +#spring.data.mongodb.database= + +spring.data.mongodb.host=localhost +spring.data.mongodb.port=27017 +spring.data.mongodb.database=testdb + +XIAOJU_SURVEY_JWT_EXPIRES_IN=8 +XIAOJU_SURVEY_JWT_SECRET=xiaojuSurveyJwtSecret diff --git a/survey-server/src/main/resources/application-prod.properties b/survey-server/src/main/resources/application-prod.properties index eba23834..ea3f417d 100644 --- a/survey-server/src/main/resources/application-prod.properties +++ b/survey-server/src/main/resources/application-prod.properties @@ -1,4 +1,7 @@ server.port=8080 spring.data.mongodb.uri= -spring.data.mongodb.database= \ No newline at end of file +spring.data.mongodb.database= + +XIAOJU_SURVEY_JWT_EXPIRES_IN=8 +XIAOJU_SURVEY_JWT_SECRET=xiaojuSurveyJwtSecret diff --git a/survey-server/src/main/resources/application-test.properties b/survey-server/src/main/resources/application-test.properties index a3ac65ce..ea3f417d 100644 --- a/survey-server/src/main/resources/application-test.properties +++ b/survey-server/src/main/resources/application-test.properties @@ -1 +1,7 @@ -server.port=8080 \ No newline at end of file +server.port=8080 + +spring.data.mongodb.uri= +spring.data.mongodb.database= + +XIAOJU_SURVEY_JWT_EXPIRES_IN=8 +XIAOJU_SURVEY_JWT_SECRET=xiaojuSurveyJwtSecret diff --git a/survey-server/src/main/resources/application.properties b/survey-server/src/main/resources/application.properties index 404bb3bd..e269fdef 100644 --- a/survey-server/src/main/resources/application.properties +++ b/survey-server/src/main/resources/application.properties @@ -1,6 +1 @@ spring.profiles.active=@spring.profiles.active@ - - -#?? -XIAOJU_SURVEY_JWT_EXPIRES_IN=8 -XIAOJU_SURVEY_JWT_SECRET=xiaojuSurveyJwtSecret \ No newline at end of file diff --git a/survey-server/src/test/java/com/xiaojusurvey/engine/controller/AuthTest.java b/survey-server/src/test/java/com/xiaojusurvey/engine/controller/AuthTest.java index f4ea229c..72cc5324 100644 --- a/survey-server/src/test/java/com/xiaojusurvey/engine/controller/AuthTest.java +++ b/survey-server/src/test/java/com/xiaojusurvey/engine/controller/AuthTest.java @@ -1,10 +1,10 @@ package com.xiaojusurvey.engine.controller; import com.xiaojusurvey.engine.SurveyApplication; -import com.xiaojusurvey.engine.common.entity.user.CaptchaVo; +import com.xiaojusurvey.engine.core.auth.vo.CaptchaVo; import com.xiaojusurvey.engine.common.rpc.RpcResult; -import com.xiaojusurvey.engine.core.auth.domain.UserParam; -import com.xiaojusurvey.engine.core.auth.domain.UserVo; +import com.xiaojusurvey.engine.core.auth.param.UserParam; +import com.xiaojusurvey.engine.core.auth.vo.UserVo; import com.xiaojusurvey.engine.core.auth.util.JwtTokenUtil; import org.junit.Test; import org.junit.runner.RunWith; @@ -23,8 +23,6 @@ public class AuthTest { @Resource private JwtTokenUtil jwtTokenUtil; - - @Resource AuthController authController; @@ -59,5 +57,4 @@ public class AuthTest { } - }