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 @@ -17,6 +17,7 @@

import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

/**
Expand All @@ -26,6 +27,7 @@
* @since 1.5.0
*/
@Component
@ConditionalOnProperty(name="auth.enabled", havingValue="false")
public class FakeAuthServiceImpl implements AuthService<HttpServletRequest> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.alibaba.csp.sentinel.dashboard.auth;

import org.springframework.context.annotation.Primary;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -25,8 +25,8 @@
* @author cdfive
* @since 1.6.0
*/
@Primary
@Component
@ConditionalOnProperty(name="auth.enabled", havingValue="true")
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 @@ -16,12 +16,14 @@
package com.alibaba.csp.sentinel.dashboard.controller;

import com.alibaba.csp.sentinel.dashboard.auth.AuthService;
import com.alibaba.csp.sentinel.dashboard.auth.FakeAuthServiceImpl;
import com.alibaba.csp.sentinel.dashboard.auth.SimpleWebAuthServiceImpl;
import com.alibaba.csp.sentinel.dashboard.config.DashboardConfig;
import com.alibaba.csp.sentinel.dashboard.domain.Result;
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;
Expand All @@ -46,6 +48,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 Down Expand Up @@ -77,4 +82,16 @@ public Result<?> logout(HttpServletRequest request) {
request.getSession().invalidate();
return Result.ofSuccess(null);
}

@RequestMapping(value = "/check")
cdfive marked this conversation as resolved.
Show resolved Hide resolved
public Result<?> check(HttpServletRequest request) {
AuthService.AuthUser authUser = authService.getAuthUser(request);
if (authUser == null) {
return Result.ofFail(-1, "Not logged in");
}
if (authService instanceof FakeAuthServiceImpl || request.getSession().getAttribute(SimpleWebAuthServiceImpl.WEB_SESSION_KEY) == null) {
request.getSession().setAttribute(SimpleWebAuthServiceImpl.WEB_SESSION_KEY, authUser);
cdfive marked this conversation as resolved.
Show resolved Hide resolved
}
return Result.ofSuccess(authUser);
}
}
2 changes: 2 additions & 0 deletions sentinel-dashboard/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ 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.enabled=true
auth.username=sentinel
auth.password=sentinel

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 id="li-logout">
<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 @@ -12,7 +12,20 @@ angular.module('sentinelDashboardApp')
replace: true,
controller: function ($scope, $state, $window, AuthService) {
if (!$window.localStorage.getItem('session_sentinel_admin')) {
$state.go('login');
AuthService.check().success(function (data) {
if (data.code == 0) {
$window.localStorage.setItem('session_sentinel_admin', {
username: data.data
});
if (data.data.id == 'FAKE_EMP_ID') {
document.getElementById('li-logout').style.display = 'none';
} else {
document.getElementById('li-logout').style.display = 'block';
cdfive marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
$state.go('login');
}
});
}

$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