Browse Source

各种小优化

zhouhao 8 năm trước cách đây
mục cha
commit
204ae12163

+ 7 - 7
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-server/hsweb-authorization-oauth2-server-controller/src/main/java/org/hswebframework/web/authorization/oauth2/controller/OAuth2AuthorizationController.java

@@ -18,17 +18,17 @@
 
 package org.hswebframework.web.authorization.oauth2.controller;
 
-import io.swagger.annotations.*;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
 import org.apache.commons.codec.binary.Base64;
-import org.hswebframework.web.authorization.Authorization;
-import org.hswebframework.web.authorization.AuthorizationHolder;
+import org.hswebframework.web.authorization.Authentication;
+import org.hswebframework.web.authorization.AuthenticationHolder;
 import org.hswebframework.web.authorization.annotation.Authorize;
 import org.hswebframework.web.authorization.oauth2.api.OAuth2ServerService;
-import org.hswebframework.web.entity.authorization.oauth2.OAuth2AccessEntity;
 import org.hswebframework.web.authorization.oauth2.model.AccessTokenModel;
 import org.hswebframework.web.authorization.oauth2.model.AuthorizationCodeModel;
 import org.hswebframework.web.authorization.oauth2.model.ImplicitAccessTokenModel;
-import org.hswebframework.web.controller.message.ResponseMessage;
+import org.hswebframework.web.entity.authorization.oauth2.OAuth2AccessEntity;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
@@ -55,8 +55,8 @@ public class OAuth2AuthorizationController {
             @RequestParam("redirect_uri") String redirectUri,
             @RequestParam(value = "scope", required = false) String scope,
             @RequestParam(value = "state", required = false) String state) {
-        Authorization authorization = AuthorizationHolder.get();
-        String code = oAuth2ServerService.requestCode(clientId, authorization.getUser().getId(), scope);
+        Authentication authentication = AuthenticationHolder.get();
+        String code = oAuth2ServerService.requestCode(clientId, authentication.getUser().getId(), scope);
         AuthorizationCodeModel model = new AuthorizationCodeModel();
         model.setCode(code);
         model.setRedirectUri(redirectUri);

+ 1 - 1
hsweb-commons/hsweb-commons-service/hsweb-commons-service-api/src/main/java/org/hswebframework/web/service/QueryByEntityService.java

@@ -42,7 +42,7 @@ public interface QueryByEntityService<E> extends Service {
     PagerResult<E> selectPager(Entity param);
 
     /**
-     * 不分页查询
+     * 直接查询
      *
      * @param param 查询参数
      * @return 查询结果

+ 7 - 1
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericEntityService.java

@@ -28,7 +28,7 @@ import org.springframework.transaction.annotation.Transactional;
 import java.util.List;
 
 /**
- * TODO 完成注释
+ * 通用实体服务类,提供增删改查的默认实现
  *
  * @author zhouhao
  */
@@ -42,6 +42,12 @@ public abstract class GenericEntityService<E extends GenericEntity<PK>, PK>
         super();
     }
 
+    /**
+     * 获取ID生成器,在insert的时候,如果ID为空,则调用生成器进行生成
+     *
+     * @return IDGenerator
+     * @see IDGenerator
+     */
     protected abstract IDGenerator<PK> getIDGenerator();
 
     @Override

+ 21 - 1
hsweb-commons/hsweb-commons-utils/src/main/java/org/hswebframework/web/AopUtils.java

@@ -18,14 +18,34 @@
 package org.hswebframework.web;
 
 import org.aspectj.lang.JoinPoint;
-import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.reflect.MethodSignature;
+import org.springframework.core.annotation.AnnotationUtils;
+import org.springframework.util.ClassUtils;
 
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
 public class AopUtils {
 
+    public static <T extends Annotation> T findAnnotation(Class targetClass, Method method, Class<T> annClass) {
+        Method m = method;
+        T a = AnnotationUtils.findAnnotation(m, annClass);
+        if (a != null) return a;
+        m = ClassUtils.getMostSpecificMethod(m, targetClass);
+        a = AnnotationUtils.findAnnotation(m, annClass);
+        if (a != null) return a;
+        return AnnotationUtils.findAnnotation(targetClass, annClass);
+    }
+
+    public static <T extends Annotation> T findAnnotation(JoinPoint pjp, Class<T> annClass) {
+        MethodSignature signature = (MethodSignature) pjp.getSignature();
+        Method m = signature.getMethod();
+        Class<?> targetClass = pjp.getTarget().getClass();
+        return findAnnotation(targetClass, m, annClass);
+    }
+
     public static final String getMethodBody(JoinPoint pjp) {
         StringBuilder methodName = new StringBuilder(pjp.getSignature().getName()).append("(");
         MethodSignature signature = (MethodSignature) pjp.getSignature();

+ 15 - 3
hsweb-commons/hsweb-commons-utils/src/main/java/org/hswebframework/web/ThreadLocalUtils.java

@@ -20,10 +20,13 @@ package org.hswebframework.web;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.function.Supplier;
 
 /**
- * Created by zhouhao on 16-5-26.
+ * @author zhouhao
+ * @since 2.0
  */
+@SuppressWarnings("unchecked")
 public class ThreadLocalUtils {
     private static final ThreadLocal<Map<String, Object>> local = ThreadLocal.withInitial(() -> new HashMap<>());
 
@@ -40,12 +43,21 @@ public class ThreadLocalUtils {
         local.remove();
     }
 
-    @SuppressWarnings("unchecked")
     public static <T> T get(String key) {
         return ((T) local.get().get(key));
     }
 
-    @SuppressWarnings("unchecked")
+    /**
+     * @since 3.0
+     */
+    public static <T> T get(String key, Supplier<T> supplierOnNull) {
+        T val = ((T) local.get().get(key));
+        if (null != val) return val;
+        val = supplierOnNull.get();
+        local.get().put(key, val);
+        return val;
+    }
+
     public static <T> T getAndRemove(String key) {
         try {
             return ((T) local.get().get(key));

+ 10 - 10
hsweb-starter/hsweb-spring-boot-starter/src/main/java/org/hswebframework/web/starter/resolver/AuthorizationArgumentResolver.java

@@ -17,8 +17,8 @@
 
 package org.hswebframework.web.starter.resolver;
 
-import org.hswebframework.web.authorization.Authorization;
-import org.hswebframework.web.authorization.AuthorizationSupplier;
+import org.hswebframework.web.authorization.Authentication;
+import org.hswebframework.web.authorization.AuthenticationSupplier;
 import org.springframework.core.MethodParameter;
 import org.springframework.util.Assert;
 import org.springframework.web.bind.support.WebDataBinderFactory;
@@ -27,7 +27,7 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver;
 import org.springframework.web.method.support.ModelAndViewContainer;
 
 /**
- * 权限参数转换器,自动将{@link Authorization}注入controller
+ * 权限参数转换器,自动将{@link Authentication}注入controller
  * 例如:
  * <pre>
  *     &#064;RequestMapping("/example")
@@ -37,25 +37,25 @@ import org.springframework.web.method.support.ModelAndViewContainer;
  * </pre>
  *
  * @author zhouhao
- * @see Authorization
+ * @see Authentication
  * @since 3.0
  */
 public class AuthorizationArgumentResolver implements HandlerMethodArgumentResolver {
 
-    AuthorizationSupplier authorizationSupplier;
+    AuthenticationSupplier authenticationSupplier;
 
-    public AuthorizationArgumentResolver(AuthorizationSupplier authorizationSupplier) {
-        Assert.notNull(authorizationSupplier);
-        this.authorizationSupplier = authorizationSupplier;
+    public AuthorizationArgumentResolver(AuthenticationSupplier authenticationSupplier) {
+        Assert.notNull(authenticationSupplier);
+        this.authenticationSupplier = authenticationSupplier;
     }
 
     @Override
     public boolean supportsParameter(MethodParameter parameter) {
-        return parameter.getParameterType() == Authorization.class;
+        return parameter.getParameterType() == Authentication.class;
     }
 
     @Override
     public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
-        return authorizationSupplier.get();
+        return authenticationSupplier.get();
     }
 }