You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ma...@apache.org on 2019/08/08 01:30:01 UTC

[servicecomb-samples] 22/43: provide default authentication service implementation

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

mabin pushed a commit to branch houserush-sample
in repository https://gitbox.apache.org/repos/asf/servicecomb-samples.git

commit ab23af153a31eca1daa4406797caa80d89250c26
Author: liubao <ba...@huawei.com>
AuthorDate: Fri May 17 11:35:41 2019 +0800

    provide default authentication service implementation
---
 .../api/authentication-server/endpoint/pom.xml     |  5 ++
 .../authentication/api/AuthenticationEndpoint.java | 46 +++++++++++--
 .../service/AuthenticationServiceImpl.java         | 75 ----------------------
 3 files changed, 47 insertions(+), 79 deletions(-)

diff --git a/authentication/api/authentication-server/endpoint/pom.xml b/authentication/api/authentication-server/endpoint/pom.xml
index 5eebece..a653462 100644
--- a/authentication/api/authentication-server/endpoint/pom.xml
+++ b/authentication/api/authentication-server/endpoint/pom.xml
@@ -34,5 +34,10 @@
       <artifactId>authentication-server-api-service</artifactId>
       <version>${project.parent.version}</version>
     </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb.authentication</groupId>
+      <artifactId>authentication-common-api-service</artifactId>
+      <version>${project.parent.version}</version>
+    </dependency>
   </dependencies>
 </project>
diff --git a/authentication/api/authentication-server/endpoint/src/main/java/org/apache/servicecomb/authentication/api/AuthenticationEndpoint.java b/authentication/api/authentication-server/endpoint/src/main/java/org/apache/servicecomb/authentication/api/AuthenticationEndpoint.java
index 5177aeb..c777bc5 100644
--- a/authentication/api/authentication-server/endpoint/src/main/java/org/apache/servicecomb/authentication/api/AuthenticationEndpoint.java
+++ b/authentication/api/authentication-server/endpoint/src/main/java/org/apache/servicecomb/authentication/api/AuthenticationEndpoint.java
@@ -17,26 +17,64 @@
 
 package org.apache.servicecomb.authentication.api;
 
+import org.apache.servicecomb.authentication.jwt.JWTClaims;
+import org.apache.servicecomb.authentication.jwt.JsonParser;
 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.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.crypto.password.PasswordEncoder;
+import org.springframework.security.jwt.Jwt;
+import org.springframework.security.jwt.JwtHelper;
+import org.springframework.security.jwt.crypto.sign.Signer;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 
 @RestSchema(schemaId = "AuthenticationEndpoint")
 @RequestMapping(path = "/v1/auth")
-public class AuthenticationEndpoint {
+public class AuthenticationEndpoint implements AuthenticationService {
   @Autowired
-  private AuthenticationService authenticationService;
+  @Qualifier("authUserDetailsService")
+  private UserDetailsService userDetailsService;
 
+  @Autowired
+  @Qualifier("authPasswordEncoder")
+  private PasswordEncoder passwordEncoder;
+
+  @Autowired
+  @Qualifier("authSigner")
+  private Signer signer;
+
+  @Override
   @PostMapping(path = "login")
   public Token login(@RequestParam(name = "userName") String userName,
       @RequestParam(name = "password") String password) {
-    return authenticationService.login(userName, password);
+    UserDetails userDetails = userDetailsService.loadUserByUsername(userName);
+    if (passwordEncoder.matches(password, userDetails.getPassword())) {
+      JWTClaims claims = new JWTClaims();
+      if (userDetails.getAuthorities() != null) {
+        userDetails.getAuthorities().forEach(authority -> claims.addAuthority(authority.getAuthority()));
+      }
+      String content = JsonParser.unparse(claims);
+      Jwt accessToken = JwtHelper.encode(content, signer);
+
+      Token token = new Token();
+      token.setScope(claims.getScope());
+      token.setExpires_in(10 * 60);
+      token.setToken_type("bearer");
+      token.setAccess_token(accessToken.getEncoded());
+      return token;
+    } else {
+      return null;
+    }
   }
 
+  @Override
   @PostMapping(path = "refresh")
   public Token refresh(@RequestParam(name = "refreshToken") String refreshToken) {
-    return authenticationService.refresh(refreshToken);
+    return null;
   }
+
 }
diff --git a/authentication/samples/AuthenticationServer/src/main/java/org/apache/servicecomb/authentication/service/AuthenticationServiceImpl.java b/authentication/samples/AuthenticationServer/src/main/java/org/apache/servicecomb/authentication/service/AuthenticationServiceImpl.java
deleted file mode 100644
index cfbe22b..0000000
--- a/authentication/samples/AuthenticationServer/src/main/java/org/apache/servicecomb/authentication/service/AuthenticationServiceImpl.java
+++ /dev/null
@@ -1,75 +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.
- */
-
-package org.apache.servicecomb.authentication.service;
-
-import org.apache.servicecomb.authentication.api.AuthenticationService;
-import org.apache.servicecomb.authentication.api.Token;
-import org.apache.servicecomb.authentication.jwt.JWTClaims;
-import org.apache.servicecomb.authentication.jwt.JsonParser;
-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.crypto.password.PasswordEncoder;
-import org.springframework.security.jwt.Jwt;
-import org.springframework.security.jwt.JwtHelper;
-import org.springframework.security.jwt.crypto.sign.Signer;
-import org.springframework.stereotype.Service;
-
-@Service
-public class AuthenticationServiceImpl implements AuthenticationService {
-  @Autowired
-  @Qualifier("authUserDetailsService")
-  private UserDetailsService userDetailsService;
-
-  @Autowired
-  @Qualifier("authPasswordEncoder")
-  private PasswordEncoder passwordEncoder;
-
-  @Autowired
-  @Qualifier("authSigner")
-  private Signer signer;
-
-  @Override
-  public Token login(String userName, String password) {
-    UserDetails userDetails = userDetailsService.loadUserByUsername(userName);
-    if (passwordEncoder.matches(password, userDetails.getPassword())) {
-      JWTClaims claims = new JWTClaims();
-      if (userDetails.getAuthorities() != null) {
-        userDetails.getAuthorities().forEach(authority -> claims.addAuthority(authority.getAuthority()));
-      }
-      String content = JsonParser.unparse(claims);
-      Jwt accessToken = JwtHelper.encode(content, signer);
-
-      Token token = new Token();
-      token.setScope(claims.getScope());
-      token.setExpires_in(10 * 60);
-      token.setToken_type("bearer");
-      token.setAccess_token(accessToken.getEncoded());
-      return token;
-    } else {
-      return null;
-    }
-  }
-
-  @Override
-  public Token refresh(String refreshToken) {
-    return null;
-  }
-
-}