@@ -63,7 +63,9 @@ public class TokenServiceImpl implements TokenService {
63
63
private RedisUtil redisUtil ;
64
64
65
65
@ Override
66
- public String generateSalt (String username ) {
66
+ public String generateSalt (String username , String tenantName ) {
67
+ // todo 此处一个bug,会抛异常,导致无法记录失败登录次数
68
+ Tenant tenant = tenantService .selectByName (tenantName );
67
69
String redisSaltKey = CacheConstant .Entity .USER + CacheConstant .Suffix .SALT + CommonConstant .Symbol .SEPARATOR + username ;
68
70
String salt = redisUtil .getKey (redisSaltKey , String .class );
69
71
if (StrUtil .isBlank (salt )) {
@@ -74,43 +76,48 @@ public String generateSalt(String username) {
74
76
}
75
77
76
78
@ Override
77
- public String generateToken (String tenant , String name , String salt , String password ) {
78
- checkUserLimit (name );
79
- Tenant tempTenant = tenantService .selectByName (tenant );
80
- User tempUser = userService .selectByName (name , false );
81
- if (tempTenant .getEnable () && tempUser .getEnable ()) {
82
- tenantBindService .selectByTenantIdAndUserId (tempTenant .getId (), tempUser .getId ());
83
- String redisSaltKey = CacheConstant .Entity .USER + CacheConstant .Suffix .SALT + CommonConstant .Symbol .SEPARATOR + name ;
84
- String tempSalt = redisUtil .getKey (redisSaltKey , String .class );
85
- if (StrUtil .isNotEmpty (tempSalt ) && tempSalt .equals (salt )) {
86
- if (Dc3Util .md5 (tempUser .getPassword () + tempSalt ).equals (password )) {
87
- String redisTokenKey = CacheConstant .Entity .USER + CacheConstant .Suffix .TOKEN + CommonConstant .Symbol .SEPARATOR + name ;
88
- String token = KeyUtil .generateToken (name , tempSalt );
79
+ public String generateToken (String username , String salt , String password , String tenantName ) {
80
+ checkUserLimit (username );
81
+ // todo 此处一个bug,会抛异常,导致无法记录失败登录次数
82
+ Tenant tenant = tenantService .selectByName (tenantName );
83
+ User user = userService .selectByName (username , false );
84
+ if (tenant .getEnable () && user .getEnable ()) {
85
+ tenantBindService .selectByTenantIdAndUserId (tenant .getId (), user .getId ());
86
+ String redisSaltKey = CacheConstant .Entity .USER + CacheConstant .Suffix .SALT + CommonConstant .Symbol .SEPARATOR + username ;
87
+ String saltValue = redisUtil .getKey (redisSaltKey , String .class );
88
+ if (StrUtil .isNotEmpty (saltValue ) && saltValue .equals (salt )) {
89
+ if (Dc3Util .md5 (user .getPassword () + saltValue ).equals (password )) {
90
+ String redisTokenKey = CacheConstant .Entity .USER + CacheConstant .Suffix .TOKEN + CommonConstant .Symbol .SEPARATOR + username ;
91
+ String token = KeyUtil .generateToken (username , saltValue , tenant .getId ());
89
92
redisUtil .setKey (redisTokenKey , token , CacheConstant .Timeout .TOKEN_CACHE_TIMEOUT , TimeUnit .HOURS );
90
93
return token ;
91
94
}
92
95
}
93
96
}
94
- updateUserLimit (name , true );
95
- throw new ServiceException ("Invalid tenant、 username、password" );
97
+ updateUserLimit (username , true );
98
+ throw new ServiceException ("Invalid username、password、tenant " );
96
99
}
97
100
98
101
@ Override
99
- public TokenValid checkTokenValid (String username , String salt , String token ) {
102
+ public TokenValid checkTokenValid (String username , String salt , String token , String tenantName ) {
103
+ // todo 此处一个bug,会抛异常,导致无法记录失败登录次数
104
+ Tenant tenant = tenantService .selectByName (tenantName );
100
105
String redisToken = redisUtil .getKey (CacheConstant .Entity .USER + CacheConstant .Suffix .TOKEN + CommonConstant .Symbol .SEPARATOR + username , String .class );
101
106
if (StrUtil .isBlank (redisToken ) || !redisToken .equals (token )) {
102
107
return new TokenValid (false , null );
103
108
}
104
109
try {
105
- Claims claims = KeyUtil .parserToken (username , salt , token );
110
+ // todo 需要传 tenantId
111
+ Claims claims = KeyUtil .parserToken (username , salt , token , tenant .getId ());
106
112
return new TokenValid (true , claims .getExpiration ());
107
113
} catch (Exception e ) {
108
114
return new TokenValid (false , null );
109
115
}
110
116
}
111
117
112
118
@ Override
113
- public boolean cancelToken (String username ) {
119
+ public boolean cancelToken (String username , String tenantName ) {
120
+ Tenant tenant = tenantService .selectByName (tenantName );
114
121
redisUtil .deleteKey (CacheConstant .Entity .USER + CacheConstant .Suffix .TOKEN + CommonConstant .Symbol .SEPARATOR + username );
115
122
return true ;
116
123
}
0 commit comments