22using Microsoft . AspNetCore . Authorization ;
33using Microsoft . AspNetCore . Mvc ;
44using Microsoft . Extensions . Caching . Memory ;
5+ using Microsoft . Extensions . DependencyInjection ;
56using System ;
7+ using System . Collections . Concurrent ;
8+ using System . Linq ;
69using System . Linq . Expressions ;
710using System . Threading . Tasks ;
11+ using VOL . Core . CacheManager ;
812using VOL . Core . Configuration ;
913using VOL . Core . Controllers . Basic ;
1014using VOL . Core . DBManager ;
1115using VOL . Core . EFDbContext ;
16+ using VOL . Core . Enums ;
1217using VOL . Core . Extensions ;
1318using VOL . Core . Filters ;
1419using VOL . Core . Infrastructure ;
1520using VOL . Core . ManageUser ;
1621using VOL . Core . ObjectActionValidator ;
22+ using VOL . Core . Services ;
1723using VOL . Core . Utilities ;
1824using VOL . Entity . AttributeManager ;
1925using VOL . Entity . DomainModels ;
@@ -26,19 +32,93 @@ namespace VOL.System.Controllers
2632 [ Route ( "api/User" ) ]
2733 public partial class Sys_UserController
2834 {
35+ private ISys_UserRepository _userRepository ;
36+ private ICacheService _cache ;
37+ [ ActivatorUtilitiesConstructor ]
38+ public Sys_UserController (
39+ ISys_UserService userService ,
40+ ISys_UserRepository userRepository ,
41+ ICacheService cahce
42+ )
43+ : base ( userService )
44+ {
45+ _userRepository = userRepository ;
46+ _cache = cahce ;
47+ }
48+
2949 [ HttpPost , HttpGet , Route ( "login" ) , AllowAnonymous ]
3050 [ ObjectModelValidatorFilter ( ValidatorModel . Login ) ]
31- public async Task < IActionResult > Login ( [ FromBody ] LoginInfo loginInfo )
51+ public async Task < IActionResult > Login ( [ FromBody ] LoginInfo loginInfo )
3252 {
3353 return Json ( await Service . Login ( loginInfo ) ) ;
3454 }
3555
36- [ HttpPost , Route ( "replaceToken" ) , AllowAnonymous ]
37- public async Task < IActionResult > ReplaceToken ( )
56+ private readonly ConcurrentDictionary < int , object > _lockCurrent = new ConcurrentDictionary < int , object > ( ) ;
57+ [ HttpPost , Route ( "replaceToken" ) ]
58+ public IActionResult ReplaceToken ( )
3859 {
39- return Json ( await Service . ReplaceToken ( ) ) ;
60+ WebResponseContent responseContent = new WebResponseContent ( ) ;
61+ string error = "" ;
62+ string key = $ "rp:Token:{ UserContext . Current . UserId } ";
63+ UserInfo userInfo = null ;
64+ try
65+ {
66+ //如果5秒内替换过token,直接使用最新的token(防止一个页面多个并发请求同时替换token导致token错位)
67+ if ( _cache . Exists ( key ) )
68+ {
69+ return Json ( responseContent . OK ( null , _cache . Get ( key ) ) ) ;
70+ }
71+ var _obj = _lockCurrent . GetOrAdd ( UserContext . Current . UserId , new object ( ) { } ) ;
72+ lock ( _obj )
73+ {
74+ if ( _cache . Exists ( key ) )
75+ {
76+ return Json ( responseContent . OK ( null , _cache . Get ( key ) ) ) ;
77+ }
78+ string requestToken = HttpContext . Request . Headers [ AppSetting . TokenHeaderName ] ;
79+ requestToken = requestToken ? . Replace ( "Bearer " , "" ) ;
80+
81+ if ( JwtHelper . IsExp ( requestToken ) ) return Json ( responseContent . Error ( "Token已过期!" ) ) ;
82+
83+ int userId = UserContext . Current . UserId ;
84+
85+ userInfo = _userRepository . FindAsIQueryable ( x => x . User_Id == userId ) . Select (
86+ s => new UserInfo ( )
87+ {
88+ User_Id = userId ,
89+ UserName = s . UserName ,
90+ UserTrueName = s . UserTrueName ,
91+ Role_Id = s . Role_Id ,
92+ RoleName = s . RoleName
93+ } ) . FirstOrDefault ( ) ;
94+
95+ if ( userInfo == null ) return Json ( responseContent . Error ( "未查到用户信息!" ) ) ;
96+
97+ string token = JwtHelper . IssueJwt ( userInfo ) ;
98+ //移除当前缓存
99+ _cache . Remove ( userId . GetUserIdKey ( ) ) ;
100+ //只更新的token字段
101+ _userRepository . Update ( new Sys_User ( ) { User_Id = userId , Token = token } , x => x . Token , true ) ;
102+ //添加一个5秒缓存
103+ _cache . Add ( key , token , 5 ) ;
104+ responseContent . OK ( null , token ) ;
105+ }
106+ }
107+ catch ( Exception ex )
108+ {
109+ error = ex . Message + ex . StackTrace ;
110+ responseContent . Error ( "token替换异常" ) ;
111+ }
112+ finally
113+ {
114+ _lockCurrent . TryRemove ( UserContext . Current . UserId , out object val ) ;
115+ string _message = $ "用户{ userInfo ? . User_Id } _{ userInfo ? . UserTrueName } ,({ ( responseContent . Status ? "token替换成功" : "token替换失败" ) } )";
116+ Logger . Info ( LoggerType . ReplaceToeken , _message , null , error ) ;
117+ }
118+ return Json ( responseContent ) ;
40119 }
41120
121+
42122 [ HttpPost , Route ( "modifyPwd" ) ]
43123 [ ApiActionPermission ]
44124 //通过ObjectGeneralValidatorFilter校验参数,不再需要if esle判断OldPwd与NewPwd参数
@@ -76,7 +156,7 @@ public IActionResult ModifyUserPwd(string password, string userName)
76156 repository . Update ( user , x => new { x . UserPwd } , true ) ;
77157 //如果用户在线,强制下线
78158 UserContext . Current . LogOut ( user . User_Id ) ;
79- return Json ( webResponse . OK ( "密码修改成功" ) ) ;
159+ return Json ( webResponse . OK ( "密码修改成功" ) ) ;
80160 }
81161
82162 /// <summary>
0 commit comments