You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2019/07/03 04:05:39 UTC

[servicecomb-fence] branch master updated (5c383c0 -> 33d3d13)

This is an automated email from the ASF dual-hosted git repository.

liubao pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-fence.git.


    from 5c383c0  [SCB-1340]support expiration time & not before time configuration
     new e8dcaff  [SCB-1343]provide web pages to demonstrate login and operations
     new 33d3d13  [SCB-1343]minor code condition check improvements

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 README.md                                          |   7 ++
 README_ZH.md                                       |   8 ++
 .../server/PasswordTokenGranter.java               |  22 +++--
 .../server/RefreshTokenTokenGranter.java           |   5 +
 .../edge/CustomVertxRestDispatcher.java            |   2 +-
 .../authentication/edge/TokenEndpoint.java         |   4 -
 .../servicecomb/authentication/TestEndpoint.java   |   1 -
 .../src/main/resources/microservice.yaml           |   4 +-
 .../EdgeService/src/main/resources/ui/js/login.js  |  22 ++---
 .../src/main/resources/ui/js/operation.js          | 108 +++++++++++++++++++++
 .../EdgeService/src/main/resources/ui/js/upload.js |  63 ------------
 .../resources/ui/{upload.html => operation.html}   |  32 +++---
 12 files changed, 173 insertions(+), 105 deletions(-)
 create mode 100644 samples/EdgeService/src/main/resources/ui/js/operation.js
 delete mode 100644 samples/EdgeService/src/main/resources/ui/js/upload.js
 rename samples/EdgeService/src/main/resources/ui/{upload.html => operation.html} (66%)


[servicecomb-fence] 01/02: [SCB-1343]provide web pages to demonstrate login and operations

Posted by li...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-fence.git

commit e8dcaffc660870ced658305071f121bdb068d341
Author: liubao <bi...@qq.com>
AuthorDate: Tue Jul 2 16:29:55 2019 +0800

    [SCB-1343]provide web pages to demonstrate login and operations
---
 README.md                                          |   7 ++
 README_ZH.md                                       |   8 ++
 .../edge/CustomVertxRestDispatcher.java            |   2 +-
 .../servicecomb/authentication/TestEndpoint.java   |   1 -
 .../src/main/resources/microservice.yaml           |   4 +-
 .../EdgeService/src/main/resources/ui/js/login.js  |  22 ++---
 .../src/main/resources/ui/js/operation.js          | 108 +++++++++++++++++++++
 .../EdgeService/src/main/resources/ui/js/upload.js |  63 ------------
 .../resources/ui/{upload.html => operation.html}   |  32 +++---
 9 files changed, 152 insertions(+), 95 deletions(-)

diff --git a/README.md b/README.md
index 9cfeab5..1fd5f35 100644
--- a/README.md
+++ b/README.md
@@ -24,6 +24,13 @@ mvn clean install
 
 After build, the Authentication Server, Resource Server, Edge Service and Testing Client runnable jar are generated, start and run the four services.
 
+* Demo web pages
+
+Using admin/changeMyPassword login to the demo pages, and try operations. 
+```
+http://localhost:9090/ui/login.html
+```
+
 * Run tests
 
 After services are started, try
diff --git a/README_ZH.md b/README_ZH.md
index 55dda1a..19cab05 100644
--- a/README_ZH.md
+++ b/README_ZH.md
@@ -25,6 +25,14 @@ mvn clean install
 
 编译完成后,会生成Authentication Server, Resource Server, Edge Service and Testing Client可执行jar包,运行这四个服务。
 
+* 使用演示界面
+
+进入页面,使用admin/changeMyPassword登录
+```
+http://localhost:9090/ui/login.html
+```
+
+
 * 运行测试用例
 
 当四个服务都运行起来后,访问:
