Skip to content

Commit

Permalink
[Fix][admin] Fix token invalid exception (#3091)
Browse files Browse the repository at this point in the history
Co-authored-by: Zzm0809 <[email protected]>
  • Loading branch information
Zzm0809 and Zzm0809 authored Jan 30, 2024
1 parent fa3ca0d commit c1f96f0
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 58 deletions.
12 changes: 1 addition & 11 deletions .github/workflows/pr-reviewer-assign.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,4 @@ jobs:
token: ${{ secrets.GITHUB_TOKEN }}
pr-emoji: '+1, rocket'
reviewers: 'zackyoungh,gaoyan1998,Zzm0809,aiwenmo'
review-creator: false


- name: Add project
if: "github.event_name == 'pull_request_target'"
uses: alex-page/[email protected]
with:
project: Dinky Roadmap
column: RoadMap
repo-token: ${{ secrets.GITHUB_TOKEN }}
action: add
review-creator: false
4 changes: 4 additions & 0 deletions dinky-admin/src/main/java/org/dinky/data/result/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ public static <T> Result<T> authorizeFailed(Status status) {
return of(null, CodeEnum.AUTHORIZE_ERROR.getCode(), status.getMessage());
}

public static <T> Result<T> authorizeFailed(Status status, Object... args) {
return of(null, CodeEnum.AUTHORIZE_ERROR.getCode(), MessageFormat.format(status.getMessage(), args));
}

public static <T> Result<T> authorizeFailed(String msg) {
return of(null, CodeEnum.AUTHORIZE_ERROR.getCode(), msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@
import org.springframework.web.servlet.AsyncHandlerInterceptor;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Opt;
import lombok.extern.slf4j.Slf4j;

/** tenant interceptor */
/**
* tenant interceptor
*/
@Slf4j
public class TenantInterceptor implements AsyncHandlerInterceptor {

Expand All @@ -56,13 +59,18 @@ public boolean preHandle(HttpServletRequest request, @NotNull HttpServletRespons
if (Asserts.isNotNull(cookies)) {
for (Cookie cookie : cookies) {
switch (cookie.getName()) {
case "satoken":
case "token":
token = Opt.ofBlankAble(cookie.getValue());
if (SaManager.getSaTokenDao().get("satoken:login:token:" + token.get()) != null) {
SaTokenDao saTokenDao = SaManager.getSaTokenDao();
String keyTokenValue = StpUtil.getStpLogic().splicingKeyTokenValue(token.get());
if (saTokenDao.get(keyTokenValue) != null) {
isPass = true;
}
break;
case "tenantId":
if (!StpUtil.isLogin()) {
return false;
}
UserDTO userInfo = UserInfoContextHolder.get(StpUtil.getLoginIdAsInt());
if (Asserts.isNull(userInfo)) {
StpUtil.logout(StpUtil.getLoginIdAsInt());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public User authenticate(LoginDTO loginDTO) throws AuthException {
if (result.size() == 0) {
log.info(String.format(
"No results found for search, base: '%s'; filter: '%s'", configuration.getLdapBaseDn(), filter));
throw new AuthException(Status.USER_NOT_EXIST);
throw new AuthException(Status.USER_NOT_EXIST, loginDTO.getUsername());
} else if (result.size() > 1) {
log.error(String.format(
"IncorrectResultSize, base: '%s'; filter: '%s'", configuration.getLdapBaseDn(), filter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
* UserServiceImpl
Expand All @@ -83,6 +84,7 @@
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implements UserService {

private static final String DEFAULT_PASSWORD = "123456";
Expand Down Expand Up @@ -141,7 +143,7 @@ public Boolean modifyUser(User user) {
public Result<Void> modifyPassword(ModifyPasswordDTO modifyPasswordDTO) {
User user = getById(modifyPasswordDTO.getId());
if (Asserts.isNull(user)) {
return Result.failed(Status.USER_NOT_EXIST);
return Result.authorizeFailed(Status.USER_NOT_EXIST, modifyPasswordDTO.getUsername());
}
if (!Asserts.isEquals(SaSecureUtil.md5(modifyPasswordDTO.getPassword()), user.getPassword())) {
return Result.failed(Status.USER_OLD_PASSWORD_INCORRECT);
Expand Down Expand Up @@ -180,7 +182,7 @@ public Result<UserDTO> loginUser(LoginDTO loginDTO) {
user = loginDTO.isLdapLogin() ? ldapLogin(loginDTO) : localLogin(loginDTO);
} catch (AuthException e) {
// Handle authentication exceptions and return the corresponding error status
return Result.authorizeFailed(e.getStatus() + e.getMessage());
return Result.authorizeFailed(Status.USER_NOT_EXIST, loginDTO.getUsername());

This comment has been minimized.

Copy link
@liyichencc

liyichencc Mar 5, 2024

这里这样改会造成所有的AuthException异常,例如用户密码输入错误等等,都返回用户不存在的提示吧。

}

// Check if the user is enabled
Expand Down Expand Up @@ -247,7 +249,7 @@ private User localLogin(LoginDTO loginDTO) throws AuthException {
User user = getUserByUsername(loginDTO.getUsername());
if (Asserts.isNull(user)) {
// User doesn't exist
throw new AuthException(Status.USER_NOT_EXIST);
throw new AuthException(Status.USER_NOT_EXIST, loginDTO.getUsername());
}

String userPassword = user.getPassword();
Expand Down Expand Up @@ -464,8 +466,14 @@ public List<User> getUserListByTenantId(int id) {
userTenantService.list(new LambdaQueryWrapper<UserTenant>().eq(UserTenant::getTenantId, id));
userTenants.forEach(userTenant -> {
User user = getById(userTenant.getUserId());
user.setTenantAdminFlag(userTenant.getTenantAdminFlag());
userList.add(user);
if (!Asserts.isNull(user)) {
user.setTenantAdminFlag(userTenant.getTenantAdminFlag());
userList.add(user);
} else {
log.error(
"Unable to obtain user information, the user may have been deleted, please contact the administrator to verify, userId:[{}]",
userTenant.getUserId());
}
});
return userList;
}
Expand Down
7 changes: 4 additions & 3 deletions dinky-admin/src/main/resources/application-pgsql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

spring:
datasource:
username: postgres
password: dinky
username: ${POSTGRES_USER:dinky}
password: ${POSTGRES_PASSWORD:dinky}
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/dinky?stringtype=unspecified
# The POSTGRES_ADDR variable is not officially provided. If you use docker, please manually add an env to support it.
url: jdbc:postgresql://${POSTGRES_ADDR:localhost:5432}/${POSTGRES_DB:dinky}?stringtype=unspecified
40 changes: 7 additions & 33 deletions dinky-admin/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,9 @@ spring:
max-file-size: 524288000
max-request-size: 524288000


# By default, memory cache metadata information is used,
# dinky supports redis cache, if necessary, please change simple to redis, and open the following redis connection configuration
# Sub-configuration items can be opened or customized as needed
cache:
type: simple
# If type is configured as redis, this item can be configured as needed, note: Pay attention to the indentation of this configuration item
# redis:
# # Whether to cache empty values, save the default
# cache-null-values: false
# # Cache expiration time, default 24 hours
# time-to-live: 86400

########################################################## Redis配置 ##########################################################
# If sa-token needs to rely on redis, please open the redis configuration and depend on pom.xml and dinky-admin/pom.xml, and configure redis connection information in application.yml
# note: pay attention to the indentation of this configuration item
########################################################## Redis配置 ##########################################################
# If sa-token needs to rely on redis, please open the redis configuration and depend on pom.xml and dinky-admin/pom.xml, and configure redis connection information in application.yml
# note: pay attention to the indentation of this configuration item
# redis:
# host: localhost
# port: 6379
Expand Down Expand Up @@ -102,21 +89,8 @@ mybatis-plus:
#################################################################################################################
################################################# SMS Config ####################################################
#################################################################################################################
#sms:
# # Whether to enable SMS
# config-type: sql_config
# is-print: false
# sql:
# # The database connection information
# url: ${spring.datasource.url}
# username: ${spring.datasource.username}
# password: ${spring.datasource.password}
# driver-class-name: ${spring.datasource.driver-class-name}
# table-name: dinky_alert_instance
# supplier-field-name: manufacturers
# config-name: params
# start-name: enabled
# is-start: 1
sms:
is-print: false



Expand Down Expand Up @@ -147,6 +121,7 @@ sa-token:
# is read header
is-read-header: true
token-name: token
is-read-cookie: true

#################################################################################################################
################################################# knife4j Config ################################################
Expand All @@ -156,8 +131,7 @@ knife4j:
setting:
language: en

sms:
is-print: false


#################################################################################################################
################################################# Crypto Config #################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import org.dinky.data.enums.Status;

import java.text.MessageFormat;

import lombok.Data;

/**
Expand All @@ -42,4 +44,9 @@ public AuthException(Throwable cause, Status status) {
super(status.getMessage(), cause);
this.status = status;
}

public AuthException(Status status, Object... args) {
super(MessageFormat.format(status.getMessage(), args));
this.status = status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test.msg.job.log.url=Click to view the exception log for this task
user.assign.role.success=User Assign Role Success
global.params.check.error.value=Field: {0}, Illegal Value: {1}
change.password.success=Change Password Success
user.not.exist=User Not Exist
user.not.exist=User:{0} Not Exist
refresh.success=Refresh Successfully
ds.get.node.list.error=Get Node List Error
ldap.default.tenant.nofound=The LDAP default tenant does not exist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test.msg.job.log.url=点击查看该任务的异常日志
user.assign.role.success=用户分配角色成功
global.params.check.error.value=字段: {0}, 不合法的值: {1}
change.password.success=修改密码成功
user.not.exist=用户不存在
user.not.exist=用户:{0} 不存在
refresh.success=刷新成功
ds.get.node.list.error=节点获取失败
ldap.default.tenant.nofound=LDAP默认租户不存在
Expand Down

0 comments on commit c1f96f0

Please sign in to comment.