gpt4 book ai didi

php - 从外部应用程序访问 Symfony session 值

转载 作者:行者123 更新时间:2023-12-02 18:18:38 25 4
gpt4 key购买 nike

我有一个第三方应用程序(TinyMCEresponsivefilemanager插件),我无法使用Symfony2重写它。
我需要保护它免受未经授权的用户的侵害。
是否可以从外部应用程序访问 Symfony2 的 session 变量(用户、角色等)? 如何?
我尝试执行 session_start() 并读取 $_SESSION 变量,但它是空的!
我的 config.yml 是:

imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: @ar1y4nArticleBundle/Resources/config/admin.yml }

framework:
#esi: ~
translator: { fallback: %locale% }
secret: %secret%
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
default_locale: "%locale%"
trusted_proxies: ~
session: ~
fragments: ~

# Twig Configuration
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%

# Assetic Configuration
assetic:
debug: %kernel.debug%
use_controller: false
bundles: [ ]
#java: /usr/bin/java
filters:
cssrewrite: ~
#closure:
# jar: %kernel.root_dir%/Resources/java/compiler.jar
#yui_css:
# jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar

# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
types: #this is about this line and line below
json: Sonata\Doctrine\Types\JsonType
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: %kernel.root_dir%/data/data.db3
# path: %database_path%

orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
transport: %mailer_transport%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
spool: { type: memory }

fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main
user_class: ar1y4n\UserBundle\Entity\User

group:
group_class: ar1y4n\UserBundle\Entity\Group

sonata_block:
default_contexts: [cms]
blocks:
sonata.admin.block.admin_list:
contexts: [admin]

#sonata.admin_doctrine_orm.block.audit:
# contexts: [admin]

sonata.block.service.text:
sonata.block.service.rss:

sonata.user.block.menu: # used to display the menu in profile pages
sonata.user.block.account: # used to display menu option (login option)

# Some specific block from the SonataMediaBundle
#sonata.media.block.media:
#sonata.media.block.gallery:
#sonata.media.block.feature_media:

knp_menu:
twig: # use "twig: false" to disable the Twig extension and the TwigRenderer
template: knp_menu.html.twig
templating: false # if true, enables the helper for PHP templates
default_renderer: twig # The renderer to use, list is also available by default

sonata_user:
security_acl: true
class: # Entity Classes
user: ar1y4n\UserBundle\Entity\User
group: ar1y4n\UserBundle\Entity\Group

sonata_admin:
title: My title
title_logo: bundles/ar1y4narticle/images/logo-big.png

genemu_form:
tinymce:
enabled: true
theme: modern
configs: {plugins: ["responsivefilemanager advlist autolink lists link image charmap print preview hr anchor pagebreak","searchreplace wordcount visualblocks visualchars code fullscreen","insertdatetime media nonbreaking save table contextmenu directionality", "emoticons template paste textcolor"],toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",toolbar2: "print preview media | forecolor backcolor emoticons | responsivefilemanager",image_advtab: true, external_filemanager_path:"/filemanager/",filemanager_title:"Responsive Filemanager" ,external_plugins: { "filemanager" : "/filemanager/plugin.min.js"}}

最佳答案

我通过这样做设法访问安全上下文:
reponsivefilemanager/config/config.php中添加:

require_once '../../vendor/autoload.php';
require_once '../../app/bootstrap.php.cache';
require_once '../../app/AppKernel.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;

$kernel = new AppKernel('dev', true);
//$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
$kernel->boot();

$session = new \Symfony\Component\HttpFoundation\Session\Session($kernel->getContainer()->get('session.storage'));
$session->start();
$request = Request::createFromGlobals();
$request->setSession($session);
$event = new GetResponseEvent($kernel->getContainer()->get('http_kernel'),$request, HttpKernel::MASTER_REQUEST);

$firewall = $kernel->getContainer()->get('security.firewall');
$firewall->onKernelRequest($event);
if(!$kernel->getContainer()->get('security.context')->isGranted('ROLE_ADMIN')) die("Access Denied");

当然,您应该根据您的文件结构更改 autoload.phpbootstrap.php.cacheAppKernel.php 路径。< br/>这有两个问题:

  • 当使用 prod 模式 (app.php) 时,您应该使用 $kernel = new AppKernel('prod', false); $kernel = new AppKernel('dev', true); 使用 dev 模式时 (app_dev.php)
  • 当未登录的用户尝试访问文件管理器并给出 symfony 的 Access Denied 错误时,就会出现问题;然而,它完成了这项工作并阻止非授权用户使用文件管理器

我正在努力解决问题;我将在这里发布结果。

祝你好运

关于php - 从外部应用程序访问 Symfony session 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20858769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com