diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/auth/SimpleWebAuthServiceImpl.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/auth/SimpleWebAuthServiceImpl.java index 4de6898c9e..5d9599ee96 100644 --- a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/auth/SimpleWebAuthServiceImpl.java +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/auth/SimpleWebAuthServiceImpl.java @@ -15,6 +15,7 @@ */ package com.alibaba.csp.sentinel.dashboard.auth; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; @@ -25,8 +26,9 @@ * @author cdfive * @since 1.6.0 */ -@Primary @Component +@Primary +@ConditionalOnProperty(name = "auth.enabled", matchIfMissing = true) public class SimpleWebAuthServiceImpl implements AuthService { public static final String WEB_SESSION_KEY = "session_sentinel_admin"; diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/AuthController.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/AuthController.java index e8f3f74871..8b01aed232 100644 --- a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/AuthController.java +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/AuthController.java @@ -22,10 +22,10 @@ import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; @@ -46,6 +46,9 @@ public class AuthController { @Value("${auth.password:sentinel}") private String authPassword; + @Autowired + private AuthService authService; + @PostMapping("/login") public Result login(HttpServletRequest request, String username, String password) { if (StringUtils.isNotBlank(DashboardConfig.getAuthUsername())) { @@ -72,9 +75,18 @@ public Result login(HttpServletRequest request, String use return Result.ofSuccess(authUser); } - @RequestMapping(value = "/logout", method = RequestMethod.POST) + @PostMapping(value = "/logout") public Result logout(HttpServletRequest request) { request.getSession().invalidate(); return Result.ofSuccess(null); } + + @PostMapping(value = "/check") + public Result check(HttpServletRequest request) { + AuthService.AuthUser authUser = authService.getAuthUser(request); + if (authUser == null) { + return Result.ofFail(-1, "Not logged in"); + } + return Result.ofSuccess(authUser); + } } diff --git a/sentinel-dashboard/src/main/resources/application.properties b/sentinel-dashboard/src/main/resources/application.properties index edab1027aa..a2f84dec89 100755 --- a/sentinel-dashboard/src/main/resources/application.properties +++ b/sentinel-dashboard/src/main/resources/application.properties @@ -12,6 +12,7 @@ logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - % #auth settings 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 +# If auth.enabled=false, Sentinel console disable login auth.username=sentinel auth.password=sentinel diff --git a/sentinel-dashboard/src/main/webapp/resources/app/scripts/controllers/login.js b/sentinel-dashboard/src/main/webapp/resources/app/scripts/controllers/login.js index 1230129c83..3d49d3c16f 100644 --- a/sentinel-dashboard/src/main/webapp/resources/app/scripts/controllers/login.js +++ b/sentinel-dashboard/src/main/webapp/resources/app/scripts/controllers/login.js @@ -22,10 +22,7 @@ app.controller('LoginCtl', ['$scope', '$state', '$window', 'AuthService', AuthService.login(param).success(function (data) { if (data.code == 0) { - $window.localStorage.setItem('session_sentinel_admin', { - username: data.data - }); - + $window.localStorage.setItem('session_sentinel_admin', JSON.stringify(data.data)); $state.go('dashboard'); } else { alert(data.msg); diff --git a/sentinel-dashboard/src/main/webapp/resources/app/scripts/directives/header/header.html b/sentinel-dashboard/src/main/webapp/resources/app/scripts/directives/header/header.html index 744e731805..baf239ee41 100755 --- a/sentinel-dashboard/src/main/webapp/resources/app/scripts/directives/header/header.html +++ b/sentinel-dashboard/src/main/webapp/resources/app/scripts/directives/header/header.html @@ -4,7 +4,7 @@ Sentinel 控制台