Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron-boom committed Aug 2, 2021
1 parent 063dde3 commit 0d13c48
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.dubbo.admin.authentication;

import org.apache.dubbo.common.extension.SPI;

import javax.servlet.http.HttpServletRequest;

/**
* Logon permission authentication
*
*/
@SPI
public interface LoginAuthentication {

boolean authentication(HttpServletRequest request, String userName, String password);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package org.apache.dubbo.admin.controller;

import org.apache.dubbo.admin.annotation.Authority;
import org.apache.dubbo.admin.authentication.LoginAuthentication;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -29,8 +31,10 @@
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -49,15 +53,26 @@ public class UserController {
private long sessionTimeoutMilli;

@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(@RequestParam String userName, @RequestParam String password) {
if (StringUtils.isBlank(rootUserName) || (rootUserName.equals(userName) && rootUserPassword.equals(password))) {
UUID uuid = UUID.randomUUID();
String token = uuid.toString();
User user = new User();
user.setUserName(userName);
user.setLastUpdateTime(System.currentTimeMillis());
tokenMap.put(token, user);
return token;
public String login(HttpServletRequest httpServletRequest, @RequestParam String userName, @RequestParam String password) {
ExtensionLoader<LoginAuthentication> extensionLoader = ExtensionLoader.getExtensionLoader(LoginAuthentication.class);
Set<LoginAuthentication> supportedExtensionInstances = extensionLoader.getSupportedExtensionInstances();
Iterator<LoginAuthentication> iterator = supportedExtensionInstances.iterator();
boolean flag = true;
if (iterator == null) {
if (StringUtils.isBlank(rootUserName) || (rootUserName.equals(userName) && rootUserPassword.equals(password))) {
return creatToken(rootUserName);
}
}
while (iterator.hasNext()) {
LoginAuthentication loginAuthentication = iterator.next();
boolean b = loginAuthentication.authentication(httpServletRequest, userName, password);
flag = b & flag;
if (flag == false) {
break;
}
}
if (flag) {
return creatToken(userName);
}
return null;
}
Expand Down Expand Up @@ -97,4 +112,13 @@ public void setLastUpdateTime(long lastUpdateTime) {
}
}

public String creatToken(String userName) {
UUID uuid = UUID.randomUUID();
String token = uuid.toString();
User user = new User();
user.setUserName(userName);
user.setLastUpdateTime(System.currentTimeMillis());
tokenMap.put(token, user);
return token;
}
}

0 comments on commit 0d13c48

Please sign in to comment.