forked from alibaba/Sentinel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dashboard: Add a simple login page to support basic auth (alibaba#659)
- Add `AuthController` and `SimpleWebAuthServiceImpl` - Update `AuthFilter` - Add a simple login page and frontend interceptor to support auth check and session storage
- Loading branch information
Showing
18 changed files
with
514 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...board/src/main/java/com/alibaba/csp/sentinel/dashboard/auth/SimpleWebAuthServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.csp.sentinel.dashboard.auth; | ||
|
||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpSession; | ||
|
||
/** | ||
* @author cdfive | ||
* @since 1.6.0 | ||
*/ | ||
@Primary | ||
@Component | ||
public class SimpleWebAuthServiceImpl implements AuthService<HttpServletRequest> { | ||
|
||
public static final String WEB_SESSTION_KEY = "session_sentinel_admin"; | ||
|
||
@Override | ||
public AuthUser getAuthUser(HttpServletRequest request) { | ||
HttpSession session = request.getSession(); | ||
Object sentinelUserObj = session.getAttribute(SimpleWebAuthServiceImpl.WEB_SESSTION_KEY); | ||
if (sentinelUserObj != null && sentinelUserObj instanceof AuthUser) { | ||
return (AuthUser) sentinelUserObj; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static final class SimpleWebAuthUserImpl implements AuthUser { | ||
|
||
private String username; | ||
|
||
public SimpleWebAuthUserImpl(String username) { | ||
this.username = username; | ||
} | ||
|
||
@Override | ||
public boolean authTarget(String target, PrivilegeType privilegeType) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isSuperUser() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public String getNickName() { | ||
return username; | ||
} | ||
|
||
@Override | ||
public String getLoginName() { | ||
return username; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return username; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.csp.sentinel.dashboard.controller; | ||
|
||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService; | ||
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.Value; | ||
import org.springframework.http.MediaType; | ||
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; | ||
|
||
/** | ||
* @author cdfive | ||
* @since 1.6.0 | ||
*/ | ||
@RestController | ||
@RequestMapping(value = "/auth", produces = MediaType.APPLICATION_JSON_VALUE) | ||
public class AuthController { | ||
|
||
private static Logger LOGGER = LoggerFactory.getLogger(AuthController.class); | ||
|
||
@Value("${auth.username:sentinel}") | ||
private String authUsername; | ||
|
||
@Value("${auth.password:sentinel}") | ||
private String authPassword; | ||
|
||
@RequestMapping(value = "/login", method = RequestMethod.POST) | ||
public Result login(HttpServletRequest request, String username, String password) { | ||
if (StringUtils.isNotBlank(DashboardConfig.getAuthUsername())) { | ||
authUsername = DashboardConfig.getAuthUsername(); | ||
} | ||
|
||
if (StringUtils.isNotBlank(DashboardConfig.getAuthPassword())) { | ||
authPassword = DashboardConfig.getAuthPassword(); | ||
} | ||
|
||
/** | ||
* If auth.username or auth.password is blank(set in application.properties or VM arguments), | ||
* auth will pass, as the front side validate the input which can't be blank, | ||
* so user can input any username or password(both are not blank) to login in that case. | ||
*/ | ||
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); | ||
return Result.ofFail(-1, "Invalid username or password"); | ||
} | ||
|
||
AuthService.AuthUser authUser = new SimpleWebAuthServiceImpl.SimpleWebAuthUserImpl(username); | ||
request.getSession().setAttribute(SimpleWebAuthServiceImpl.WEB_SESSTION_KEY, authUser); | ||
return Result.ofSuccess(authUser); | ||
} | ||
|
||
@RequestMapping(value = "/logout", method = RequestMethod.POST) | ||
public Result logout(HttpServletRequest request) { | ||
request.getSession().invalidate(); | ||
return Result.ofSuccess(null); | ||
} | ||
} |
Oops, something went wrong.