Browse Source

dashboard: code refinement for version and auth API

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 5 years ago
parent
commit
281d3420ca
4 changed files with 22 additions and 18 deletions
  1. +2
    -2
      sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/auth/SimpleWebAuthServiceImpl.java
  2. +5
    -5
      sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/AuthController.java
  3. +12
    -9
      sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/VersionController.java
  4. +3
    -2
      sentinel-dashboard/src/main/resources/application.properties

+ 2
- 2
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/auth/SimpleWebAuthServiceImpl.java View File

@@ -29,12 +29,12 @@ import javax.servlet.http.HttpSession;
@Component @Component
public class SimpleWebAuthServiceImpl implements AuthService<HttpServletRequest> { 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 @Override
public AuthUser getAuthUser(HttpServletRequest request) { public AuthUser getAuthUser(HttpServletRequest request) {
HttpSession session = request.getSession(); 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) { if (sentinelUserObj != null && sentinelUserObj instanceof AuthUser) {
return (AuthUser) sentinelUserObj; return (AuthUser) sentinelUserObj;
} }


+ 5
- 5
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/AuthController.java View File

@@ -38,7 +38,7 @@ import javax.servlet.http.HttpServletRequest;
@RequestMapping("/auth") @RequestMapping("/auth")
public class AuthController { public class AuthController {


private static Logger LOGGER = LoggerFactory.getLogger(AuthController.class);
private static final Logger LOGGER = LoggerFactory.getLogger(AuthController.class);


@Value("${auth.username:sentinel}") @Value("${auth.username:sentinel}")
private String authUsername; private String authUsername;
@@ -47,7 +47,7 @@ public class AuthController {
private String authPassword; private String authPassword;


@PostMapping("/login") @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())) { if (StringUtils.isNotBlank(DashboardConfig.getAuthUsername())) {
authUsername = DashboardConfig.getAuthUsername(); authUsername = DashboardConfig.getAuthUsername();
} }
@@ -63,17 +63,17 @@ public class AuthController {
*/ */
if (StringUtils.isNotBlank(authUsername) && !authUsername.equals(username) if (StringUtils.isNotBlank(authUsername) && !authUsername.equals(username)
|| StringUtils.isNotBlank(authPassword) && !authPassword.equals(password)) { || 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"); return Result.ofFail(-1, "Invalid username or password");
} }


AuthService.AuthUser authUser = new SimpleWebAuthServiceImpl.SimpleWebAuthUserImpl(username); 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); return Result.ofSuccess(authUser);
} }


@RequestMapping(value = "/logout", method = RequestMethod.POST) @RequestMapping(value = "/logout", method = RequestMethod.POST)
public Result logout(HttpServletRequest request) {
public Result<?> logout(HttpServletRequest request) {
request.getSession().invalidate(); request.getSession().invalidate();
return Result.ofSuccess(null); return Result.ofSuccess(null);
} }


+ 12
- 9
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/VersionController.java View File

@@ -16,31 +16,34 @@
package com.alibaba.csp.sentinel.dashboard.controller; package com.alibaba.csp.sentinel.dashboard.controller;


import com.alibaba.csp.sentinel.dashboard.domain.Result; 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.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; import org.springframework.web.bind.annotation.RestController;


/** /**
* @author hisenyuan * @author hisenyuan
* @date 2019-05-30 10:47:50
* @since 1.7.0
*/ */
@RestController @RestController
@RequestMapping(value = "/")
public class VersionController { public class VersionController {
@Value("${sentinel.dashboard.version:''}")

private static final String VERSION_PATTERN = "-";

@Value("${sentinel.dashboard.version:}")
private String sentinelDashboardVersion; private String sentinelDashboardVersion;
private static String VERSION_PATTERN = "-";


@RequestMapping(value = "/getVersion")
public Result<String> getVersion() {
if (sentinelDashboardVersion != null) {
@GetMapping("/version")
public Result<String> apiGetVersion() {
if (StringUtil.isNotBlank(sentinelDashboardVersion)) {
String res = sentinelDashboardVersion; String res = sentinelDashboardVersion;
if (sentinelDashboardVersion.contains(VERSION_PATTERN)) { if (sentinelDashboardVersion.contains(VERSION_PATTERN)) {
res = sentinelDashboardVersion.substring(0, sentinelDashboardVersion.indexOf(VERSION_PATTERN)); res = sentinelDashboardVersion.substring(0, sentinelDashboardVersion.indexOf(VERSION_PATTERN));
} }
return Result.ofSuccess(res); return Result.ofSuccess(res);
} else { } else {
return Result.ofFail(1, "getVersion failed");
return Result.ofFail(1, "getVersion failed: empty version");
} }
} }
} }

+ 3
- 2
sentinel-dashboard/src/main/resources/application.properties View File

@@ -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 #logging.pattern.console= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n


#auth settings #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.filter.exclude-url-suffixes=htm,html,js,css,map,ico,ttf,woff,png
auth.username=sentinel auth.username=sentinel
auth.password=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} sentinel.dashboard.version=${project.version}

Loading…
Cancel
Save