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 830 #832
The head ref may contain hidden characters: "develop-issue\#${830}"
for 830 #832
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,39 +18,28 @@ | |
|
||
import org.apache.dubbo.admin.annotation.Authority; | ||
import org.apache.dubbo.admin.authentication.LoginAuthentication; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.dubbo.admin.utils.JwtTokenUtil; | ||
import org.apache.dubbo.common.extension.ExtensionLoader; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
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; | ||
|
||
@RestController | ||
@RequestMapping("/api/{env}/user") | ||
public class UserController { | ||
public static Map<String /*token*/, User /*user info*/> tokenMap = new ConcurrentHashMap<>(); | ||
|
||
@Value("${admin.root.user.name:}") | ||
private String rootUserName; | ||
@Value("${admin.root.user.password:}") | ||
private String rootUserPassword; | ||
//make session timeout configurable | ||
//default to be an hour:1000 * 60 * 60 | ||
@Value("${admin.check.sessionTimeoutMilli:3600000}") | ||
private long sessionTimeoutMilli; | ||
|
||
@RequestMapping(value = "/login", method = RequestMethod.GET) | ||
public String login(HttpServletRequest httpServletRequest, @RequestParam String userName, @RequestParam String password) { | ||
|
@@ -60,35 +49,27 @@ public String login(HttpServletRequest httpServletRequest, @RequestParam String | |
boolean flag = true; | ||
if (iterator == null) { | ||
if (StringUtils.isBlank(rootUserName) || (rootUserName.equals(userName) && rootUserPassword.equals(password))) { | ||
return creatToken(rootUserName); | ||
return JwtTokenUtil.generateToken(userName); | ||
} | ||
} | ||
while (iterator.hasNext()) { | ||
LoginAuthentication loginAuthentication = iterator.next(); | ||
boolean b = loginAuthentication.authentication(httpServletRequest, userName, password); | ||
flag = b & flag; | ||
if (flag == false) { | ||
if (!flag) { | ||
break; | ||
} | ||
} | ||
if (flag) { | ||
return creatToken(userName); | ||
return JwtTokenUtil.generateToken(userName); | ||
} | ||
return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use |
||
} | ||
|
||
@Authority(needLogin = true) | ||
@RequestMapping(value = "/logout", method = RequestMethod.DELETE) | ||
public boolean logout() { | ||
HttpServletRequest request = | ||
((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); | ||
String token = request.getHeader("Authorization"); | ||
return null != tokenMap.remove(token); | ||
} | ||
|
||
@Scheduled(cron= "0 5 * * * ?") | ||
public void clearExpiredToken() { | ||
tokenMap.entrySet().removeIf(entry -> entry.getValue() == null || System.currentTimeMillis() - entry.getValue().getLastUpdateTime() > sessionTimeoutMilli); | ||
return true; | ||
} | ||
|
||
public static class User { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
|
@@ -112,13 +93,4 @@ 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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* 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.utils; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
|
||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Jwt token tool class. | ||
*/ | ||
public class JwtTokenUtil { | ||
/** | ||
* Jwt signingKey | ||
*/ | ||
private static final String secret = "a1g2y47dg3dj59fjhhsd7cnewy73j"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
/** | ||
* token timeout configurable | ||
* default to be an hour: 1000 * 60 * 60 | ||
*/ | ||
private static final long expiration = 60 * 60 * 1000; | ||
|
||
/** | ||
* initialise jwt token and claims | ||
* | ||
* @return jwt token | ||
* @param rootUserName | ||
*/ | ||
public static String generateToken(String rootUserName) { | ||
Map<String, Object> claims = new HashMap<>(1); | ||
claims.put("sub", rootUserName); | ||
return generateToken(claims); | ||
} | ||
|
||
/** | ||
* generate token | ||
* | ||
* @return jwt token | ||
* @param claims | ||
*/ | ||
private static String generateToken(Map<String, Object> claims) { | ||
return Jwts.builder() | ||
.setClaims(claims) | ||
.setExpiration(generateExpirationDate()) | ||
.setIssuedAt(generateCurrentDate()) | ||
.signWith(SignatureAlgorithm.HS512, secret) | ||
.compact(); | ||
} | ||
|
||
/** | ||
* Get the current time | ||
*/ | ||
private static Date generateCurrentDate() { | ||
return new Date(System.currentTimeMillis()); | ||
} | ||
|
||
/** | ||
* Get the token expiration time | ||
*/ | ||
private static Date generateExpirationDate() { | ||
return new Date(System.currentTimeMillis() + expiration); | ||
} | ||
|
||
/** | ||
* Check whether the token is invalid | ||
* | ||
* @return boolean type | ||
* @param token | ||
*/ | ||
public static Boolean canTokenBeExpiration(String token) { | ||
Claims claims; | ||
try { | ||
claims = Jwts.parser() | ||
.setSigningKey(secret) | ||
.parseClaimsJws(token) | ||
.getBody(); | ||
final Date exp = claims.getExpiration(); | ||
if (exp.before(generateCurrentDate())) { | ||
return false; | ||
} | ||
return true; | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* refresh token | ||
* | ||
* @return token | ||
* @param token | ||
*/ | ||
public static String refreshToken(String token) { | ||
String refreshedToken; | ||
try { | ||
final Claims claims = Jwts.parser() | ||
.setSigningKey(secret) | ||
.parseClaimsJws(token) | ||
.getBody(); | ||
refreshedToken = generateToken(claims); | ||
} catch (Exception e) { | ||
refreshedToken = null; | ||
} | ||
return refreshedToken; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
(iterator != null && !iterator.hasNext())
better.