瀏覽代碼

优化权限控制

zhouhao 7 年之前
父節點
當前提交
83f9da757a
共有 15 個文件被更改,包括 353 次插入30 次删除
  1. 107 0
      hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/web/UserTokenController.java
  2. 23 0
      hsweb-authorization/hsweb-authorization-cloud/src/main/java/org/hswebframework/web/authorization/cloud/feign/FeignAuthenticationManager.java
  3. 23 23
      hsweb-authorization/hsweb-authorization-cloud/src/main/java/org/hswebframework/web/authorization/cloud/feign/FeignUserTokenManager.java
  4. 2 2
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-gateway/src/main/java/org/hswebframework/web/examples/cloud/gateway/Application.java
  5. 4 0
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-gateway/src/main/resources/application.yml
  6. 77 0
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/pom.xml
  7. 32 0
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/java/org/hswebframework/web/examples/cloud/service/Service01Application.java
  8. 19 0
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/java/org/hswebframework/web/examples/cloud/service/UserInfoController.java
  9. 18 0
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/resources/application.yml
  10. 9 0
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/resources/bootstrap.yml
  11. 12 0
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-user-center/pom.xml
  12. 21 1
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-user-center/src/main/java/org/hswebframework/web/examples/cloud/user/UserCenterApplication.java
  13. 1 1
      hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-user-center/src/main/resources/application.yml
  14. 1 0
      hsweb-examples/hsweb-examples-cloud/pom.xml
  15. 4 3
      hsweb-starter/hsweb-spring-boot-starter/src/main/java/org/hswebframework/web/starter/HswebAutoConfiguration.java

+ 107 - 0
hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/web/UserTokenController.java

@@ -0,0 +1,107 @@
+package org.hswebframework.web.authorization.basic.web;
+
+import org.hswebframework.web.authorization.Authentication;
+import org.hswebframework.web.authorization.AuthenticationManager;
+import org.hswebframework.web.authorization.token.TokenState;
+import org.hswebframework.web.authorization.token.UserToken;
+import org.hswebframework.web.authorization.token.UserTokenManager;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Lazy;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+/**
+ * Created by zhouhao on 2017/10/10.
+ */
+@RestController
+@RequestMapping
+public class UserTokenController {
+
+    private UserTokenManager userTokenManager;
+
+    private AuthenticationManager authenticationManager;
+
+    @Autowired
+    @Lazy
+    public void setUserTokenManager(UserTokenManager userTokenManager) {
+        this.userTokenManager = userTokenManager;
+    }
+
+    @Autowired
+    @Lazy
+    public void setAuthenticationManager(AuthenticationManager authenticationManager) {
+        this.authenticationManager = authenticationManager;
+    }
+
+    @GetMapping("/user-token/token/{token}")
+    public UserToken getByToken(@PathVariable String token) {
+        return userTokenManager.getByToken(token);
+    }
+
+    @GetMapping("/user-token/user/{userId}")
+    public List<UserToken> getByUserId(@PathVariable String userId){
+        return userTokenManager.getByUserId(userId);
+    }
+
+    @GetMapping("/user-token/user/{userId}/logged")
+    public boolean userIsLoggedIn(@PathVariable String userId){
+        return userTokenManager.userIsLoggedIn(userId);
+    }
+
+    @GetMapping("/user-token/token/{token}/logged")
+    public  boolean tokenIsLoggedIn(@PathVariable String token){
+        return userTokenManager.tokenIsLoggedIn(token);
+    }
+
+    @GetMapping("/user-token/user/total")
+    public  long totalUser(){
+        return userTokenManager.totalUser();
+    }
+
+    @GetMapping("/user-token/token/total")
+    public long totalToken(){
+        return userTokenManager.totalToken();
+    }
+
+    @GetMapping("/user-token}")
+    public List<UserToken> allLoggedUser(){
+        return userTokenManager.allLoggedUser();
+    }
+
+    @DeleteMapping("/user-token/user/{userId}")
+    public  void signOutByUserId(@PathVariable String userId){
+        userTokenManager.signOutByUserId(userId);
+    }
+
+    @DeleteMapping("/user-token/token/{token}")
+    public void signOutByToken(@PathVariable String token){
+        userTokenManager.signOutByToken(token);
+    }
+
+    @PutMapping("/user-token/user/{userId}/{state}")
+    public void changeUserState(@PathVariable String userId, @PathVariable TokenState state){
+        userTokenManager.changeUserState(userId, state);
+    }
+
+    @PutMapping("/user-token/token/{token}/{state}")
+    public  void changeTokenState(String token, TokenState state){
+         userTokenManager.changeTokenState(token,state);
+    }
+
+    @PostMapping("/user-token/{token}/{userId}/{maxInactiveInterval}")
+    public  UserToken signIn(@PathVariable String token, @PathVariable String userId, @PathVariable long maxInactiveInterval)
+    {
+        return userTokenManager.signIn(token,userId,maxInactiveInterval);
+    }
+
+    @GetMapping("/user-token/{token}/touch")
+    public void touch(@PathVariable String token) {
+        userTokenManager.touch(token);
+    }
+
+    @GetMapping("/user-auth/{userId}")
+    public Authentication userAuthInfo(@PathVariable String userId){
+        return authenticationManager.getByUserId(userId);
+    }
+}

+ 23 - 0
hsweb-authorization/hsweb-authorization-cloud/src/main/java/org/hswebframework/web/authorization/cloud/feign/FeignAuthenticationManager.java

@@ -0,0 +1,23 @@
+package org.hswebframework.web.authorization.cloud.feign;
+
+import org.hswebframework.web.authorization.Authentication;
+import org.hswebframework.web.authorization.AuthenticationManager;
+import org.springframework.cloud.netflix.feign.FeignClient;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+/**
+ * Created by zhouhao on 2017/10/10.
+ */
+@FeignClient(name = "${hsweb.cloud.user-center.name:user-center}")
+public interface FeignAuthenticationManager extends AuthenticationManager {
+    @Override
+    @RequestMapping(value = "/user-auth/{userId}",method = RequestMethod.GET)
+     Authentication getByUserId(@PathVariable("userId") String userId);
+
+    @Override
+    @RequestMapping(value = "/user-auth",method = RequestMethod.PUT)
+     Authentication sync(Authentication authentication);
+}

+ 23 - 23
hsweb-authorization/hsweb-authorization-cloud/src/main/java/org/hswebframework/web/authorization/cloud/feign/FeignUserTokenManager.java

@@ -16,54 +16,54 @@ import java.util.List;
 public interface FeignUserTokenManager extends UserTokenManager {
 
     @Override
-    @GetMapping("/user-token/token/{token}")
-    UserToken getByToken(@PathVariable String token);
+    @RequestMapping(value = "/user-token/token/{token}",method = RequestMethod.GET)
+    UserToken getByToken(@PathVariable("token") String token);
 
     @Override
-    @GetMapping("/user-token/user/{userId}")
-    List<UserToken> getByUserId(@PathVariable String userId);
+    @RequestMapping(value = "/user-token/user/{userId}",method = RequestMethod.GET)
+    List<UserToken> getByUserId(@PathVariable("userId") String userId);
 
     @Override
-    @GetMapping("/user-token/user/{userId}/logged")
-    boolean userIsLoggedIn(@PathVariable String userId);
+    @RequestMapping(value = "/user-token/user/{userId}/logged",method = RequestMethod.GET)
+    boolean userIsLoggedIn(@PathVariable("userId") String userId);
 
     @Override
-    @GetMapping("/user-token/token/{token}/logged")
-    boolean tokenIsLoggedIn(@PathVariable String token);
+    @RequestMapping(value = "/user-token/token/{token}/logged",method = RequestMethod.GET)
+    boolean tokenIsLoggedIn(@PathVariable("token") String token);
 
     @Override
-    @GetMapping("/user-token/user/total")
+    @RequestMapping(value = "/user-token/user/total",method = RequestMethod.GET)
     long totalUser();
 
     @Override
-    @GetMapping("/user-token/token/total")
+    @RequestMapping(value = "/user-token/token/total",method = RequestMethod.GET)
     long totalToken();
 
     @Override
-    @GetMapping("/user-token}")
+    @RequestMapping(value = "/user-token",method = RequestMethod.GET)
     List<UserToken> allLoggedUser();
 
     @Override
-    @DeleteMapping("/user-token/user/{userId}")
-    void signOutByUserId(@PathVariable String userId);
+    @RequestMapping(value = "/user-token/user/{userId}",method = RequestMethod.DELETE)
+    void signOutByUserId(@PathVariable("userId") String userId);
 
     @Override
-    @DeleteMapping("/user-token/token/{token}")
-    void signOutByToken(@PathVariable String token);
+    @RequestMapping(value = "/user-token/token/{token}",method = RequestMethod.DELETE)
+    void signOutByToken(@PathVariable("token") String token);
 
     @Override
-    @PutMapping("/user-token/user/{userId}/{state}")
-    void changeUserState(@PathVariable String userId, @PathVariable TokenState state);
+    @RequestMapping(value = "/user-token/user/{userId}/{state}",method = RequestMethod.PUT)
+    void changeUserState(@PathVariable("userId") String userId, @PathVariable("state") TokenState state);
 
     @Override
-    @PutMapping("/user-token/token/{token}/{state}")
-    void changeTokenState(String token, TokenState state);
+    @RequestMapping(value = "/user-token/token/{token}/{state}",method = RequestMethod.PUT)
+    void changeTokenState(@PathVariable("token") String token, @PathVariable("state") TokenState state);
 
     @Override
-    @PostMapping("/user-token/{token}/{userId}/{maxInactiveInterval}")
-    UserToken signIn(@PathVariable String token, @PathVariable String userId, @PathVariable long maxInactiveInterval);
+    @RequestMapping(value = "/user-token/{token}/{userId}/{maxInactiveInterval}",method = RequestMethod.POST)
+    UserToken signIn(@PathVariable("token") String token, @PathVariable("userId") String userId, @PathVariable("maxInactiveInterval") long maxInactiveInterval);
 
     @Override
-    @GetMapping("/user-token/{token}/touch")
-    void touch(String token);
+    @RequestMapping(value = "/user-token/{token}/touch",method = RequestMethod.GET)
+    void touch(@PathVariable("token") String token);
 }

