diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/AuthenticationException.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/AuthenticationException.java index 767166d5e..f5079ccf9 100644 --- a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/AuthenticationException.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/AuthenticationException.java @@ -27,4 +27,26 @@ public class AuthenticationException extends I18nSupportException { super(message, cause); this.code = code; } + + /** + * 不填充线程栈的异常,在一些对线程栈不敏感,且对异常不可控(如: 来自未认证请求产生的异常)的情况下不填充线程栈对性能有利。 + */ + public static class NoStackTrace extends AuthenticationException { + public NoStackTrace(String code) { + super(code); + } + + public NoStackTrace(String code, String message) { + super(code, message); + } + + public NoStackTrace(String code, String message, Throwable cause) { + super(code, message, cause); + } + + @Override + public final synchronized Throwable fillInStackTrace() { + return this; + } + } } diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/UnAuthorizedException.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/UnAuthorizedException.java index 75dd34fd6..e2bf61fe5 100644 --- a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/UnAuthorizedException.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/UnAuthorizedException.java @@ -18,6 +18,7 @@ package org.hswebframework.web.authorization.exception; +import lombok.Getter; import org.hswebframework.web.authorization.token.TokenState; import org.hswebframework.web.exception.I18nSupportException; import org.springframework.http.HttpStatus; @@ -29,6 +30,7 @@ import org.springframework.web.bind.annotation.ResponseStatus; * @author zhouhao * @since 3.0 */ +@Getter @ResponseStatus(HttpStatus.UNAUTHORIZED) public class UnAuthorizedException extends I18nSupportException { private static final long serialVersionUID = 2422918455013900645L; @@ -53,7 +55,29 @@ public class UnAuthorizedException extends I18nSupportException { this.state = state; } - public TokenState getState() { - return state; + /** + * 不填充线程栈的异常,在一些对线程栈不敏感,且对异常不可控(如: 来自未认证请求产生的异常)的情况下不填充线程栈对性能有利。 + */ + public static class NoStackTrace extends UnAuthorizedException { + public NoStackTrace() { + super(); + } + + public NoStackTrace(TokenState state) { + super(state); + } + + public NoStackTrace(String message, TokenState state) { + super(message, state); + } + + public NoStackTrace(String message, TokenState state, Throwable cause) { + super(message, state, cause); + } + + @Override + public final synchronized Throwable fillInStackTrace() { + return this; + } } } diff --git a/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/web/AuthorizationController.java b/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/web/AuthorizationController.java index 3d7351e3a..8ac82b20b 100644 --- a/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/web/AuthorizationController.java +++ b/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/web/AuthorizationController.java @@ -131,12 +131,12 @@ public class AuthorizationController { } else { authenticationMono = ReactiveAuthenticationHolder .get(event.getUserId()) - .switchIfEmpty(Mono.error(() -> new AuthenticationException(AuthenticationException.USER_DISABLED))); + .switchIfEmpty(Mono.error(() -> new AuthenticationException.NoStackTrace(AuthenticationException.USER_DISABLED))); } } else { authenticationMono = authenticationManager .authenticate(Mono.just(new PlainTextUsernamePasswordAuthenticationRequest(event.getUsername(), event.getPassword()))) - .switchIfEmpty(Mono.error(() -> new AuthenticationException(AuthenticationException.ILLEGAL_PASSWORD))); + .switchIfEmpty(Mono.error(() -> new AuthenticationException.NoStackTrace(AuthenticationException.ILLEGAL_PASSWORD))); } return authenticationMono; } diff --git a/hsweb-authorization/hsweb-authorization-oauth2/src/main/java/org/hswebframework/web/oauth2/OAuth2Exception.java b/hsweb-authorization/hsweb-authorization-oauth2/src/main/java/org/hswebframework/web/oauth2/OAuth2Exception.java index 41d8a62a5..8a87b07b6 100644 --- a/hsweb-authorization/hsweb-authorization-oauth2/src/main/java/org/hswebframework/web/oauth2/OAuth2Exception.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/src/main/java/org/hswebframework/web/oauth2/OAuth2Exception.java @@ -2,6 +2,7 @@ package org.hswebframework.web.oauth2; import lombok.Getter; import org.hswebframework.web.exception.BusinessException; +import org.hswebframework.web.exception.I18nSupportException; @Getter public class OAuth2Exception extends BusinessException { @@ -16,4 +17,22 @@ public class OAuth2Exception extends BusinessException { super(message, cause); this.type = type; } + + /** + * 不填充线程栈的异常,在一些对线程栈不敏感,且对异常不可控(如: 来自未认证请求产生的异常)的情况下不填充线程栈对性能有利。 + */ + public static class NoStackTrace extends OAuth2Exception { + public NoStackTrace(ErrorType type) { + super(type); + } + + public NoStackTrace(String message, Throwable cause, ErrorType type) { + super(message, cause, type); + } + + @Override + public final synchronized Throwable fillInStackTrace() { + return this; + } + } } diff --git a/hsweb-authorization/hsweb-authorization-oauth2/src/main/java/org/hswebframework/web/oauth2/server/OAuth2Client.java b/hsweb-authorization/hsweb-authorization-oauth2/src/main/java/org/hswebframework/web/oauth2/server/OAuth2Client.java index d231171a5..c3205c28c 100644 --- a/hsweb-authorization/hsweb-authorization-oauth2/src/main/java/org/hswebframework/web/oauth2/server/OAuth2Client.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/src/main/java/org/hswebframework/web/oauth2/server/OAuth2Client.java @@ -4,6 +4,7 @@ import lombok.Getter; import lombok.Setter; import org.hswebframework.web.oauth2.ErrorType; import org.hswebframework.web.oauth2.OAuth2Exception; +import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import javax.validation.constraints.NotBlank; @@ -30,13 +31,13 @@ public class OAuth2Client { private String userId; public void validateRedirectUri(String redirectUri) { - if (StringUtils.isEmpty(redirectUri) || (!redirectUri.startsWith(this.redirectUrl))) { + if (ObjectUtils.isEmpty(redirectUri) || (!redirectUri.startsWith(this.redirectUrl))) { throw new OAuth2Exception(ErrorType.ILLEGAL_REDIRECT_URI); } } public void validateSecret(String secret) { - if (StringUtils.isEmpty(secret) || (!secret.equals(this.clientSecret))) { + if (ObjectUtils.isEmpty(secret) || (!secret.equals(this.clientSecret))) { throw new OAuth2Exception(ErrorType.ILLEGAL_CLIENT_SECRET); } } diff --git a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/service/ReactiveTreeSortEntityService.java b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/service/ReactiveTreeSortEntityService.java index 3003bee8b..6e4c6742c 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/service/ReactiveTreeSortEntityService.java +++ b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/service/ReactiveTreeSortEntityService.java @@ -201,7 +201,7 @@ public interface ReactiveTreeSortEntityService { if (Objects.equals(ele.getParentId(), e.getId())) { - throw new ValidationException("parentId", "error.tree_entity_cyclic_dependency"); + throw new ValidationException.NoStackTrace("parentId", "error.tree_entity_cyclic_dependency"); } }) .then(Mono.just(ele)); diff --git a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/QueryController.java b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/QueryController.java index 98ec2cc13..5bb2b4f3a 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/QueryController.java +++ b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/QueryController.java @@ -169,7 +169,7 @@ public interface QueryController { default E getById(@PathVariable K id) { return getRepository() .findById(id) - .orElseThrow(NotFoundException::new); + .orElseThrow(NotFoundException.NoStackTrace::new); } } diff --git a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveDeleteController.java b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveDeleteController.java index 9bfa0672c..75f06da11 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveDeleteController.java +++ b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveDeleteController.java @@ -19,7 +19,7 @@ public interface ReactiveDeleteController { default Mono delete(@PathVariable K id) { return getRepository() .findById(Mono.just(id)) - .switchIfEmpty(Mono.error(NotFoundException::new)) + .switchIfEmpty(Mono.error(NotFoundException.NoStackTrace::new)) .flatMap(e -> getRepository() .deleteById(Mono.just(id)) .thenReturn(e)); diff --git a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveQueryController.java b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveQueryController.java index b568cad64..6f9e2b5cc 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveQueryController.java +++ b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveQueryController.java @@ -160,7 +160,7 @@ public interface ReactiveQueryController { default Mono getById(@PathVariable K id) { return getRepository() .findById(Mono.just(id)) - .switchIfEmpty(Mono.error(NotFoundException::new)); + .switchIfEmpty(Mono.error(NotFoundException.NoStackTrace::new)); } } diff --git a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveServiceDeleteController.java b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveServiceDeleteController.java index 7908d9659..ba3074f0e 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveServiceDeleteController.java +++ b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveServiceDeleteController.java @@ -19,7 +19,7 @@ public interface ReactiveServiceDeleteController { default Mono delete(@PathVariable K id) { return getService() .findById(Mono.just(id)) - .switchIfEmpty(Mono.error(NotFoundException::new)) + .switchIfEmpty(Mono.error(NotFoundException.NoStackTrace::new)) .flatMap(e -> getService() .deleteById(Mono.just(id)) .thenReturn(e)); diff --git a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveServiceQueryController.java b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveServiceQueryController.java index d798dacad..ad641ba18 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveServiceQueryController.java +++ b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveServiceQueryController.java @@ -240,7 +240,7 @@ public interface ReactiveServiceQueryController { default Mono getById(@PathVariable K id) { return getService() .findById(id) - .switchIfEmpty(Mono.error(NotFoundException::new)); + .switchIfEmpty(Mono.error(NotFoundException.NoStackTrace::new)); } } diff --git a/hsweb-core/src/main/java/org/hswebframework/web/exception/BusinessException.java b/hsweb-core/src/main/java/org/hswebframework/web/exception/BusinessException.java index 16a09d7a7..a74b541de 100644 --- a/hsweb-core/src/main/java/org/hswebframework/web/exception/BusinessException.java +++ b/hsweb-core/src/main/java/org/hswebframework/web/exception/BusinessException.java @@ -26,12 +26,11 @@ import lombok.Getter; * @author zhouhao * @since 2.0 */ +@Getter public class BusinessException extends I18nSupportException { private static final long serialVersionUID = 5441923856899380112L; - @Getter private int status = 500; - @Getter private String code; public BusinessException(String message) { @@ -62,4 +61,40 @@ public class BusinessException extends I18nSupportException { super(message, cause); this.status = status; } + + /** + * 不填充线程栈的异常,在一些对线程栈不敏感,且对异常不可控(如: 来自未认证请求产生的异常)的情况下不填充线程栈对性能有利。 + */ + public static class NoStackTrace extends BusinessException { + public NoStackTrace(String message) { + this(message, 500); + } + + public NoStackTrace(String message, int status, Object... args) { + this(message, null, status, args); + } + + public NoStackTrace(String message, String code) { + this(message, code, 500); + } + + public NoStackTrace(String message, String code, int status, Object... args) { + super(message, code, status, args); + + } + + public NoStackTrace(String message, Throwable cause) { + super(message, cause); + } + + public NoStackTrace(String message, Throwable cause, int status) { + super(message, cause, status); + } + + + @Override + public final synchronized Throwable fillInStackTrace() { + return this; + } + } } diff --git a/hsweb-core/src/main/java/org/hswebframework/web/exception/I18nSupportException.java b/hsweb-core/src/main/java/org/hswebframework/web/exception/I18nSupportException.java index c5cada337..9a5e8a787 100644 --- a/hsweb-core/src/main/java/org/hswebframework/web/exception/I18nSupportException.java +++ b/hsweb-core/src/main/java/org/hswebframework/web/exception/I18nSupportException.java @@ -9,7 +9,6 @@ import org.springframework.util.StringUtils; import reactor.core.publisher.Mono; import java.util.Locale; -import java.util.Objects; /** * 支持国际化消息的异常,code为 @@ -68,8 +67,8 @@ public class I18nSupportException extends TraceSourceException { public final Mono getLocalizedMessageReactive() { return LocaleUtils - .currentReactive() - .map(this::getLocalizedMessage); + .currentReactive() + .map(this::getLocalizedMessage); } public static String tryGetLocalizedMessage(Throwable error, Locale locale) { @@ -93,7 +92,25 @@ public class I18nSupportException extends TraceSourceException { public static Mono tryGetLocalizedMessageReactive(Throwable error) { return LocaleUtils - .currentReactive() - .map(locale -> tryGetLocalizedMessage(error, locale)); + .currentReactive() + .map(locale -> tryGetLocalizedMessage(error, locale)); + } + + /** + * 不填充线程栈的异常,在一些对线程栈不敏感,且对异常不可控(如: 来自未认证请求产生的异常)的情况下不填充线程栈对性能有利。 + */ + public static class NoStackTrace extends I18nSupportException { + public NoStackTrace(String code, Object... args) { + super(code, args); + } + + public NoStackTrace(String code, Throwable cause, Object... args) { + super(code, cause, args); + } + + @Override + public final synchronized Throwable fillInStackTrace() { + return this; + } } } diff --git a/hsweb-core/src/main/java/org/hswebframework/web/exception/NotFoundException.java b/hsweb-core/src/main/java/org/hswebframework/web/exception/NotFoundException.java index b005303e4..bcfabe713 100644 --- a/hsweb-core/src/main/java/org/hswebframework/web/exception/NotFoundException.java +++ b/hsweb-core/src/main/java/org/hswebframework/web/exception/NotFoundException.java @@ -31,4 +31,22 @@ public class NotFoundException extends BusinessException { public NotFoundException() { this("error.not_found"); } + + /** + * 不填充线程栈的异常,在一些对线程栈不敏感,且对异常不可控(如: 来自未认证请求产生的异常)的情况下不填充线程栈对性能有利。 + */ + public static class NoStackTrace extends NotFoundException { + public NoStackTrace(String code, Object... args) { + super(code, args); + } + + public NoStackTrace() { + super(); + } + + @Override + public final synchronized Throwable fillInStackTrace() { + return this; + } + } } diff --git a/hsweb-core/src/main/java/org/hswebframework/web/exception/ValidationException.java b/hsweb-core/src/main/java/org/hswebframework/web/exception/ValidationException.java index 97d61f2c7..370b974bc 100644 --- a/hsweb-core/src/main/java/org/hswebframework/web/exception/ValidationException.java +++ b/hsweb-core/src/main/java/org/hswebframework/web/exception/ValidationException.java @@ -49,8 +49,8 @@ public class ValidationException extends I18nSupportException { //{0} 属性 ,{1} 验证消息 //property也支持国际化? String propertyI18n = propertyI18nEnabled ? - first.getRootBeanClass().getName() + "." + property - : property; + first.getRootBeanClass().getName() + "." + property + : property; setArgs(new Object[]{propertyI18n, first.getMessage()}); @@ -64,11 +64,11 @@ public class ValidationException extends I18nSupportException { public List getDetails(Locale locale) { return CollectionUtils.isEmpty(details) - ? Collections.emptyList() - : details - .stream() - .map(detail -> detail.translateI18n(locale)) - .collect(Collectors.toList()); + ? Collections.emptyList() + : details + .stream() + .map(detail -> detail.translateI18n(locale)) + .collect(Collectors.toList()); } @Override @@ -102,4 +102,31 @@ public class ValidationException extends I18nSupportException { return this; } } + + /** + * 不填充线程栈的异常,在一些对线程栈不敏感,且对异常不可控(如: 来自未认证请求产生的异常)的情况下不填充线程栈对性能有利。 + */ + public static class NoStackTrace extends ValidationException { + public NoStackTrace(String message) { + super(message); + } + + public NoStackTrace(String property, String message, Object... args) { + super(property, message, args); + } + + public NoStackTrace(String message, List details, Object... args) { + super(message, details, args); + + } + + public NoStackTrace(Set> violations) { + super(violations); + } + + @Override + public final synchronized Throwable fillInStackTrace() { + return this; + } + } } diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultReactiveAuthenticationManager.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultReactiveAuthenticationManager.java index 20f134621..9d5d64955 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultReactiveAuthenticationManager.java +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultReactiveAuthenticationManager.java @@ -58,7 +58,6 @@ public class DefaultReactiveAuthenticationManager implements ReactiveAuthenticat public Mono authenticate(Mono request) { return request .filter(PlainTextUsernamePasswordAuthenticationRequest.class::isInstance) - .switchIfEmpty(Mono.error(() -> new UnsupportedOperationException("不支持的请求类型"))) .map(PlainTextUsernamePasswordAuthenticationRequest.class::cast) .flatMap(pwdRequest -> reactiveUserService.findByUsernameAndPassword(pwdRequest.getUsername(), pwdRequest.getPassword())) .filter(user -> Byte.valueOf((byte) 1).equals(user.getStatus())) diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultReactiveUserService.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultReactiveUserService.java index 3256c4d93..a7aaae404 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultReactiveUserService.java +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultReactiveUserService.java @@ -188,7 +188,7 @@ public class DefaultReactiveUserService extends GenericReactiveCrudService changePassword(String userId, String oldPassword, String newPassword) { passwordValidator.validate(newPassword); return findById(userId) - .switchIfEmpty(Mono.error(NotFoundException::new)) + .switchIfEmpty(Mono.error(NotFoundException.NoStackTrace::new)) .filter(user -> passwordEncoder.encode(oldPassword, user.getSalt()).equals(user.getPassword())) .switchIfEmpty(Mono.error(() -> new ValidationException("error.illegal_user_password"))) .flatMap(old -> {