Procházet zdrojové kódy

自己调用自己的权限管理

gaodm před 5 roky
rodič
revize
8fa5195b37

+ 31 - 0
src/main/java/com/diagbot/client/AuthServiceClient.java

@@ -0,0 +1,31 @@
+package com.diagbot.client;
+
+import com.diagbot.client.hystrix.AuthServiceHystrix;
+import com.diagbot.entity.JWT;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.security.oauth2.common.OAuth2AccessToken;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestParam;
+
+
+/**
+ * @Description: 请求认证授权服务器客户端
+ * @author: gaodm
+ * @time: 2018/8/2 13:37
+ */
+@FeignClient(name = "oath-self", url = "${oath.self.address}",fallback = AuthServiceHystrix.class)
+public interface AuthServiceClient {
+
+    @PostMapping(value = "/oauth/token")
+    JWT getToken(@RequestHeader(value = "Authorization") String authorization, @RequestParam("grant_type") String type, @RequestParam("username") String username, @RequestParam("password") String password);
+
+    @PostMapping(value = "/oauth/token")
+    JWT refreshToken(@RequestHeader(value = "Authorization") String authorization, @RequestParam("grant_type") String type, @RequestParam("refresh_token") String refreshToken);
+
+    @PostMapping(value = "/oauth/check_token")
+    OAuth2AccessToken checkToken(@RequestHeader(value = "Authorization") String authorization, @RequestParam("token") String token);
+}
+
+
+

+ 35 - 0
src/main/java/com/diagbot/client/hystrix/AuthServiceHystrix.java

@@ -0,0 +1,35 @@
+package com.diagbot.client.hystrix;
+
+
+import com.diagbot.client.AuthServiceClient;
+import com.diagbot.entity.JWT;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.security.oauth2.common.OAuth2AccessToken;
+import org.springframework.stereotype.Component;
+
+/**
+ * @Description: 请求认证授权服务器客户端(请求失败熔断)
+ * @author: gaodm
+ * @time: 2018/8/2 13:37
+ */
+@Component
+@Slf4j
+public class AuthServiceHystrix implements AuthServiceClient {
+    @Override
+    public JWT getToken(String authorization, String type, String username, String password) {
+        log.error("【hystrix】调用{}异常", "getToken");
+        return null;
+    }
+
+    @Override
+    public JWT refreshToken(String authorization, String type, String refreshToken) {
+        log.error("【hystrix】调用{}异常", "refreshToken");
+        return null;
+    }
+
+    @Override
+    public OAuth2AccessToken checkToken(String authorization, String token){
+        log.error("【hystrix】调用{}异常", "checkToken");
+        return null;
+    }
+}

+ 75 - 0
src/main/java/com/diagbot/entity/JWT.java

@@ -0,0 +1,75 @@
+package com.diagbot.entity;
+
+/**
+ * @Description: JWT实体类
+ * @author: gaodm
+ * @time: 2018/8/2 13:53
+ */
+public class JWT {
+    private String access_token;
+    private String token_type;
+    private String refresh_token;
+    private int expires_in;
+    private String scope;
+    private String jti;
+
+    public String getAccess_token() {
+        return access_token;
+    }
+
+    public void setAccess_token(String access_token) {
+        this.access_token = access_token;
+    }
+
+    public String getToken_type() {
+        return token_type;
+    }
+
+    public void setToken_type(String token_type) {
+        this.token_type = token_type;
+    }
+
+    public String getRefresh_token() {
+        return refresh_token;
+    }
+
+    public void setRefresh_token(String refresh_token) {
+        this.refresh_token = refresh_token;
+    }
+
+    public int getExpires_in() {
+        return expires_in;
+    }
+
+    public void setExpires_in(int expires_in) {
+        this.expires_in = expires_in;
+    }
+
+    public String getScope() {
+        return scope;
+    }
+
+    public void setScope(String scope) {
+        this.scope = scope;
+    }
+
+    public String getJti() {
+        return jti;
+    }
+
+    public void setJti(String jti) {
+        this.jti = jti;
+    }
+
+    @Override
+    public String toString() {
+        return "JWT{" +
+                "access_token='" + access_token + '\'' +
+                ", token_type='" + token_type + '\'' +
+                ", refresh_token='" + refresh_token + '\'' +
+                ", expires_in=" + expires_in +
+                ", scope='" + scope + '\'' +
+                ", jti='" + jti + '\'' +
+                '}';
+    }
+}

+ 27 - 0
src/main/java/com/diagbot/web/TestController.java

@@ -0,0 +1,27 @@
+package com.diagbot.web;
+
+import com.diagbot.client.AuthServiceClient;
+import com.diagbot.entity.JWT;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @Description:
+ * @author: gaodm
+ * @date: 2020/4/9 20:03
+ * @version: V1.0
+ */
+@RestController
+@RequestMapping("/test")
+public class TestController {
+    @Autowired
+    private AuthServiceClient authServiceClient;
+    //p:f6af7afd01d4eb0dc5fe0a342cd6cee7
+    @PostMapping(value = "/oauth/token")
+    public JWT getToken(String username, String password) {
+        return authServiceClient.getToken("Basic dWFhLXNlcnZpY2U6MTIzNDU2",
+                "password", username, password);
+    }
+}

+ 2 - 0
src/main/resources/application-dev.yml

@@ -149,5 +149,7 @@ mybatis-plus:
     map-underscore-to-camel-case: true
     cache-enabled: false
 
+oath.self.address: http://localhost:${server.port}
+
 swagger:
   enable: true

+ 2 - 0
src/main/resources/application-local.yml

@@ -149,5 +149,7 @@ mybatis-plus:
     map-underscore-to-camel-case: true
     cache-enabled: false
 
+oath.self.address: http://localhost:${server.port}
+
 swagger:
   enable: true

+ 2 - 0
src/main/resources/application-pre.yml

@@ -149,5 +149,7 @@ mybatis-plus:
     map-underscore-to-camel-case: true
     cache-enabled: false
 
+oath.self.address: http://localhost:${server.port}
+
 swagger:
   enable: true

+ 2 - 0
src/main/resources/application-pro.yml

@@ -149,5 +149,7 @@ mybatis-plus:
     map-underscore-to-camel-case: true
     cache-enabled: false
 
+oath.self.address: http://localhost:${server.port}
+
 swagger:
   enable: true

+ 2 - 0
src/main/resources/application-test.yml

@@ -149,5 +149,7 @@ mybatis-plus:
     map-underscore-to-camel-case: true
     cache-enabled: false
 
+oath.self.address: http://localhost:${server.port}
+
 swagger:
   enable: true