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
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
234 additions
and
84 deletions
+234
-84
AutoTurnOver.Utility.csproj
AutoTurnOver.Utility/AutoTurnOver.Utility.csproj
+2
-0
RedisClient.cs
AutoTurnOver.Utility/RedisClient.cs
+112
-0
RedisHelper.cs
AutoTurnOver.Utility/RedisHelper.cs
+37
-19
AutoUtility.cs
AutoTurnOver/Common/AutoUtility.cs
+20
-5
UserFilterAttribute.cs
AutoTurnOver/Models/UserFilterAttribute.cs
+63
-60
No files found.
AutoTurnOver.Utility/AutoTurnOver.Utility.csproj
View file @
d5a95bcb
...
@@ -16,6 +16,7 @@
...
@@ -16,6 +16,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Qiniu.SDK" Version="8.0.0" />
<PackageReference Include="Qiniu.SDK" Version="8.0.0" />
<PackageReference Include="StackExchange.Redis" Version="2.1.58" />
</ItemGroup>
</ItemGroup>
</Project>
</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
...
@@ -10,37 +10,55 @@ namespace AutoTurnOver.Utility
{
{
public
class
RedisHelper
public
class
RedisHelper
{
{
public
static
T
Get
<
T
>(
string
name
,
Func
<
T
>
getData
,
int
tick
s
=
0
)
where
T
:
class
public
static
T
Get
<
T
>(
string
name
,
Func
<
T
>
getData
,
int
minute
s
=
0
)
where
T
:
class
{
{
var
environmentName
=
Environment
.
GetEnvironmentVariable
(
"ASPNETCORE_ENVIRONMENT"
);
name
=
(
name
+
"v_aims_v1"
);
var
configurationBuilder
=
new
ConfigurationBuilder
()
.
SetBasePath
(
Directory
.
GetCurrentDirectory
())
.
AddJsonFile
(
"appsettings.json"
,
optional
:
true
,
reloadOnChange
:
true
)
string
caheStr
=
RedisClient
.
redisClient
.
GetStringKey
(
name
);
.
AddJsonFile
(
$"appsettings.
{
environmentName
}
.json"
,
true
,
reloadOnChange
:
true
);
var
configuration
=
configurationBuilder
.
Build
();
ConfigManagerConf
.
SetConfiguration
(
configuration
);
T
cache
=
default
(
T
);
var
_cache
=
CacheManagerFactory
.
GetCacheManager
();
if
(!
string
.
IsNullOrWhiteSpace
(
caheStr
))
cache
=
caheStr
.
ToObj
<
T
>();
object
cache
=
_cache
.
Get
<
T
>(
name
);
if
(
cache
==
null
)
if
(
cache
==
null
)
{
{
cache
=
getData
();
cache
=
getData
();
if
(
!
string
.
IsNullOrEmpty
(
cache
.
ToString
())
)
if
(
cache
is
string
)
{
{
_cache
.
Add
(
name
,
cache
);
if
(!
string
.
IsNullOrEmpty
(
cache
.
ToString
()))
if
(
ticks
<=
0
)
{
{
_cache
.
SetExpire
(
name
,
new
TimeSpan
(
1
,
0
,
0
,
0
));
//设置一天缓存
if
(
minutes
<=
0
)
}
{
RedisClient
.
redisClient
.
SetStringKey
(
name
,
cache
.
ToString
(),
(
DateTime
.
Parse
(
DateTime
.
Now
.
ToString
(
"yyyy-MM-dd 23:59:59"
))
-
DateTime
.
Now
));
}
else
{
RedisClient
.
redisClient
.
SetStringKey
(
name
,
cache
.
ToString
(),
new
TimeSpan
(
0
,
minutes
,
0
,
0
));
}
}
else
else
_cache
.
SetExpire
(
name
,
new
TimeSpan
(
ticks
)
);
return
default
(
T
);
}
}
else
else
return
default
(
T
);
{
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
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;
...
@@ -6,6 +6,7 @@ using Newtonsoft.Json;
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Configuration
;
using
System.Configuration
;
using
System.IdentityModel.Tokens.Jwt
;
using
System.IO
;
using
System.IO
;
using
System.Linq
;
using
System.Linq
;
using
System.Net
;
using
System.Net
;
...
@@ -21,16 +22,30 @@ namespace AutoTurnOver.Common
...
@@ -21,16 +22,30 @@ namespace AutoTurnOver.Common
/// <returns></returns>
/// <returns></returns>
public
static
UserData
GetUser
()
public
static
UserData
GetUser
()
{
{
var
userInfo
=
MyHttpContext
.
Current
.
Session
.
GetString
(
"UserInfo"
);
var
token
=
MyHttpContext
.
Current
.
Request
.
Headers
[
"Authorization"
];
var
saasUser
=
userInfo
.
ToObj
<
SaasUserInfo
>();
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
return
new
UserData
{
{
UserName
=
saasUser
.
Account
,
UserName
=
userInfo
.
Account
,
UserAccount
=
saasUser
.
Account
,
UserAccount
=
userInfo
.
Account
,
DepartmentName
=
""
,
//saasUser.Department==null?"":saasUser.Department.Name
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>
/// <summary>
...
...
AutoTurnOver/Models/UserFilterAttribute.cs
View file @
d5a95bcb
...
@@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc.Controllers;
...
@@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc.Controllers;
using
Microsoft.AspNetCore.Mvc.Filters
;
using
Microsoft.AspNetCore.Mvc.Filters
;
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.IdentityModel.Tokens.Jwt
;
using
System.Linq
;
using
System.Linq
;
using
System.Net
;
using
System.Net
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
...
@@ -56,26 +57,28 @@ namespace AutoTurnOver.Models
...
@@ -56,26 +57,28 @@ namespace AutoTurnOver.Models
if
(
token
!=
null
)
if
(
token
!=
null
)
{
{
token
=
WebUtility
.
UrlDecode
(
token
);
token
=
WebUtility
.
UrlDecode
(
token
);
//context.Response.Cookies.Delete("BailunToken");
var
tokenDecode
=
WebUtility
.
UrlDecode
(
token
);
//context.Response.Cookies.Append("BailunToken",token,new CookieOptions { Domain = "bailuntec.com"});
context
.
Response
.
Cookies
.
Delete
(
"BailunToken"
);
context
.
Response
.
Cookies
.
Append
(
"BailunToken"
,
tokenDecode
,
new
CookieOptions
{
Domain
=
"bailuntec.com"
});
var
getTokenValid
=
ConfigHelper
.
GetValue
(
"Saas_GetTokenValid"
).
ToString
();
//Console.WriteLine(tokenDecode);
var
getUser
=
ConfigHelper
.
GetValue
(
"Saas_GetUser"
).
ToString
();
var
dd
=
new
JwtSecurityToken
(
tokenDecode
.
Replace
(
"Bearer "
,
""
));
var
getMenus
=
ConfigHelper
.
GetValue
(
"Saas_GetMenus"
).
ToString
();
var
userId
=
dd
.
Claims
.
FirstOrDefault
(
m
=>
m
.
Type
==
"UserId"
).
Value
;
var
httpHelper
=
new
HHttpHelper
();
var
userData
=
RedisHelper
.
Get
(
"sso-aims-authorization-user-info-"
+
userId
,
()
=>
{
httpHelper
.
AddHeaderValue
(
"Authorization"
,
token
);
var
getTokenValid
=
ConfigHelper
.
GetValue
(
"Saas_GetTokenValid"
).
ToString
();
string
tokenValidJson
=
httpHelper
.
Request
(
getTokenValid
,
HHttpHelper
.
RequestType
.
GET
);
var
getUser
=
ConfigHelper
.
GetValue
(
"Saas_GetUser"
).
ToString
();
var
objToken
=
tokenValidJson
.
ToObj
<
TokenResult
>();
var
getMenus
=
ConfigHelper
.
GetValue
(
"Saas_GetMenus"
).
ToString
();
if
(
objToken
.
result
)
{
var
httpHelper
=
new
HHttpHelper
();
var
userInfo
=
context
.
Session
.
GetString
(
"UserInfo"
);
httpHelper
.
AddHeaderValue
(
"Authorization"
,
token
);
if
(
string
.
IsNullOrWhiteSpace
(
userInfo
))
string
tokenValidJson
=
httpHelper
.
Request
(
getTokenValid
,
HHttpHelper
.
RequestType
.
GET
);
var
objToken
=
tokenValidJson
.
ToObj
<
TokenResult
>();
if
(
objToken
.
result
)
{
{
string
userStr
=
httpHelper
.
Request
(
getUser
,
HHttpHelper
.
RequestType
.
GET
);
string
userStr
=
httpHelper
.
Request
(
getUser
,
HHttpHelper
.
RequestType
.
GET
);
tokenUser
user_info
=
userStr
.
ToObj
<
tokenUser
>();
tokenUser
user_info
=
userStr
.
ToObj
<
tokenUser
>();
user_info
.
result
.
has_admin
=
false
;
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
>>();
var
saas_menus
=
menusStr
.
ToObj
<
saas_result_dto
<
Saas_MenusDto
>>();
if
(
saas_menus
==
null
)
if
(
saas_menus
==
null
)
{
{
...
@@ -106,62 +109,62 @@ namespace AutoTurnOver.Models
...
@@ -106,62 +109,62 @@ namespace AutoTurnOver.Models
}
}
}
}
}
}
userInfo
=
user_info
.
result
.
ToJson
();
return
user_info
;
context
.
Session
.
SetString
(
"UserInfo"
,
userInfo
);
}
}
else
{
context
.
Response
.
StatusCode
=
401
;
filterContext
.
Result
=
new
ContentResult
{
Content
=
"未授权"
,
StatusCode
=
StatusCodes
.
Status401Unauthorized
,
ContentType
=
"text/html;charset=utf-8"
};
return
null
;
}
},
24
*
60
);
#
region
效验操作权限
#
region
效验操作权限
var
isAciton
=
false
;
var
isAciton
=
false
;
if
(
controllerActionDescriptor
!=
null
)
if
(
controllerActionDescriptor
!=
null
)
{
// 判断控制器或者acion 是否需要验证
isAciton
=
controllerActionDescriptor
.
MethodInfo
.
GetCustomAttributes
(
inherit
:
true
)
.
Any
(
a
=>
a
.
GetType
().
Equals
(
typeof
(
UseActionAttribute
)))
||
controllerActionDescriptor
.
ControllerTypeInfo
.
GetCustomAttributes
(
inherit
:
true
)
.
Any
(
a
=>
a
.
GetType
().
Equals
(
typeof
(
UseActionAttribute
)))
;
//需要验证
if
(
isAciton
)
{
{
// 判断控制器或者acion 是否需要验证
SaasUserInfo
userInfoData
=
userData
.
result
;
isAciton
=
controllerActionDescriptor
.
MethodInfo
.
GetCustomAttributes
(
inherit
:
true
)
var
controllerName
=
controllerActionDescriptor
.
ControllerTypeInfo
.
Name
.
Replace
(
"Controller"
,
""
);
.
Any
(
a
=>
a
.
GetType
().
Equals
(
typeof
(
UseActionAttribute
)))
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
))))
controllerActionDescriptor
.
ControllerTypeInfo
.
GetCustomAttributes
(
inherit
:
true
)
.
Any
(
a
=>
a
.
GetType
().
Equals
(
typeof
(
UseActionAttribute
)))
;
//需要验证
if
(
isAciton
)
{
{
SaasUserInfo
userInfoData
=
userInfo
.
ToObj
<
SaasUserInfo
>();
context
.
Response
.
StatusCode
=
401
;
var
controllerName
=
controllerActionDescriptor
.
ControllerTypeInfo
.
Name
.
Replace
(
"Controller"
,
""
);
filterContext
.
Result
=
new
ContentResult
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
))))
{
{
context
.
Response
.
StatusCode
=
401
;
Content
=
"无该功能的操作权限"
,
filterContext
.
Result
=
new
ContentResult
StatusCode
=
StatusCodes
.
Status401Unauthorized
,
{
ContentType
=
"text/html;charset=utf-8"
Content
=
"无该功能的操作权限"
,
};
StatusCode
=
StatusCodes
.
Status401Unauthorized
,
ContentType
=
"text/html;charset=utf-8"
};
}
}
}
}
}
else
{
throw
new
Exception
(
"程序异常"
);
}
#
endregion
return
;
}
}
else
else
{
{
context
.
Response
.
StatusCode
=
401
;
throw
new
Exception
(
"程序异常"
);
filterContext
.
Result
=
new
ContentResult
{
Content
=
"未授权"
,
StatusCode
=
StatusCodes
.
Status401Unauthorized
,
ContentType
=
"text/html;charset=utf-8"
};
}
}
#
endregion
}
}
else
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