diff --git a/api/edge-service/endpoint/src/main/java/org/apache/servicecomb/authentication/edge/CustomVertxRestDispatcher.java b/api/edge-service/endpoint/src/main/java/org/apache/servicecomb/authentication/edge/CustomVertxRestDispatcher.java
index a08873d..46fb804 100644
--- a/api/edge-service/endpoint/src/main/java/org/apache/servicecomb/authentication/edge/CustomVertxRestDispatcher.java
+++ b/api/edge-service/endpoint/src/main/java/org/apache/servicecomb/authentication/edge/CustomVertxRestDispatcher.java
@@ -64,7 +64,7 @@ public class CustomVertxRestDispatcher extends AbstractVertxHttpDispatcher {
   @Override
   public void init(Router router) {
     // TODO: regex configuration
-    String regex = "(/v1/log|/inspector|/v1/auth)/(.*)";
+    String regex = "(/v1/log|/inspector|/v1/token)(.*)";
     router.routeWithRegex(regex).handler(CookieHandler.create());
     router.routeWithRegex(regex).handler(createBodyHandler());
     router.routeWithRegex(regex).failureHandler(this::failureHandler).handler(this::onRequest);
diff --git a/samples/Client/src/main/java/org/apache/servicecomb/authentication/TestEndpoint.java b/samples/Client/src/main/java/org/apache/servicecomb/authentication/TestEndpoint.java
index 68f727e..d51dd54 100644
--- a/samples/Client/src/main/java/org/apache/servicecomb/authentication/TestEndpoint.java
+++ b/samples/Client/src/main/java/org/apache/servicecomb/authentication/TestEndpoint.java
@@ -23,7 +23,6 @@ import org.apache.servicecomb.provider.rest.common.RestSchema;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.client.HttpClientErrorException;
 
 @RestSchema(schemaId = "TestEndpoint")
 @RequestMapping(path = "/v1/test")
diff --git a/samples/EdgeService/src/main/resources/microservice.yaml b/samples/EdgeService/src/main/resources/microservice.yaml
index 7531d52..c75034f 100644
--- a/samples/EdgeService/src/main/resources/microservice.yaml
+++ b/samples/EdgeService/src/main/resources/microservice.yaml
@@ -64,8 +64,8 @@ servicecomb:
         default:
           enabled: false # overriden by ApiDispatcher
       rest:
-        eanbled: false # overriden by CustomVertxRestDispatcher
+        enabled: false # overriden by CustomVertxRestDispatcher
 
 # StaticWebpageDispatcher checking file exists is async, and will mark request status to ended, and VertxRestDispatcher read 
 # body will print exception. 
-#gateway.webroot: /code/servicecomb-samples/porter_lightweight/gateway-service/src/main/resources
+gateway.webroot: /code/servicecomb-fence/samples/EdgeService/src/main/resources
diff --git a/samples/EdgeService/src/main/resources/ui/js/login.js b/samples/EdgeService/src/main/resources/ui/js/login.js
index c7e7c8e..69c3c9b 100644
--- a/samples/EdgeService/src/main/resources/ui/js/login.js
+++ b/samples/EdgeService/src/main/resources/ui/js/login.js
@@ -19,20 +19,21 @@ function loginAction() {
      var username = document.getElementById("username").value;
      var password = document.getElementById("paasword").value;
      var formData = {};
-     formData.userName = username;
+     formData.username = username;
      formData.password = password;
+     formData.grant_type = "password";
 
      $.ajax({
         type: 'POST',
-        url: "/api/user-service/v1/user/login",
+        url: "/v1/token",
         data: formData,
         success: function (data) {
-            console.log(data);
-            setCookie("session-id", data.sessiondId, 1);
-            window.location = "/ui/upload.html";
+            console.log(JSON.stringify(data));
+            window.localStorage.setItem("token", JSON.stringify(data));
+            window.location = "/ui/operation.html";
         },
         error: function(data) {
-            console.log(data);
+            console.log(JSON.stringify(data));
             var error = document.getElementById("error");
             error.textContent="Login failed";
             error.hidden=false;
@@ -41,12 +42,3 @@ function loginAction() {
     });
 }
 
-function setCookie(name,value,days) {
-    var expires = "";
-    if (days) {
-        var date = new Date();
-        date.setTime(date.getTime() + (days*24*60*60*1000));
-        expires = "; expires=" + date.toUTCString();
-    }
-    document.cookie = name + "=" + (value || "")  + expires + "; path=/";
-}
\ No newline at end of file
diff --git a/samples/EdgeService/src/main/resources/ui/js/operation.js b/samples/EdgeService/src/main/resources/ui/js/operation.js
new file mode 100644
index 0000000..f911432
--- /dev/null
+++ b/samples/EdgeService/src/main/resources/ui/js/operation.js
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+function everyoneSayHello() {
+     $.ajax({
+        type: 'POST',
+        url: "/api/resource-server/v1/auth/handler/everyoneSayHello?name=Hi",
+        headers: {
+          Authorization: "Bearer " + JSON.parse(window.localStorage.getItem("token")).id_token
+        },
+        success: function (data) {
+            console.log(data);
+            var error = document.getElementById("error");
+            error.textContent="Successfully";
+            error.hidden=false;
+        },
+        error: function(data) {
+            console.log(data);
+            var error = document.getElementById("error");
+            error.textContent="Failed";
+            error.hidden=false;
+        },
+        async: true
+    });
+}
+
+function adminSayHello() {
+     $.ajax({
+        type: 'POST',
+        url: "/api/resource-server/v1/auth/handler/adminSayHello?name=Hi",
+        headers: {
+          Authorization: "Bearer " + JSON.parse(window.localStorage.getItem("token")).id_token
+        },
+        success: function (data) {
+            console.log(data);
+            var error = document.getElementById("error");
+            error.textContent="Successfully";
+            error.hidden=false;
+        },
+        error: function(data) {
+            console.log(data);
+            var error = document.getElementById("error");
+            error.textContent="Failed";
+            error.hidden=false;
+        },
+        async: true
+    });
+}
+
+function guestOrAdminSayHello() {
+     $.ajax({
+        type: 'POST',
+        url: "/api/resource-server/v1/auth/handler/guestOrAdminSayHello?name=Hi",
+        headers: {
+          Authorization: "Bearer " + JSON.parse(window.localStorage.getItem("token")).id_token
+        },
+        success: function (data) {
+            console.log(data);
+            var error = document.getElementById("error");
+            error.textContent="Successfully";
+            error.hidden=false;
+        },
+        error: function(data) {
+            console.log(data);
+            var error = document.getElementById("error");
+            error.textContent="Failed";
+            error.hidden=false;
+        },
+        async: true
+    });
+}
+
+function guestSayHello() {
+     $.ajax({
+        type: 'POST',
+        url: "/api/resource-server/v1/auth/handler/guestSayHello?name=Hi",
+        headers: {
+          Authorization: "Bearer " + JSON.parse(window.localStorage.getItem("token")).id_token
+        },
+        success: function (data) {
+            console.log(data);
+            var error = document.getElementById("error");
+            error.textContent="Successfully";
+            error.hidden=false;
+        },
+        error: function(data) {
+            console.log(data);
+            var error = document.getElementById("error");
+            error.textContent="Failed";
+            error.hidden=false;
+        },
+        async: true
+    });
+}
\ No newline at end of file
diff --git a/samples/EdgeService/src/main/resources/ui/js/upload.js b/samples/EdgeService/src/main/resources/ui/js/upload.js
deleted file mode 100644
index 7d66555..0000000
--- a/samples/EdgeService/src/main/resources/ui/js/upload.js
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.
- */
-
-function uploadAction() {
-     var formData = new FormData(document.getElementById("upload_form"));
-
-     $.ajax({
-        type: 'POST',
-        url: "/api/file-service/upload",
-        data: formData,
-        processData:false,
-        contentType:false,
-        success: function (data) {
-            console.log(data);
-            var error = document.getElementById("error");
-            error.textContent="Upload Successfully";
-            error.hidden=false;
-        },
-        error: function(data) {
-            console.log(data);
-            var error = document.getElementById("error");
-            error.textContent="Upload failed";
-            error.hidden=false;
-        },
-        async: true
-    });
-}
-
-function deleteAction() {
-     var fileID = document.getElementById("fileID").value;
-     $.ajax({
-        type: 'DELETE',
-        url: "/api/file-service/delete?" + $.param({ id: fileID }),
-        data: {},
-        success: function (data) {
-            console.log(data);
-            var error = document.getElementById("error");
-            error.textContent="Delete successfully";
-            error.hidden=false;
-        },
-        error: function(data) {
-            console.log(data);
-            var error = document.getElementById("error");
-            error.textContent="Delete failed";
-            error.hidden=false;
-        },
-        async: true
-    });
-}
\ No newline at end of file
diff --git a/samples/EdgeService/src/main/resources/ui/upload.html b/samples/EdgeService/src/main/resources/ui/operation.html
similarity index 66%
rename from samples/EdgeService/src/main/resources/ui/upload.html
rename to samples/EdgeService/src/main/resources/ui/operation.html
index bd5eeff..c127612 100644
--- a/samples/EdgeService/src/main/resources/ui/upload.html
+++ b/samples/EdgeService/src/main/resources/ui/operation.html
@@ -22,37 +22,43 @@
         <title>PORTER</title>
         <link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
         <script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
-        <script type="text/javascript" src="js/upload.js"></script>
+        <script type="text/javascript" src="js/operation.js"></script>
     </head>
 
     <body>
-    <div class="header">
-        <h2>Upload</h2>
-    </div>
+
     <div class="section">
-        <form id="upload_form" method="POST">
+        <form id="everyoneSayHello" method="POST">
             <p>
-                File Name: <input type="file" name="fileName"/>
+                <input type="button" value="everyoneSayHello" onclick="everyoneSayHello()">
             </p>
+        </form>
+    </div>
+
+    <div class="section">
+        <form id="adminSayHello" method="POST">
             <p>
-                <input type="button" value="Upload" onclick="uploadAction()">
+                <input type="button" value="adminSayHello" onclick="adminSayHello()">
             </p>
         </form>
     </div>
 
-    <div class="header">
-        <h2>Delete file</h2>
-    </div>
     <div class="section">
-        <form id="delete_form" method="DELETE">
+        <form id="guestOrAdminSayHello" method="POST">
             <p>
-                File ID: <input id="fileID" type="text"/>
+                <input type="button" value="guestOrAdminSayHello" onclick="guestOrAdminSayHello()">
             </p>
+        </form>
+    </div>
+
+    <div class="section">
+        <form id="guestSayHello" method="POST">
             <p>
-                <input type="button" value="Delete" onclick="deleteAction()">
+                <input type="button" value="guestSayHello" onclick="guestSayHello()">
             </p>
         </form>
     </div>
+
     <div class="footer">
         <p id="error" hidden="true" class="error"/>
     </div>


[servicecomb-fence] 02/02: [SCB-1343]minor code condition check improvements

Posted by li...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-fence.git

commit 33d3d138839379a698bf8f7ae31dedd66c96164b
Author: liubao <bi...@qq.com>
AuthorDate: Wed Jul 3 12:04:51 2019 +0800

    [SCB-1343]minor code condition check improvements
---
 .../server/PasswordTokenGranter.java               | 22 ++++++++++++++++------
 .../server/RefreshTokenTokenGranter.java           |  5 +++++
 .../authentication/edge/TokenEndpoint.java         |  4 ----
 3 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/api/authentication-server/endpoint/src/main/java/org/apache/servicecomb/authentication/server/PasswordTokenGranter.java b/api/authentication-server/endpoint/src/main/java/org/apache/servicecomb/authentication/server/PasswordTokenGranter.java
index 45c8ca2..bb32d48 100644
--- a/api/authentication-server/endpoint/src/main/java/org/apache/servicecomb/authentication/server/PasswordTokenGranter.java
+++ b/api/authentication-server/endpoint/src/main/java/org/apache/servicecomb/authentication/server/PasswordTokenGranter.java
@@ -19,6 +19,7 @@ package org.apache.servicecomb.authentication.server;
 
 import java.util.Map;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.servicecomb.authentication.token.AbstractOpenIDTokenStore;
 import org.apache.servicecomb.authentication.token.OpenIDToken;
 import org.apache.servicecomb.authentication.util.CommonConstants;
@@ -26,6 +27,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.security.core.userdetails.UserDetails;
 import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
 import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.stereotype.Component;
 
@@ -50,12 +52,20 @@ public class PasswordTokenGranter implements TokenGranter {
     String username = parameters.get(AuthenticationServerConstants.PARAM_USERNAME);
     String password = parameters.get(AuthenticationServerConstants.PARAM_PASSWORD);
 
-    UserDetails userDetails = userDetailsService.loadUserByUsername(username);
-    if (passwordEncoder.matches(password, userDetails.getPassword())) {
-      OpenIDToken openIDToken = openIDTokenStore.createToken(userDetails);
-      openIDTokenStore.saveToken(openIDToken);
-      return TokenResponse.fromOpenIDToken(openIDToken);
-    } else {
+    if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
+      return null;
+    }
+
+    try {
+      UserDetails userDetails = userDetailsService.loadUserByUsername(username);
+      if (passwordEncoder.matches(password, userDetails.getPassword())) {
+        OpenIDToken openIDToken = openIDTokenStore.createToken(userDetails);
+        openIDTokenStore.saveToken(openIDToken);
+        return TokenResponse.fromOpenIDToken(openIDToken);
+      } else {
+        return null;
+      }
+    } catch (UsernameNotFoundException e) {
       return null;
     }
   }
diff --git a/api/authentication-server/endpoint/src/main/java/org/apache/servicecomb/authentication/server/RefreshTokenTokenGranter.java b/api/authentication-server/endpoint/src/main/java/org/apache/servicecomb/authentication/server/RefreshTokenTokenGranter.java
index 77a9f98..a18bd2d 100644
--- a/api/authentication-server/endpoint/src/main/java/org/apache/servicecomb/authentication/server/RefreshTokenTokenGranter.java
+++ b/api/authentication-server/endpoint/src/main/java/org/apache/servicecomb/authentication/server/RefreshTokenTokenGranter.java
@@ -19,6 +19,7 @@ package org.apache.servicecomb.authentication.server;
 
 import java.util.Map;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.servicecomb.authentication.token.AbstractOpenIDTokenStore;
 import org.apache.servicecomb.authentication.token.OpenIDToken;
 import org.apache.servicecomb.authentication.token.Token;
@@ -57,6 +58,10 @@ public class RefreshTokenTokenGranter implements TokenGranter {
   public TokenResponse grant(Map<String, String> parameters) {
     String refreshTokenValue = parameters.get(AuthenticationServerConstants.PARAM_REFRESH_TOKEN);
 
+    if (StringUtils.isEmpty(refreshTokenValue)) {
+      return null;
+    }
+
     Token refreshToken = openIDTokenStore.readTokenByRefreshTokenValue(refreshTokenValue);
 
     if (refreshToken != null && !refreshToken.isExpired()) {
diff --git a/api/edge-service/endpoint/src/main/java/org/apache/servicecomb/authentication/edge/TokenEndpoint.java b/api/edge-service/endpoint/src/main/java/org/apache/servicecomb/authentication/edge/TokenEndpoint.java
index 8718a0c..578f71a 100644
--- a/api/edge-service/endpoint/src/main/java/org/apache/servicecomb/authentication/edge/TokenEndpoint.java
+++ b/api/edge-service/endpoint/src/main/java/org/apache/servicecomb/authentication/edge/TokenEndpoint.java
@@ -26,7 +26,6 @@ import org.apache.servicecomb.provider.pojo.RpcReference;
 import org.apache.servicecomb.provider.rest.common.RestSchema;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
-import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -47,9 +46,6 @@ public class TokenEndpoint implements TokenService {
   public CompletableFuture<TokenResponse> getToken(@RequestBody Map<String, String> parameters) {
     CompletableFuture<TokenResponse> result = new CompletableFuture<>();
 
-    HttpHeaders headers = new HttpHeaders();
-    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
-
     CompletableFuture<TokenResponse> response =
         authenticationSererTokenEndpoint.getToken(parameters);
     response.whenComplete((tokenResonse, ex) -> {