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

sentinel console disable login #1004

Merged
merged 13 commits into from
Aug 26, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package com.alibaba.csp.sentinel.dashboard.auth;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
* A fake AuthService implementation, which will pass all user auth checking.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,8 +26,9 @@
* @author cdfive
* @since 1.6.0
*/
@Primary
@Component
@Primary
@ConditionalOnProperty(name = "auth.enabled", matchIfMissing = true)
cdfive marked this conversation as resolved.
Show resolved Hide resolved
public class SimpleWebAuthServiceImpl implements AuthService<HttpServletRequest> {
cdfive marked this conversation as resolved.
Show resolved Hide resolved

public static final String WEB_SESSION_KEY = "session_sentinel_admin";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,6 +46,9 @@ public class AuthController {
@Value("${auth.password:sentinel}")
private String authPassword;

@Autowired
private AuthService<HttpServletRequest> authService;

@PostMapping("/login")
public Result<AuthService.AuthUser> login(HttpServletRequest request, String username, String password) {
if (StringUtils.isNotBlank(DashboardConfig.getAuthUsername())) {
Expand All @@ -72,9 +75,18 @@ public Result<AuthService.AuthUser> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
cdfive marked this conversation as resolved.
Show resolved Hide resolved
auth.username=sentinel
auth.password=sentinel

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ app.controller('LoginCtl', ['$scope', '$state', '$window', 'AuthService',

AuthService.login(param).success(function (data) {
if (data.code == 0) {
$window.localStorage.setItem('session_sentinel_admin', {
$window.localStorage.setItem('session_sentinel_admin', JSON.stringify({
username: data.data
});
}));

$state.go('dashboard');
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<span style="color: #fff;font-size: 26px;">Sentinel 控制台</span>
</div>
<ul class="nav navbar-nav navbar-right">
<li>
<li ng-show="showLogout">
<a href="javascript:void(0);" ng-click="logout()"
style="margin: 3px 15px 0 0;"><span class="glyphicon glyphicon-log-out"></span>&nbsp;注销</a>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,27 @@ angular.module('sentinelDashboardApp')
restrict: 'E',
replace: true,
controller: function ($scope, $state, $window, AuthService) {
if (!$window.localStorage.getItem('session_sentinel_admin')) {
$state.go('login');
if (!$window.localStorage.getItem("session_sentinel_admin")) {
AuthService.check().success(function (data) {
if (data.code == 0) {
$window.localStorage.setItem('session_sentinel_admin', JSON.stringify({
username: data.data
}));
handleLogout($scope, data.data.id)
} else {
$state.go('login');
}
});
} else {
handleLogout($scope, JSON.parse($window.localStorage.getItem("session_sentinel_admin")).username.id)
}

function handleLogout($scope, id) {
if (id == 'FAKE_EMP_ID') {
$scope.showLogout = false;
} else {
$scope.showLogout = true;
}
}

$scope.logout = function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
var app = angular.module('sentinelDashboardApp');

app.service('AuthService', ['$http', function ($http) {
this.check = function () {
return $http({
url: '/auth/check',
method: 'POST'
});
};

this.login = function (param) {
return $http({
url: '/auth/login',
Expand Down