Procházet zdrojové kódy

优化授权信息参数自动注入

zhouhao před 8 roky
rodič
revize
15e0514a49

+ 0 - 1
hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/Authorization.java

@@ -34,7 +34,6 @@ import java.util.function.Supplier;
  *
  * @author zhouhao
  * @see AuthorizationHolder
- * @see org.hswebframework.web.authorization.annotation.AuthInfo
  * @since 3.0
  */
 public interface Authorization extends Serializable {

+ 0 - 41
hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/annotation/AuthInfo.java

@@ -1,41 +0,0 @@
-/*
- * Copyright 2016 http://www.hswebframework.org
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *       http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- */
-
-package org.hswebframework.web.authorization.annotation;
-
-import java.lang.annotation.*;
-
-/**
- * 用于springmvc中,将授权信息自动注入到参数中.
- * 例如:
- * <pre>
- *    &#064;ReuqestMapping("/example")
- *    public ResponseMessage(&#064;AuthInfo Authorization auth){
- *      User user = auth.getUser();
- *      return ok();
- *    }
- * </pre>
- *
- * @author zhouhao
- * @see org.hswebframework.web.authorization.Authorization
- * @since 3.0
- */
-@Target(ElementType.PARAMETER)
-@Retention(RetentionPolicy.RUNTIME)
-@Documented
-public @interface AuthInfo {
-}

+ 2 - 4
hsweb-examples/hsweb-examples-simple/src/main/java/org/hswebframework/web/example/simple/TestController.java

@@ -5,7 +5,6 @@ import org.apache.shiro.authz.annotation.RequiresUser;
 import org.hswebframework.web.authorization.Authorization;
 import org.hswebframework.web.authorization.AuthorizationHolder;
 import org.hswebframework.web.authorization.Permission;
-import org.hswebframework.web.authorization.annotation.AuthInfo;
 import org.hswebframework.web.authorization.annotation.Authorize;
 import org.hswebframework.web.authorization.annotation.RequiresDataAccess;
 import org.hswebframework.web.authorization.annotation.RequiresFieldAccess;
@@ -19,7 +18,6 @@ import org.hswebframework.web.entity.authorization.SimpleUserEntity;
 import org.hswebframework.web.entity.authorization.UserEntity;
 import org.hswebframework.web.service.QueryByEntityService;
 import org.hswebframework.web.service.QueryService;
-import org.hswebframwork.utils.ClassUtils;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
@@ -36,13 +34,13 @@ public class TestController implements QueryController<UserEntity, String, Query
 
     @GetMapping("/test1")
     @Authorize(action = "query", message = "${'表达式方式'}")
-    public ResponseMessage testSimple(@AuthInfo Authorization authorization) {
+    public ResponseMessage testSimple(Authorization authorization) {
         return ResponseMessage.ok(authorization);
     }
 
     @GetMapping("/test")
     @RequiresPermissions("test:*")
-    public ResponseMessage testShiro(@AuthInfo Authorization authorization) {
+    public ResponseMessage testShiro(Authorization authorization) {
         return ResponseMessage.ok(authorization);
     }
 

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

@@ -17,30 +17,41 @@
 
 package org.hswebframework.web.starter.resolver;
 
+import org.hswebframework.web.authorization.Authorization;
 import org.hswebframework.web.authorization.AuthorizationSupplier;
-import org.hswebframework.web.authorization.annotation.AuthInfo;
 import org.springframework.core.MethodParameter;
+import org.springframework.util.Assert;
 import org.springframework.web.bind.support.WebDataBinderFactory;
 import org.springframework.web.context.request.NativeWebRequest;
 import org.springframework.web.method.support.HandlerMethodArgumentResolver;
 import org.springframework.web.method.support.ModelAndViewContainer;
 
 /**
- * TODO 完成注释
+ * 权限参数转换器,自动将{@link Authorization}注入controller
+ * 例如:
+ * <pre>
+ *     &#064;RequestMapping("/example")
+ *     public ResponseMessage foo(Authorization auth){
+ *          return ok();
+ *     }
+ * </pre>
  *
  * @author zhouhao
+ * @see Authorization
+ * @since 3.0
  */
 public class AuthorizationArgumentResolver implements HandlerMethodArgumentResolver {
 
     AuthorizationSupplier authorizationSupplier;
 
     public AuthorizationArgumentResolver(AuthorizationSupplier authorizationSupplier) {
+        Assert.notNull(authorizationSupplier);
         this.authorizationSupplier = authorizationSupplier;
     }
 
     @Override
     public boolean supportsParameter(MethodParameter parameter) {
-        return parameter.hasParameterAnnotation(AuthInfo.class);
+        return parameter.getParameterType() == Authorization.class;
     }
 
     @Override

+ 7 - 0
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/AuthorizationController.java

@@ -95,6 +95,13 @@ public class AuthorizationController {
         verifyCode.write(response.getOutputStream());
     }
 
+    @RequestMapping("/login-out")
+    @AccessLogger("退出登录")
+    public ResponseMessage loginOut() {
+
+        return ok();
+    }
+
     @PostMapping(value = "/login")
     @AccessLogger("授权")
     public ResponseMessage authorize(@RequestParam String username,

+ 1 - 3
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/UserController.java

@@ -17,8 +17,6 @@
 
 package org.hswebframework.web.controller.authorization;
 
-import org.hswebframework.web.authorization.Permission;
-import org.hswebframework.web.authorization.annotation.AuthInfo;
 import org.hswebframework.web.authorization.Authorization;
 import org.hswebframework.web.authorization.annotation.Authorize;
 import org.hswebframework.web.commons.entity.param.QueryParamEntity;
@@ -77,7 +75,7 @@ public class UserController implements QueryController<UserEntity, String, Query
     @Authorize(merge = false)
     @PutMapping(path = "/password")
     @AccessLogger("{update_password_login_user}")
-    public ResponseMessage updateLoginUserPassword(@AuthInfo Authorization authorization,
+    public ResponseMessage updateLoginUserPassword(Authorization authorization,
                                                    @RequestParam String password,
                                                    @RequestParam String oldPassword) {
         getService().updatePassword(authorization.getUser().getId(), oldPassword, password);