Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-micro
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
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
without authentication
meibuyu-micro
Commits
88942b22
Commit
88942b22
authored
Mar 28, 2022
by
秦俊坤
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增鉴权相关接口
parent
e5fc1ff4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
109 additions
and
0 deletions
+109
-0
AuthorizeMiddleware.php
src/Middleware/AuthorizeMiddleware.php
+69
-0
AuthenticationServiceInterface.php
...ervice/Interfaces/User/AuthenticationServiceInterface.php
+40
-0
No files found.
src/Middleware/AuthorizeMiddleware.php
0 → 100644
View file @
88942b22
<?php
namespace
Meibuyu\Micro\Middleware
;
use
Meibuyu\Micro\Model\Auth
;
use
Meibuyu\Micro\Service\Interfaces\User\AuthenticationServiceInterface
;
use
Psr\Http\Message\ResponseInterface
;
use
Psr\Http\Message\ServerRequestInterface
;
use
Psr\Http\Server\MiddlewareInterface
;
use
Psr\Http\Server\RequestHandlerInterface
;
class
AuthorizeMiddleware
implements
MiddlewareInterface
{
/**
* @Inject()
* @var AuthenticationServiceInterface
*/
private
$authorizationService
;
public
function
process
(
ServerRequestInterface
$request
,
RequestHandlerInterface
$handler
)
:
ResponseInterface
{
$route
=
$request
->
getUri
()
->
getPath
();
$token
=
token
();
$applicationName
=
env
(
'APP_NAME'
);
if
(
empty
(
$route
))
return
$handler
->
handle
(
$request
);
//获取对应的 route 对应的权限,如果 route 是不需要登录鉴权,直接返回
$passed
=
$this
->
routerAuth
(
$applicationName
,
$route
,
$token
);
if
(
$passed
)
{
return
$handler
->
handle
(
$request
);
}
return
response
()
->
withStatus
(
403
);
//鉴权失败,错误码 403 forbidden
//route 是需要登录鉴权的,判断当前用户是佛有对应 route 的权限
}
/**
* 获取对应路由的权限,调用 RPC 服务
* @param $applicationName
* @param $route
* @param $token
* @return bool
*/
protected
function
routerAuth
(
$applicationName
,
$route
,
$token
)
:
bool
{
$userId
=
$this
->
getUserIdByToken
(
$token
);
return
$this
->
authorizationService
->
authByRouter
(
$applicationName
,
$route
,
$userId
);
}
/**
* 根据 token 获取对应的 user_id
* @param $token
* @return int|mixed
*/
protected
function
getUserIdByToken
(
$token
)
{
if
(
empty
(
$token
))
return
0
;
$user
=
redis
()
->
get
(
$token
);
if
(
!
$user
)
return
0
;
$userArr
=
\json_decode
(
$user
,
true
);
return
!
empty
(
$userArr
[
'id'
])
?
$userArr
[
'id'
]
:
0
;
}
}
\ No newline at end of file
src/Service/Interfaces/User/AuthenticationServiceInterface.php
0 → 100644
View file @
88942b22
<?php
/**
* Created by PhpStorm.
* User: zero
* Date: 2020/5/26
* Time: 15:17
*/
namespace
Meibuyu\Micro\Service\Interfaces\User
;
interface
AuthenticationServiceInterface
{
/**
* 获取对应用户能够看到的菜单
* @param string $name
* @param array $columns
* @return array
*/
public
function
getMenus
(
$userId
)
:
array
;
/**
* 获取对应用户的菜单权限
* @param $userId
* @return array
*/
public
function
getButtons
(
$userId
)
:
array
;
/**
* 获取对应路由的接口权限结果
* @param $router string 路由名字
* @param $applicationName string 应用名字
* @param $userId integer 用户 ID
* @return bool
*/
public
function
authByRouter
(
$applicationName
,
$router
,
$userId
)
:
bool
;
}
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