dashboard: code refinement for version and auth API
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
parent
72276a3951
commit
281d3420ca
|
|
@ -29,12 +29,12 @@ import javax.servlet.http.HttpSession;
|
|||
@Component
|
||||
public class SimpleWebAuthServiceImpl implements AuthService<HttpServletRequest> {
|
||||
|
||||
public static final String WEB_SESSTION_KEY = "session_sentinel_admin";
|
||||
public static final String WEB_SESSION_KEY = "session_sentinel_admin";
|
||||
|
||||
@Override
|
||||
public AuthUser getAuthUser(HttpServletRequest request) {
|
||||
HttpSession session = request.getSession();
|
||||
Object sentinelUserObj = session.getAttribute(SimpleWebAuthServiceImpl.WEB_SESSTION_KEY);
|
||||
Object sentinelUserObj = session.getAttribute(SimpleWebAuthServiceImpl.WEB_SESSION_KEY);
|
||||
if (sentinelUserObj != null && sentinelUserObj instanceof AuthUser) {
|
||||
return (AuthUser) sentinelUserObj;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ import javax.servlet.http.HttpServletRequest;
|
|||
@RequestMapping("/auth")
|
||||
public class AuthController {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(AuthController.class);
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(AuthController.class);
|
||||
|
||||
@Value("${auth.username:sentinel}")
|
||||
private String authUsername;
|
||||
|
|
@ -47,7 +47,7 @@ public class AuthController {
|
|||
private String authPassword;
|
||||
|
||||
@PostMapping("/login")
|
||||
public Result login(HttpServletRequest request, String username, String password) {
|
||||
public Result<AuthService.AuthUser> login(HttpServletRequest request, String username, String password) {
|
||||
if (StringUtils.isNotBlank(DashboardConfig.getAuthUsername())) {
|
||||
authUsername = DashboardConfig.getAuthUsername();
|
||||
}
|
||||
|
|
@ -63,17 +63,17 @@ public class AuthController {
|
|||
*/
|
||||
if (StringUtils.isNotBlank(authUsername) && !authUsername.equals(username)
|
||||
|| StringUtils.isNotBlank(authPassword) && !authPassword.equals(password)) {
|
||||
LOGGER.error("Login failed: Invalid username or password, username=" + username + ", password=" + password);
|
||||
LOGGER.error("Login failed: Invalid username or password, username=" + username);
|
||||
return Result.ofFail(-1, "Invalid username or password");
|
||||
}
|
||||
|
||||
AuthService.AuthUser authUser = new SimpleWebAuthServiceImpl.SimpleWebAuthUserImpl(username);
|
||||
request.getSession().setAttribute(SimpleWebAuthServiceImpl.WEB_SESSTION_KEY, authUser);
|
||||
request.getSession().setAttribute(SimpleWebAuthServiceImpl.WEB_SESSION_KEY, authUser);
|
||||
return Result.ofSuccess(authUser);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/logout", method = RequestMethod.POST)
|
||||
public Result logout(HttpServletRequest request) {
|
||||
public Result<?> logout(HttpServletRequest request) {
|
||||
request.getSession().invalidate();
|
||||
return Result.ofSuccess(null);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,31 +16,34 @@
|
|||
package com.alibaba.csp.sentinel.dashboard.controller;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.Result;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author hisenyuan
|
||||
* @date 2019-05-30 10:47:50
|
||||
* @since 1.7.0
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(value = "/")
|
||||
public class VersionController {
|
||||
@Value("${sentinel.dashboard.version:''}")
|
||||
private String sentinelDashboardVersion;
|
||||
private static String VERSION_PATTERN = "-";
|
||||
|
||||
@RequestMapping(value = "/getVersion")
|
||||
public Result<String> getVersion() {
|
||||
if (sentinelDashboardVersion != null) {
|
||||
private static final String VERSION_PATTERN = "-";
|
||||
|
||||
@Value("${sentinel.dashboard.version:}")
|
||||
private String sentinelDashboardVersion;
|
||||
|
||||
@GetMapping("/version")
|
||||
public Result<String> apiGetVersion() {
|
||||
if (StringUtil.isNotBlank(sentinelDashboardVersion)) {
|
||||
String res = sentinelDashboardVersion;
|
||||
if (sentinelDashboardVersion.contains(VERSION_PATTERN)) {
|
||||
res = sentinelDashboardVersion.substring(0, sentinelDashboardVersion.indexOf(VERSION_PATTERN));
|
||||
}
|
||||
return Result.ofSuccess(res);
|
||||
} else {
|
||||
return Result.ofFail(1, "getVersion failed");
|
||||
return Result.ofFail(1, "getVersion failed: empty version");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,10 +10,11 @@ logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %
|
|||
#logging.pattern.console= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
|
||||
|
||||
#auth settings
|
||||
auth.filter.exclude-urls=/,/auth/login,/auth/logout,/registry/machine
|
||||
auth.filter.exclude-urls=/,/auth/login,/auth/logout,/registry/machine,/version
|
||||
auth.filter.exclude-url-suffixes=htm,html,js,css,map,ico,ttf,woff,png
|
||||
auth.username=sentinel
|
||||
auth.password=sentinel
|
||||
|
||||
# get the project version for index
|
||||
# Inject the dashboard version. It's required to enable
|
||||
# filtering in pom.xml for this resource file.
|
||||
sentinel.dashboard.version=${project.version}
|
||||
Loading…
Reference in New Issue