+ 2 - 2
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-gateway/src/main/java/org/hswebframework/web/examples/cloud/gateway/Application.java

@@ -8,8 +8,8 @@ import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
 @SpringCloudApplication
 @EnableEurekaServer
 @EnableZuulProxy
-public class Application {
+public class GateWayApplication {
     public static void main(String[] args) {
-        SpringApplication.run(Application.class, args);
+        SpringApplication.run(GateWayApplication.class, args);
     }
 }

+ 4 - 0
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-gateway/src/main/resources/application.yml

@@ -12,6 +12,10 @@ zuul:
     user-center:
       path: /user-center/**
       service-id: user-center
+    service-1:
+      path: /service-1/**
+      service-id: service01
+  add-host-header: true
 ribbon:
   eureka:
     enabled: true

+ 77 - 0
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/pom.xml

@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>hsweb-examples-cloud</artifactId>
+        <groupId>org.hswebframework.web</groupId>
+        <version>3.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>hsweb-examples-cloud-service01</artifactId>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid</artifactId>
+            <version>1.0.26</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-spring-boot-starter</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-authorization-basic</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-authorization-cloud</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-feign</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-authorization-jwt</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-logging</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-spring-boot-starter</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-eureka</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-hystrix</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-classic</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.h2database</groupId>
+            <artifactId>h2</artifactId>
+        </dependency>
+    </dependencies>
+</project>

+ 32 - 0
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/java/org/hswebframework/web/examples/cloud/service/Service01Application.java

@@ -0,0 +1,32 @@
+package org.hswebframework.web.examples.cloud.service;
+
+
+import feign.Feign;
+import org.hswebframework.web.authorization.cloud.feign.FeignUserTokenManager;
+import org.hswebframework.web.authorization.token.UserTokenManager;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.netflix.feign.EnableFeignClients;
+import org.springframework.cloud.netflix.feign.FeignClient;
+import org.springframework.cloud.netflix.hystrix.EnableHystrix;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@SpringBootApplication
+@EnableDiscoveryClient
+@EnableHystrix
+@Configuration
+@EnableFeignClients("org.hswebframework.web.authorization.cloud.feign")
+public class Service01Application {
+
+//    @Bean
+//   public UserTokenManager userTokenManager(){
+//      return Feign.builder().target(FeignUserTokenManager.class,"http://localhost:9000");
+//    }
+
+
+    public static void main(String[] args) {
+        SpringApplication.run(Service01Application.class, args);
+    }
+}

+ 19 - 0
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/java/org/hswebframework/web/examples/cloud/service/UserInfoController.java

@@ -0,0 +1,19 @@
+package org.hswebframework.web.examples.cloud.service;
+
+import org.hswebframework.web.authorization.Authentication;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * Created by zhouhao on 2017/10/10.
+ */
+@RestController
+@RequestMapping("/user-info")
+public class UserInfoController {
+
+    @GetMapping
+    public Authentication authentication(){
+        return Authentication.current().orElse(null);
+    }
+}

+ 18 - 0
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/resources/application.yml

@@ -0,0 +1,18 @@
+spring:
+  aop:
+      auto: true
+  datasource:
+     url : jdbc:h2:mem:permission_test_mem
+     username : sa
+     password :
+     type: com.alibaba.druid.pool.DruidDataSource
+     driver-class-name : org.h2.Driver
+hsweb:
+    app:
+      name: 权限管理测试
+      version: 3.0.0
+server:
+    port: 9001
+logging:
+  level:
+    org.hswebframework.web: debug

+ 9 - 0
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/resources/bootstrap.yml

@@ -0,0 +1,9 @@
+spring:
+  application:
+    name: service01
+  cloud:
+    discovery:
+      client:
+        simple:
+          local:
+            service-id: service01

+ 12 - 0
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-user-center/pom.xml

@@ -31,6 +31,18 @@
             <version>${project.version}</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-authorization-basic</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-authorization-jwt</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-logging</artifactId>

+ 21 - 1
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-user-center/src/main/java/org/hswebframework/web/examples/cloud/user/UserCenterApplication.java

@@ -1,15 +1,35 @@
 package org.hswebframework.web.examples.cloud.user;
 
+import org.h2.command.Command;
+import org.hswebframework.web.entity.authorization.UserEntity;
+import org.hswebframework.web.service.authorization.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.CommandLineRunner;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.cloud.netflix.hystrix.EnableHystrix;
+import org.springframework.context.annotation.Configuration;
 
 @SpringBootApplication
 @EnableDiscoveryClient
 @EnableHystrix
-public class UserCenterApplication {
+@Configuration
+public class UserCenterApplication implements CommandLineRunner{
     public static void main(String[] args) {
         SpringApplication.run(UserCenterApplication.class, args);
     }
+
+    @Autowired
+    UserService userService;
+
+    @Override
+    public void run(String... strings) throws Exception {
+       UserEntity userEntity= userService.createEntity();
+       userEntity.setName("super user");
+       userEntity.setUsername("admin");
+       userEntity.setPassword("admin");
+
+       userService.insert(userEntity);
+    }
 }

+ 1 - 1
hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-user-center/src/main/resources/application.yml

@@ -12,7 +12,7 @@ hsweb:
       name: 权限管理测试
       version: 3.0.0
 server:
-    port: 9002
+    port: 9000
 logging:
   level:
     org.hswebframework.web: debug

+ 1 - 0
hsweb-examples/hsweb-examples-cloud/pom.xml

@@ -14,6 +14,7 @@
     <modules>
         <module>hsweb-examples-cloud-gateway</module>
         <module>hsweb-examples-cloud-user-center</module>
+        <module>hsweb-examples-cloud-service01</module>
     </modules>
 
     <dependencyManagement>

+ 4 - 3
hsweb-starter/hsweb-spring-boot-starter/src/main/java/org/hswebframework/web/starter/HswebAutoConfiguration.java

@@ -111,15 +111,16 @@ public class HswebAutoConfiguration {
                         return super.getDeserializer(type);
                     }
                     checkAutoType(type.getTypeName(), ((Class) type));
-
                     if (Modifier.isAbstract(classType.getModifiers()) || Modifier.isInterface(classType.getModifiers())) {
-                        if (entityFactory != null && (Entity.class.isAssignableFrom(classType) || Model.class.isAssignableFrom(classType))) {
-                            return new JavaBeanDeserializer(this, entityFactory.getInstanceType(classType), type);
+                        Class realType;
+                        if (entityFactory != null&& (realType=entityFactory.getInstanceType(classType))!=null) {
+                            return new JavaBeanDeserializer(this, realType, type);
                         }
                     } else {
                         return new JavaBeanDeserializer(this, classType);
                     }
                 }
+
                 return super.getDeserializer(type);
             }
         };