Unverified Commit c1f96f07 authored by Zzm0809's avatar Zzm0809 Committed by GitHub

[Fix][admin] Fix token invalid exception (#3091)

parent fa3ca0d2
...@@ -23,13 +23,3 @@ jobs: ...@@ -23,13 +23,3 @@ jobs:
pr-emoji: '+1, rocket' pr-emoji: '+1, rocket'
reviewers: 'zackyoungh,gaoyan1998,Zzm0809,aiwenmo' reviewers: 'zackyoungh,gaoyan1998,Zzm0809,aiwenmo'
review-creator: false review-creator: false
- name: Add project
if: "github.event_name == 'pull_request_target'"
uses: alex-page/github-project-automation-plus@v0.8.1
with:
project: Dinky Roadmap
column: RoadMap
repo-token: ${{ secrets.GITHUB_TOKEN }}
action: add
...@@ -203,6 +203,10 @@ public class Result<T> implements Serializable { ...@@ -203,6 +203,10 @@ public class Result<T> implements Serializable {
return of(null, CodeEnum.AUTHORIZE_ERROR.getCode(), status.getMessage()); 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) { public static <T> Result<T> authorizeFailed(String msg) {
return of(null, CodeEnum.AUTHORIZE_ERROR.getCode(), msg); return of(null, CodeEnum.AUTHORIZE_ERROR.getCode(), msg);
} }
......
...@@ -39,11 +39,14 @@ import org.jetbrains.annotations.NotNull; ...@@ -39,11 +39,14 @@ import org.jetbrains.annotations.NotNull;
import org.springframework.web.servlet.AsyncHandlerInterceptor; import org.springframework.web.servlet.AsyncHandlerInterceptor;
import cn.dev33.satoken.SaManager; import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Opt; import cn.hutool.core.lang.Opt;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
/** tenant interceptor */ /**
* tenant interceptor
*/
@Slf4j @Slf4j
public class TenantInterceptor implements AsyncHandlerInterceptor { public class TenantInterceptor implements AsyncHandlerInterceptor {
...@@ -56,13 +59,18 @@ public class TenantInterceptor implements AsyncHandlerInterceptor { ...@@ -56,13 +59,18 @@ public class TenantInterceptor implements AsyncHandlerInterceptor {
if (Asserts.isNotNull(cookies)) { if (Asserts.isNotNull(cookies)) {
for (Cookie cookie : cookies) { for (Cookie cookie : cookies) {
switch (cookie.getName()) { switch (cookie.getName()) {
case "satoken": case "token":
token = Opt.ofBlankAble(cookie.getValue()); 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; isPass = true;
} }
break; break;
case "tenantId": case "tenantId":
if (!StpUtil.isLogin()) {
return false;
}
UserDTO userInfo = UserInfoContextHolder.get(StpUtil.getLoginIdAsInt()); UserDTO userInfo = UserInfoContextHolder.get(StpUtil.getLoginIdAsInt());
if (Asserts.isNull(userInfo)) { if (Asserts.isNull(userInfo)) {
StpUtil.logout(StpUtil.getLoginIdAsInt()); StpUtil.logout(StpUtil.getLoginIdAsInt());
......
...@@ -73,7 +73,7 @@ public class LdapServiceImpl implements LdapService { ...@@ -73,7 +73,7 @@ public class LdapServiceImpl implements LdapService {
if (result.size() == 0) { if (result.size() == 0) {
log.info(String.format( log.info(String.format(
"No results found for search, base: '%s'; filter: '%s'", configuration.getLdapBaseDn(), filter)); "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) { } else if (result.size() > 1) {
log.error(String.format( log.error(String.format(
"IncorrectResultSize, base: '%s'; filter: '%s'", configuration.getLdapBaseDn(), filter)); "IncorrectResultSize, base: '%s'; filter: '%s'", configuration.getLdapBaseDn(), filter));
......
...@@ -75,6 +75,7 @@ import cn.hutool.core.date.DateUtil; ...@@ -75,6 +75,7 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/** /**
* UserServiceImpl * UserServiceImpl
...@@ -83,6 +84,7 @@ import lombok.RequiredArgsConstructor; ...@@ -83,6 +84,7 @@ import lombok.RequiredArgsConstructor;
*/ */
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@Slf4j
public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implements UserService { public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implements UserService {
private static final String DEFAULT_PASSWORD = "123456"; private static final String DEFAULT_PASSWORD = "123456";
...@@ -141,7 +143,7 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen ...@@ -141,7 +143,7 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen
public Result<Void> modifyPassword(ModifyPasswordDTO modifyPasswordDTO) { public Result<Void> modifyPassword(ModifyPasswordDTO modifyPasswordDTO) {
User user = getById(modifyPasswordDTO.getId()); User user = getById(modifyPasswordDTO.getId());
if (Asserts.isNull(user)) { 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())) { if (!Asserts.isEquals(SaSecureUtil.md5(modifyPasswordDTO.getPassword()), user.getPassword())) {
return Result.failed(Status.USER_OLD_PASSWORD_INCORRECT); return Result.failed(Status.USER_OLD_PASSWORD_INCORRECT);
...@@ -180,7 +182,7 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen ...@@ -180,7 +182,7 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen
user = loginDTO.isLdapLogin() ? ldapLogin(loginDTO) : localLogin(loginDTO); user = loginDTO.isLdapLogin() ? ldapLogin(loginDTO) : localLogin(loginDTO);
} catch (AuthException e) { } catch (AuthException e) {
// Handle authentication exceptions and return the corresponding error status // 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());
} }
// Check if the user is enabled // Check if the user is enabled
...@@ -247,7 +249,7 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen ...@@ -247,7 +249,7 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen
User user = getUserByUsername(loginDTO.getUsername()); User user = getUserByUsername(loginDTO.getUsername());
if (Asserts.isNull(user)) { if (Asserts.isNull(user)) {
// User doesn't exist // User doesn't exist
throw new AuthException(Status.USER_NOT_EXIST); throw new AuthException(Status.USER_NOT_EXIST, loginDTO.getUsername());
} }
String userPassword = user.getPassword(); String userPassword = user.getPassword();
...@@ -464,8 +466,14 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen ...@@ -464,8 +466,14 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen
userTenantService.list(new LambdaQueryWrapper<UserTenant>().eq(UserTenant::getTenantId, id)); userTenantService.list(new LambdaQueryWrapper<UserTenant>().eq(UserTenant::getTenantId, id));
userTenants.forEach(userTenant -> { userTenants.forEach(userTenant -> {
User user = getById(userTenant.getUserId()); User user = getById(userTenant.getUserId());
if (!Asserts.isNull(user)) {
user.setTenantAdminFlag(userTenant.getTenantAdminFlag()); user.setTenantAdminFlag(userTenant.getTenantAdminFlag());
userList.add(user); 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; return userList;
} }
......
...@@ -17,7 +17,8 @@ ...@@ -17,7 +17,8 @@
spring: spring:
datasource: datasource:
username: postgres username: ${POSTGRES_USER:dinky}
password: dinky password: ${POSTGRES_PASSWORD:dinky}
driver-class-name: org.postgresql.Driver 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.
\ No newline at end of file url: jdbc:postgresql://${POSTGRES_ADDR:localhost:5432}/${POSTGRES_DB:dinky}?stringtype=unspecified
\ No newline at end of file
...@@ -44,22 +44,9 @@ spring: ...@@ -44,22 +44,9 @@ spring:
max-file-size: 524288000 max-file-size: 524288000
max-request-size: 524288000 max-request-size: 524288000
########################################################## Redis配置 ##########################################################
# By default, memory cache metadata information is used, # 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
# dinky supports redis cache, if necessary, please change simple to redis, and open the following redis connection configuration # note: pay attention to the indentation of this configuration item
# 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: # redis:
# host: localhost # host: localhost
# port: 6379 # port: 6379
...@@ -102,21 +89,8 @@ mybatis-plus: ...@@ -102,21 +89,8 @@ mybatis-plus:
################################################################################################################# #################################################################################################################
################################################# SMS Config #################################################### ################################################# SMS Config ####################################################
################################################################################################################# #################################################################################################################
#sms: sms:
# # Whether to enable SMS is-print: false
# 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
...@@ -147,6 +121,7 @@ sa-token: ...@@ -147,6 +121,7 @@ sa-token:
# is read header # is read header
is-read-header: true is-read-header: true
token-name: token token-name: token
is-read-cookie: true
################################################################################################################# #################################################################################################################
################################################# knife4j Config ################################################ ################################################# knife4j Config ################################################
...@@ -156,8 +131,7 @@ knife4j: ...@@ -156,8 +131,7 @@ knife4j:
setting: setting:
language: en language: en
sms:
is-print: false
################################################################################################################# #################################################################################################################
################################################# Crypto Config ################################################# ################################################# Crypto Config #################################################
......
...@@ -21,6 +21,8 @@ package org.dinky.data.exception; ...@@ -21,6 +21,8 @@ package org.dinky.data.exception;
import org.dinky.data.enums.Status; import org.dinky.data.enums.Status;
import java.text.MessageFormat;
import lombok.Data; import lombok.Data;
/** /**
...@@ -42,4 +44,9 @@ public class AuthException extends Exception { ...@@ -42,4 +44,9 @@ public class AuthException extends Exception {
super(status.getMessage(), cause); super(status.getMessage(), cause);
this.status = status; this.status = status;
} }
public AuthException(Status status, Object... args) {
super(MessageFormat.format(status.getMessage(), args));
this.status = status;
}
} }
...@@ -34,7 +34,7 @@ test.msg.job.log.url=Click to view the exception log for this task ...@@ -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 user.assign.role.success=User Assign Role Success
global.params.check.error.value=Field: {0}, Illegal Value: {1} global.params.check.error.value=Field: {0}, Illegal Value: {1}
change.password.success=Change Password Success change.password.success=Change Password Success
user.not.exist=User Not Exist user.not.exist=User:{0} Not Exist
refresh.success=Refresh Successfully refresh.success=Refresh Successfully
ds.get.node.list.error=Get Node List Error ds.get.node.list.error=Get Node List Error
ldap.default.tenant.nofound=The LDAP default tenant does not exist ldap.default.tenant.nofound=The LDAP default tenant does not exist
......
...@@ -34,7 +34,7 @@ test.msg.job.log.url=点击查看该任务的异常日志 ...@@ -34,7 +34,7 @@ test.msg.job.log.url=点击查看该任务的异常日志
user.assign.role.success=用户分配角色成功 user.assign.role.success=用户分配角色成功
global.params.check.error.value=字段: {0}, 不合法的值: {1} global.params.check.error.value=字段: {0}, 不合法的值: {1}
change.password.success=修改密码成功 change.password.success=修改密码成功
user.not.exist=用户不存在 user.not.exist=用户:{0} 不存在
refresh.success=刷新成功 refresh.success=刷新成功
ds.get.node.list.error=节点获取失败 ds.get.node.list.error=节点获取失败
ldap.default.tenant.nofound=LDAP默认租户不存在 ldap.default.tenant.nofound=LDAP默认租户不存在
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment