Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
teebbstudios
teebblog
提交
f5d4d305
T
teebblog
项目概览
teebbstudios
/
teebblog
通知
0
Star
5
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
teebblog
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
f5d4d305
编写于
7月 16, 2021
作者:
T
teebbstudios
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
79. user login and FormLoginAuthenticator
上级
8b9cace2
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
146 addition
and
1 deletion
+146
-1
config/packages/security.yaml
config/packages/security.yaml
+6
-1
src/Controller/SecurityController.php
src/Controller/SecurityController.php
+36
-0
src/Security/FormLoginAuthenticator.php
src/Security/FormLoginAuthenticator.php
+62
-0
templates/security/login.html.twig
templates/security/login.html.twig
+42
-0
未找到文件。
config/packages/security.yaml
浏览文件 @
f5d4d305
...
...
@@ -19,6 +19,11 @@ security:
main
:
lazy
:
true
provider
:
app_user_provider
custom_authenticator
:
App\Security\FormLoginAuthenticator
logout
:
path
:
app_logout
# where to redirect after logout
# target: app_any_route
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#firewalls-authentication
...
...
@@ -29,5 +34,5 @@ security:
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control
:
# - { path: ^/admin, roles: ROLE
_ADMIN }
-
{
path
:
^/admin
,
roles
:
ROLE_SUPER
_ADMIN
}
# - { path: ^/profile, roles: ROLE_USER }
src/Controller/SecurityController.php
0 → 100644
浏览文件 @
f5d4d305
<?php
namespace
App\Controller
;
use
Symfony\Bundle\FrameworkBundle\Controller\AbstractController
;
use
Symfony\Component\HttpFoundation\Response
;
use
Symfony\Component\Routing\Annotation\Route
;
use
Symfony\Component\Security\Http\Authentication\AuthenticationUtils
;
class
SecurityController
extends
AbstractController
{
/**
* @Route("/login", name="app_login")
*/
public
function
login
(
AuthenticationUtils
$authenticationUtils
):
Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// get the login error if there is one
$error
=
$authenticationUtils
->
getLastAuthenticationError
();
// last username entered by the user
$lastUsername
=
$authenticationUtils
->
getLastUsername
();
return
$this
->
render
(
'security/login.html.twig'
,
[
'last_username'
=>
$lastUsername
,
'error'
=>
$error
]);
}
/**
* @Route("/logout", name="app_logout")
*/
public
function
logout
()
{
throw
new
\
LogicException
(
'This method can be blank - it will be intercepted by the logout key on your firewall.'
);
}
}
src/Security/FormLoginAuthenticator.php
0 → 100644
浏览文件 @
f5d4d305
<?php
namespace
App\Security
;
use
Symfony\Component\HttpFoundation\RedirectResponse
;
use
Symfony\Component\HttpFoundation\Request
;
use
Symfony\Component\HttpFoundation\Response
;
use
Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
use
Symfony\Component\Security\Core\Authentication\Token\TokenInterface
;
use
Symfony\Component\Security\Core\Security
;
use
Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator
;
use
Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge
;
use
Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge
;
use
Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials
;
use
Symfony\Component\Security\Http\Authenticator\Passport\Passport
;
use
Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface
;
use
Symfony\Component\Security\Http\Util\TargetPathTrait
;
class
FormLoginAuthenticator
extends
AbstractLoginFormAuthenticator
{
use
TargetPathTrait
;
public
const
LOGIN_ROUTE
=
'app_login'
;
private
UrlGeneratorInterface
$urlGenerator
;
public
function
__construct
(
UrlGeneratorInterface
$urlGenerator
)
{
$this
->
urlGenerator
=
$urlGenerator
;
}
public
function
authenticate
(
Request
$request
):
PassportInterface
{
$username
=
$request
->
request
->
get
(
'username'
,
''
);
$request
->
getSession
()
->
set
(
Security
::
LAST_USERNAME
,
$username
);
return
new
Passport
(
new
UserBadge
(
$username
),
new
PasswordCredentials
(
$request
->
request
->
get
(
'password'
,
''
)),
[
new
CsrfTokenBadge
(
'authenticate'
,
$request
->
get
(
'_csrf_token'
)),
]
);
}
public
function
onAuthenticationSuccess
(
Request
$request
,
TokenInterface
$token
,
string
$firewallName
):
?Response
{
if
(
$targetPath
=
$this
->
getTargetPath
(
$request
->
getSession
(),
$firewallName
))
{
return
new
RedirectResponse
(
$targetPath
);
}
// For example:
return
new
RedirectResponse
(
$this
->
urlGenerator
->
generate
(
'admin'
));
// throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
}
protected
function
getLoginUrl
(
Request
$request
):
string
{
return
$this
->
urlGenerator
->
generate
(
self
::
LOGIN_ROUTE
);
}
}
templates/security/login.html.twig
0 → 100644
浏览文件 @
f5d4d305
{%
extends
'base.html.twig'
%}
{%
block
title
%}
Log in!
{%
endblock
%}
{%
block
content
%}
<form
method=
"post"
>
{%
if
error
%}
<div
class=
"alert alert-danger"
>
{{
error.messageKey
|
trans
(
error.messageData
,
'security'
)
}}
</div>
{%
endif
%}
{%
if
app.user
%}
<div
class=
"mb-3"
>
You are logged in as
{{
app.user.username
}}
,
<a
href=
"
{{
path
(
'app_logout'
)
}}
"
>
Logout
</a>
</div>
{%
endif
%}
<h1
class=
"h3 mb-3 font-weight-normal"
>
Please sign in
</h1>
<label
for=
"inputUsername"
>
Username
</label>
<input
type=
"text"
value=
"
{{
last_username
}}
"
name=
"username"
id=
"inputUsername"
class=
"form-control"
autocomplete=
"username"
required
autofocus
>
<label
for=
"inputPassword"
>
Password
</label>
<input
type=
"password"
name=
"password"
id=
"inputPassword"
class=
"form-control"
autocomplete=
"current-password"
required
>
<input
type=
"hidden"
name=
"_csrf_token"
value=
"
{{
csrf_token
(
'authenticate'
)
}}
"
>
{#
Uncomment this section and add a remember_me option below your firewall to activate remember me functionality.
See https://symfony.com/doc/current/security/remember_me.html
<div class="checkbox mb-3">
<label>
<input type="checkbox" name="_remember_me"> Remember me
</label>
</div>
#}
<button
class=
"btn btn-lg btn-primary"
type=
"submit"
>
Sign in
</button>
</form>
{%
endblock
%}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录