Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For #756 #791

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}