【问题标题】:How to use Plack::Middleware::CSRFBlock with Dancer?如何将 Plack::Middleware::CSRFBlock 与 Dancer 一起使用?
【发布时间】:2012-10-28 12:45:46
【问题描述】:

我想用Dancer 保护所有表单免受 CSRF 的影响。

我尝试使用Plack::Middleware::CSRFBlock,但错误提示“CSRFBlock 需要 Session。”。即使我使用Plack::Session,表单也没有包含一次性令牌的隐藏输入字段。

有什么好的做法可以做到这一点吗?非常感谢任何建议。

我的environment/development.yml 是:

# configuration file for development environment

# the logger engine to use
# console: log messages to STDOUT (your console where you started the
#          application server)
# file:    log message to a file in log/
logger: "console"

# the log level for this environment
# core is the lowest, it shows Dancer's core log messages as well as yours
# (debug, info, warning and error)
log: "core"

# should Dancer consider warnings as critical errors?
warnings: 1

# should Dancer show a stacktrace when an error is caught?
show_errors: 1

# auto_reload is a development and experimental feature
# you should enable it by yourself if you want it
# Module::Refresh is needed 
# 
# Be aware it's unstable and may cause a memory leak.
# DO NOT EVER USE THIS FEATURE IN PRODUCTION 
# OR TINY KITTENS SHALL DIE WITH LOTS OF SUFFERING
auto_reload: 0

session: Simple
#session: YAML

plack_middlewares:
    -
        #- Session
        - CSRFBlock
        - Debug
        - panels
        -
            - Parameters
            - Dancer::Version
            - Dancer::Settings
            - Memory

路线是:

get '/test' => sub {
    return <<EOM
        <!DOCTYPE html>
        <html>
        <head><title>test route</title></head>
        <body>
            <form action="./foobar" method="post">
            <input type="text"/>
            <input type="submit"/>
            </form>
        </body>
        </html>
EOM
};

【问题讨论】:

    标签: perl csrf dancer plack


    【解决方案1】:

    好吧,我注意到 Debug 面板没有显示,这意味着 Plack::Middlewares::Debug 没有加载。 在 How to use Dancer with Plack middlewares | PerlDancer Advent Calendar Plack::Middleware::Debug::Dancer::Version 的帮助下,我设法打开了它

    session: PSGI
    ## Dancer::Session::PSGI
    
    plack_middlewares:
        -
            - Session
        -
            - CSRFBlock
        -
            - Debug
    ## panels is an argument for Debug, as in 
    ## enable 'Debug', panels => [ qw( Parameters Response Environment Session Timer Dancer::Logger Dancer::Settings Dancer::Version ) ];
            - panels
            -
                - Parameters
                - Response
                - Environment
                - Session
                - Timer
                - Dancer::Logger
                - Dancer::Settings
                - Dancer::Version
    #Plack::Middleware::Debug::Dancer::Version
    

    【讨论】:

    • 它就像一个魅力!谢谢你节省了我很多时间!我终于找到了关于 Dancer 和 PSGI 之间会话控制的答案。舞者::Session::PSGI
    猜你喜欢
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多