當有人試圖訪問不存在的web路由時,我想顯示404未找到錯誤。當路由不存在時,生產和開發環境中的不同異常
我不斷收到此異常在生產環境中:
Fatal error: Uncaught exception 'Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException' with message 'The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL.' in /<path>/app/cache/prod/classes.php:4626 Stack trace: #0 /<path>/app/cache/prod/classes.php(4364): Symfony\Component\Security\Core\Authorization\AuthorizationChecker->isGranted('ROLE_USER', NULL) #1 /<path>/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Extension/SecurityExtension.php(41): Symfony\Component\Security\Core\SecurityContext->isGranted('ROLE_USER', NULL) #2 /<path>/app/cache/prod/twig/9e/5d/2e6e87b557efe952c1ff84648c04bdb5d6870549f95d79526f65c94696d2.php(149): Symfony\Bridge\Twig\Extension\SecurityExtension->isGranted('ROLE_USER') #3 /<path>/app/cache/prod/classes.php(6519): __TwigTemplate_9e5d2e6e87b557efe952c1ff84648c04bdb5d6870549f95d79526f65c94696d2->d in /<path>/app/cache/prod/classes.php on line 6530
這是非常奇怪的,因爲在開發中我得到NotFoundHttpException,所以在生產404頁應該顯示。
這是我的安全陽明:
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: [ ROLE_USER, ROLE_SONATA_ADMIN ]
ROLE_SUPER_ADMIN: [ ROLE_ADMIN, ROLE_EDITOR ]
providers:
fos_userbundle:
id: fos_user.user_provider.username_email
firewalls:
oauth_token:
pattern: ^/oauth/v2/token
security: false
oauth_authorize:
pattern: ^/oauth/v2/auth
form_login:
provider: fos_userbundle
login_path: fos_user_security_login
use_forward: true
use_referer: true
check_path: fos_user_security_check
failure_path: front_homepage
default_target_path:/
always_use_default_target_path: false
failure_handler: app.authentication_handler
anonymous: true
api:
pattern: ^/v1
fos_oauth: true
stateless: true
anonymous: true # note that anonymous access is now enabled
main:
pattern: ^/*
oauth:
resource_owners:
facebook: "/login/check-facebook"
google: "/login/check-google"
login_path: fos_user_security_login
use_forward: true
use_referer: true
# failure_path: /login2
failure_handler: app.authentication_handler
provider: fos_userbundle
oauth_user_provider:
service: my_user_provider
form_login:
provider: fos_userbundle
login_path: fos_user_security_login
use_forward: true
use_referer: true
check_path: fos_user_security_check
failure_path: front_homepage
default_target_path:/
always_use_default_target_path: false
failure_handler: app.authentication_handler
logout:
path: fos_user_security_logout
target: /
success_handler: app.authentication_handler
anonymous: true
remember_me:
key: "%secret%"
lifetime: 31536000 # 1 year in seconds
path: /.*
domain: ~
switch_user: { role: ROLE_ADMIN }
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
default:
anonymous: ~
access_control:
# Public dev tools
- { path: ^/_wdt, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/_profiler, role: IS_AUTHENTICATED_ANONYMOUSLY }
# Public login routes
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
# Private routes
- { path: ^/admin, role: ROLE_ADMIN }
- { path: ^/admin/efconnect, role: ROLE_ADMIN }
- { path: ^/admin/elfinder, role: ROLE_ADMIN }
- { path: ^/v1, role: IS_AUTHENTICATED_ANONYMOUSLY }
# Rest of all domain
- { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }