Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
data-center-auto
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bltdc
data-center-auto
Commits
d5a95bcb
Commit
d5a95bcb
authored
Dec 26, 2020
by
泽锋 李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
seesion 信息切换到redis存储,提示访问速度
parent
3a365724
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
196 additions
and
46 deletions
+196
-46
AutoTurnOver.Utility.csproj
AutoTurnOver.Utility/AutoTurnOver.Utility.csproj
+2
-0
RedisClient.cs
AutoTurnOver.Utility/RedisClient.cs
+112
-0
RedisHelper.cs
AutoTurnOver.Utility/RedisHelper.cs
+35
-17
AutoUtility.cs
AutoTurnOver/Common/AutoUtility.cs
+20
-5
UserFilterAttribute.cs
AutoTurnOver/Models/UserFilterAttribute.cs
+27
-24
No files found.
AutoTurnOver.Utility/AutoTurnOver.Utility.csproj
View file @
d5a95bcb
...
...
@@ -16,6 +16,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Qiniu.SDK" Version="8.0.0" />
<PackageReference Include="StackExchange.Redis" Version="2.1.58" />
</ItemGroup>
</Project>
\ No newline at end of file
AutoTurnOver.Utility/RedisClient.cs
0 → 100644
View file @
d5a95bcb
using
Newtonsoft.Json
;
using
StackExchange.Redis
;
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
namespace
AutoTurnOver.Utility
{
public
class
RedisClient
{
private
static
readonly
object
Locker
=
new
object
();
private
ConnectionMultiplexer
redisMultiplexer
;
IDatabase
db
=
null
;
private
static
RedisClient
_redisClient
=
null
;
public
static
RedisClient
redisClient
{
get
{
if
(
_redisClient
==
null
)
{
lock
(
Locker
)
{
if
(
_redisClient
==
null
)
{
_redisClient
=
new
RedisClient
();
}
}
}
return
_redisClient
;
}
}
public
void
InitConnect
()
{
try
{
var
RedisConnection
=
ConfigHelper
.
GetValue
(
"ConnectionStrings:RedisConnectionString"
);
redisMultiplexer
=
ConnectionMultiplexer
.
Connect
(
RedisConnection
);
db
=
redisMultiplexer
.
GetDatabase
();
}
catch
(
Exception
ex
)
{
Console
.
WriteLine
(
ex
.
Message
);
redisMultiplexer
=
null
;
db
=
null
;
}
}
public
RedisClient
()
{
}
#
region
String
/// <summary>
/// 保存单个key value
/// </summary>
/// <param name="value">保存的值</param>
/// <param name="expiry">过期时间</param>
public
bool
SetStringKey
(
string
key
,
string
value
,
TimeSpan
?
expiry
=
default
(
TimeSpan
?))
{
return
db
.
StringSet
(
key
,
value
,
expiry
);
}
/// <summary>
/// 获取单个key的值
/// </summary>
public
RedisValue
GetStringKey
(
string
key
)
{
return
db
.
StringGet
(
key
);
}
/// <summary>
/// 获取一个key的对象
/// </summary>
public
T
GetStringKey
<
T
>(
string
key
)
{
if
(
db
==
null
)
{
return
default
;
}
var
value
=
db
.
StringGet
(
key
);
if
(
value
.
IsNullOrEmpty
)
{
return
default
;
}
return
JsonConvert
.
DeserializeObject
<
T
>(
value
);
}
/// <summary>
/// 保存一个对象
/// </summary>
/// <param name="obj"></param>
public
bool
SetStringKey
<
T
>(
string
key
,
T
obj
,
TimeSpan
?
expiry
=
default
(
TimeSpan
?))
{
if
(
db
==
null
)
{
return
false
;
}
string
json
=
JsonConvert
.
SerializeObject
(
obj
);
return
db
.
StringSet
(
key
,
json
,
expiry
);
}
#
endregion
}
}
AutoTurnOver.Utility/RedisHelper.cs
View file @
d5a95bcb
...
...
@@ -10,37 +10,55 @@ namespace AutoTurnOver.Utility
{
public
class
RedisHelper
{
public
static
T
Get
<
T
>(
string
name
,
Func
<
T
>
getData
,
int
ticks
=
0
)
where
T
:
class
{
var
environmentName
=
Environment
.
GetEnvironmentVariable
(
"ASPNETCORE_ENVIRONMENT"
);
var
configurationBuilder
=
new
ConfigurationBuilder
()
.
SetBasePath
(
Directory
.
GetCurrentDirectory
())
.
AddJsonFile
(
"appsettings.json"
,
optional
:
true
,
reloadOnChange
:
true
)
.
AddJsonFile
(
$"appsettings.
{
environmentName
}
.json"
,
true
,
reloadOnChange
:
true
);
var
configuration
=
configurationBuilder
.
Build
();
ConfigManagerConf
.
SetConfiguration
(
configuration
);
var
_cache
=
CacheManagerFactory
.
GetCacheManager
();
object
cache
=
_cache
.
Get
<
T
>(
name
);
public
static
T
Get
<
T
>(
string
name
,
Func
<
T
>
getData
,
int
minutes
=
0
)
where
T
:
class
{
name
=
(
name
+
"v_aims_v1"
);
string
caheStr
=
RedisClient
.
redisClient
.
GetStringKey
(
name
);
T
cache
=
default
(
T
);
if
(!
string
.
IsNullOrWhiteSpace
(
caheStr
))
cache
=
caheStr
.
ToObj
<
T
>();
if
(
cache
==
null
)
{
cache
=
getData
();
if
(
cache
is
string
)
{
if
(!
string
.
IsNullOrEmpty
(
cache
.
ToString
()))
{
_cache
.
Add
(
name
,
cache
);
if
(
ticks
<=
0
)
if
(
minutes
<=
0
)
{
RedisClient
.
redisClient
.
SetStringKey
(
name
,
cache
.
ToString
(),
(
DateTime
.
Parse
(
DateTime
.
Now
.
ToString
(
"yyyy-MM-dd 23:59:59"
))
-
DateTime
.
Now
));
}
else
{
_cache
.
SetExpire
(
name
,
new
TimeSpan
(
1
,
0
,
0
,
0
));
//设置一天缓存
RedisClient
.
redisClient
.
SetStringKey
(
name
,
cache
.
ToString
(),
new
TimeSpan
(
0
,
minutes
,
0
,
0
));
}
}
else
return
default
(
T
);
}
else
{
if
(
cache
!=
null
)
{
if
(
minutes
<=
0
)
RedisClient
.
redisClient
.
SetStringKey
(
name
,
cache
.
ToJson
(),
(
DateTime
.
Parse
(
DateTime
.
Now
.
ToString
(
"yyyy-MM-dd 23:59:59"
))
-
DateTime
.
Now
));
else
_cache
.
SetExpire
(
name
,
new
TimeSpan
(
ticks
));
RedisClient
.
redisClient
.
SetStringKey
(
name
,
cache
.
ToJson
(),
new
TimeSpan
(
0
,
minutes
,
0
,
0
));
}
else
return
default
(
T
);
}
return
(
T
)
cache
;
}
return
cache
;
}
}
}
AutoTurnOver/Common/AutoUtility.cs
View file @
d5a95bcb
...
...
@@ -6,6 +6,7 @@ using Newtonsoft.Json;
using
System
;
using
System.Collections.Generic
;
using
System.Configuration
;
using
System.IdentityModel.Tokens.Jwt
;
using
System.IO
;
using
System.Linq
;
using
System.Net
;
...
...
@@ -21,16 +22,30 @@ namespace AutoTurnOver.Common
/// <returns></returns>
public
static
UserData
GetUser
()
{
var
userInfo
=
MyHttpContext
.
Current
.
Session
.
GetString
(
"UserInfo"
);
var
saasUser
=
userInfo
.
ToObj
<
SaasUserInfo
>();
var
token
=
MyHttpContext
.
Current
.
Request
.
Headers
[
"Authorization"
];
var
tokenDecode
=
WebUtility
.
UrlDecode
(
token
);
var
dd
=
new
JwtSecurityToken
(
tokenDecode
.
Replace
(
"Bearer "
,
""
));
var
userId
=
dd
.
Claims
.
FirstOrDefault
(
m
=>
m
.
Type
==
"UserId"
).
Value
;
var
userInfo
=
RedisHelper
.
Get
<
tokenUser
>(
"sso-aims-authorization-user-info-"
+
userId
,
()
=>
{
return
null
;
}).
result
;
return
new
UserData
{
UserName
=
saasUser
.
Account
,
UserAccount
=
saasUser
.
Account
,
UserName
=
userInfo
.
Account
,
UserAccount
=
userInfo
.
Account
,
DepartmentName
=
""
,
//saasUser.Department==null?"":saasUser.Department.Name
tree_menu_list
=
saasUser
.
tree_menu_list
()
tree_menu_list
=
userInfo
.
tree_menu_list
()
};
//var userInfo = MyHttpContext.Current.Session.GetString("UserInfo");
//var saasUser = userInfo.ToObj<SaasUserInfo>();
//return new UserData
//{
// UserName = saasUser.Account,
// UserAccount = saasUser.Account,
// DepartmentName = "",//saasUser.Department==null?"":saasUser.Department.Name
// tree_menu_list = saasUser.tree_menu_list()
//};
}
/// <summary>
...
...
AutoTurnOver/Models/UserFilterAttribute.cs
View file @
d5a95bcb
...
...
@@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc.Controllers;
using
Microsoft.AspNetCore.Mvc.Filters
;
using
System
;
using
System.Collections.Generic
;
using
System.IdentityModel.Tokens.Jwt
;
using
System.Linq
;
using
System.Net
;
using
System.Threading.Tasks
;
...
...
@@ -56,9 +57,14 @@ namespace AutoTurnOver.Models
if
(
token
!=
null
)
{
token
=
WebUtility
.
UrlDecode
(
token
);
//context.Response.Cookies.Delete("BailunToken");
//context.Response.Cookies.Append("BailunToken",token,new CookieOptions { Domain = "bailuntec.com"});
var
tokenDecode
=
WebUtility
.
UrlDecode
(
token
);
context
.
Response
.
Cookies
.
Delete
(
"BailunToken"
);
context
.
Response
.
Cookies
.
Append
(
"BailunToken"
,
tokenDecode
,
new
CookieOptions
{
Domain
=
"bailuntec.com"
});
//Console.WriteLine(tokenDecode);
var
dd
=
new
JwtSecurityToken
(
tokenDecode
.
Replace
(
"Bearer "
,
""
));
var
userId
=
dd
.
Claims
.
FirstOrDefault
(
m
=>
m
.
Type
==
"UserId"
).
Value
;
var
userData
=
RedisHelper
.
Get
(
"sso-aims-authorization-user-info-"
+
userId
,
()
=>
{
var
getTokenValid
=
ConfigHelper
.
GetValue
(
"Saas_GetTokenValid"
).
ToString
();
var
getUser
=
ConfigHelper
.
GetValue
(
"Saas_GetUser"
).
ToString
();
var
getMenus
=
ConfigHelper
.
GetValue
(
"Saas_GetMenus"
).
ToString
();
...
...
@@ -69,13 +75,10 @@ namespace AutoTurnOver.Models
var
objToken
=
tokenValidJson
.
ToObj
<
TokenResult
>();
if
(
objToken
.
result
)
{
var
userInfo
=
context
.
Session
.
GetString
(
"UserInfo"
);
if
(
string
.
IsNullOrWhiteSpace
(
userInfo
))
{
string
userStr
=
httpHelper
.
Request
(
getUser
,
HHttpHelper
.
RequestType
.
GET
);
tokenUser
user_info
=
userStr
.
ToObj
<
tokenUser
>();
user_info
.
result
.
has_admin
=
false
;
var
menusStr
=
httpHelper
.
Request
(
getMenus
+
"?ProjectCode=Bailun_aims"
,
HHttpHelper
.
RequestType
.
GET
);
var
menusStr
=
httpHelper
.
Request
(
getMenus
+
"?ProjectCode=Bailun_aims"
,
HHttpHelper
.
RequestType
.
GET
);
var
saas_menus
=
menusStr
.
ToObj
<
saas_result_dto
<
Saas_MenusDto
>>();
if
(
saas_menus
==
null
)
{
...
...
@@ -106,10 +109,20 @@ namespace AutoTurnOver.Models
}
}
}
userInfo
=
user_info
.
result
.
ToJson
();
context
.
Session
.
SetString
(
"UserInfo"
,
userInfo
);
return
user_info
;
}
else
{
context
.
Response
.
StatusCode
=
401
;
filterContext
.
Result
=
new
ContentResult
{
Content
=
"未授权"
,
StatusCode
=
StatusCodes
.
Status401Unauthorized
,
ContentType
=
"text/html;charset=utf-8"
};
return
null
;
}
},
24
*
60
);
#
region
效验操作权限
var
isAciton
=
false
;
...
...
@@ -126,10 +139,10 @@ namespace AutoTurnOver.Models
//需要验证
if
(
isAciton
)
{
SaasUserInfo
userInfoData
=
userInfo
.
ToObj
<
SaasUserInfo
>()
;
SaasUserInfo
userInfoData
=
userData
.
result
;
var
controllerName
=
controllerActionDescriptor
.
ControllerTypeInfo
.
Name
.
Replace
(
"Controller"
,
""
);
var
requst_url
=
(
"/api/"
+
controllerName
+
"/"
+
controllerActionDescriptor
.
MethodInfo
.
Name
);
if
(!
userInfoData
.
menuInfoDtos
.
Any
(
s
=>
s
.
menuUrl_list
!=
null
&&
s
.
menuUrl_list
.
Any
(
m
=>
m
!=
null
&&
requst_url
.
Equals
(
m
,
StringComparison
.
OrdinalIgnoreCase
))))
if
(!
userInfoData
.
menuInfoDtos
.
Any
(
s
=>
s
.
menuUrl_list
!=
null
&&
s
.
menuUrl_list
.
Any
(
m
=>
m
!=
null
&&
requst_url
.
Equals
(
m
,
StringComparison
.
OrdinalIgnoreCase
))))
{
context
.
Response
.
StatusCode
=
401
;
filterContext
.
Result
=
new
ContentResult
...
...
@@ -150,18 +163,8 @@ namespace AutoTurnOver.Models
#
endregion
return
;
}
else
{
context
.
Response
.
StatusCode
=
401
;
filterContext
.
Result
=
new
ContentResult
{
Content
=
"未授权"
,
StatusCode
=
StatusCodes
.
Status401Unauthorized
,
ContentType
=
"text/html;charset=utf-8"
};
}
}
else
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment