$30 off During Our Annual Pro Sale. View Details »

Lumenで堅牢なAPIを設計する。

 Lumenで堅牢なAPIを設計する。

PHPerkaigi2018のLTで発表した内容です。
https://phperkaigi.jp/2018/proposal/c4c6998a-6433-4c5b-aa82-d13d4a03a023

Futoshi Endo

March 10, 2018
Tweet

More Decks by Futoshi Endo

Other Decks in Technology

Transcript

  1. ԕ౻ଠಙ(.01FQBCP *OD
    1)1FS,BJHJ
    -VNFOͰݎ࿚ͳ"1*Λઃܭ͢Δ

    View Slide

  2. ԕ౻ଠಙ ͑ΜͲ͎ʔ
    !'FOEP
    4)ࣄۀ෦άʔϖ8FCΤϯδχΞ

    View Slide


  3. -BSBWFMͷνϡʔτϦΞϧΛ࡞ͬͯΈͨɻ ςετۦಈ։ൃೖ໳

    View Slide

  4. ࿩͞ͳ͍ࣄ͓࿳ͼ

    View Slide

  5. ςετͷ࿩

    View Slide

  6. ςετͷ࿩
    ઃܭͷ࿩

    View Slide

  7. View Slide

  8. ςετͷ࿩
    ઃܭͷ࿩
    3&45GVMͷ࿩

    View Slide

  9. View Slide

  10. ʮ8FC"1*(PPE1BSUTʯ
    Λಡ΋͏

    View Slide

  11. ݎ࿚ͱ͸

    View Slide

  12. ݎ࿚ͱ͸
    8FC"1*Ͱ͸௨ৗͷ΢ΣϒΞϓϦέʔγϣϯͱಉ༷ʹ)551Λ௨͡
    ͯެ։͞ΕαʔϏεͰ͔͢Βɺಉ༷ʹ҆ఆੑ΍ηΩϡϦςΟ͕ཁٻ
    ͞Ε·͢ɻ
    l8FC"1*5IF(PPE1BSUTୈষΑΓ Q
    z

    View Slide

  13. ηΩϡΞͰ҆ఆͨ͠"1*Λߏங͢Δ

    View Slide

  14. ݎ࿚ͳ"1*Λઃܭ͢Δ
    3FRVFTU
    Middleware
    3PVUF $POUSPMMFS
    "QQMJDBUJPO
    .PEFM
    3FTQPOT

    View Slide

  15. ݎ࿚ͳ"1*Λઃܭ͢Δ
    ɾ08"414FDVSF)FBEFSTͷઃఆ
    ɾϒϥ΢βܦ༝Ͱͷ"1*ΞΫηεʹΑΔ߈ܸରࡦ
    ɾ5ISPUUMFΛ࢖ͬͨΞΫηε੍ݶͷઃఆ
    ɾ%P4߈ܸରࡦ
    wϥΠϒϥϦΛ࢖ͬͨೝূ
    ɾKXUBVUI
    ࣌ؒͷ౎߹্঺հͰ͖ͳ͔ͬͨ෦෼-VNFOͰग़དྷΔηΩϡϦςʔରԠʹؔ͢Δ5JQT

    View Slide

  16. 08"414FDVSF)FBEFSTͷઃఆ

    View Slide

  17. 08"414FDVSF)FBEFSTͷઃఆ
    ΞϓϦέʔγϣϯͷηΩϡϦςΟΛڧԽ͢Δҝʹ࢖༻Ͱ͖Δ)551Ϩεϙϯεϔο
    μʔͷ஋Λ08"4"1͕ఆΊ͍ͯΔ஋ʹઃఆ͢ΔࣄͰ944΍+40/ϋΠδϟοΫͳ
    Ͳͷϒϥ΢β͔ΒͷΞΫηεΛ૝ఆͨ͠৔߹ͷ੬ऑੑΛར༻ͨ͠߈ܸʹ༗ޮͰ͢ɻ

    View Slide

  18. 08"414FDVSF)FBEFSTͷઃఆ
    .JEEMFXBSFʹ௥Ճ͢Δɻ

    View Slide

  19. CPPUTUSBQBQQQIQʹొ࿥͢Δɻ
    $app->routeMiddleware([
    'security' => App\Http\Middleware\SecurityHeaders::class,
    ]);
    SPVUFTXFCQIQͰݺͼग़͢ɻ
    $router->group(['prefix' => 'api', 'middleware'=>'security'], function() use
    ($router){
    $router->get('hoges', ['uses' => 'HogesController@showAllHoges']);
    });
    08"414FDVSF)FBEFSTͷઃఆ

    View Slide

  20. 5ISPUUMFΛ࢖ͬͨΞΫηε੍ݶͷઃఆ

    View Slide

  21. 5ISPUUMFΛ࢖ͬͨΞΫηε੍ݶͷઃఆ
    ΞΫηεʹϨʔτϛϦοτΛ͔͚Δࣄ͕Ͱ͖Δ.JEEMFXBSFɻ
    %P4߈ܸͳͷͲͷେྔΞΫηεΛ๷͛Δɻ
    ˞-VNFOʹ͸ඪ४ͰඋΘͬͯͳ͍ͷͰɺ(JUIVC͔Β௚઀औಘͯ͠
    -VNFO༻ʹΧελϚΠζ͢Δɻ
    ‘throttleɿ3,1’ 1෼ؒʹ3ճͷϦΫΤετΛڐՄ͢Δɻ
    ‘throttleɿ300,1’1෼ؒʹ300ճͷϦΫΤετΛڐՄ͢Δɻ

    View Slide

  22. 5ISPUUMFΛ࢖ͬͨΞΫηε੍ݶͷઃఆ
    3PVUF XFCQIQ
    Ͱࢦఆ͢Δɻ
    $curl localhost:8010
    {"status":429,"message":"Too Many Attempts."}
    ઃఆ਺Ҏ্ʹϦΫΤετ͢ΔͱͱΤϥʔϝοηʔδΛฦ͢
    $router->group(['middleware' => 'throttle:3,1'], function () use ($router) {
    $router->get('/', function () use ($router) {
    return view('index');
    });
    });

    View Slide

  23. ϥΠϒϥϦΛ࢖ͬͨೝূ

    View Slide

  24. ϥΠϒϥϦΛ࢖ͬͨೝূ
    ϥΠϒϥϦແ͠Ͱͷ-VNFOͰͷೝূ͸ਏ͍ʜ

    View Slide

  25. ϥΠϒϥϦΛ࢖ͬͨೝূ
    -VNFO͸-BSBWFMͷެࣜύοέʔδͰ͋Δ1BTTQPSU ೝূύοέʔδ
    Λ
    αϙʔτͯ͠ͳ͍ɻ
    ˞ඇެࣜͰͳΒଘࡏ͢Δ͕ෆ҆ఆ

    0"VUIͳͲΛ࠷ॳ͔Β૊΋͏ͱ͢Δͱগ͠େมɻ
    ϥΠϒϥϦແ͠Ͱͷ-VNFOͰͷೝূ͸ਏ͍ʜ

    View Slide

  26. ϥΠϒϥϦΛ࢖ͬͨೝূ

    View Slide

  27. ͜Εͩ

    View Slide

  28. υΩϡϝϯτ͸Ͳͩ͜

    View Slide

  29. ϥΠϒϥϦΛ࢖ͬͨೝূ
    ৄࡉ(VJEFGPSTFUUJOHVQXJUI-VNFO

    View Slide

  30. ϥΠϒϥϦΛ࢖ͬͨೝূ

    View Slide

  31. ಈ͔Ͷ͐

    View Slide

  32. -VNFOͷ৔߹͸KXUPBVUI !EFW
    PSMBUFTUΛΠϯετʔϧ͢Δ
    $composer require tymon/jwt-auth:"^1.0@dev"
    ϥΠϒϥϦΛ࢖ͬͨೝূ

    View Slide

  33. ಈ͍ͨ

    View Slide

  34. ͨͩʜ

    View Slide

  35. ϥΠϒϥϦΛ࢖ͬͨೝূ

    View Slide

  36. ϥΠϒϥϦΛ࢖ͬͨೝূ

    View Slide

  37. ϥΠϒϥϦΛ࢖ͬͨೝূ

    View Slide

  38. ҆ఆͯ͠ͳ͍ʜ

    View Slide

  39. ·ͱΊ

    View Slide

  40. ɾηΩϡϦςΟʔʹ͍ͭͯ༨Γ஌͕ࣝͳ͔͕ͬͨ
    .JEEMFXBF΍ϥΠϒϥϦʔΛ௥Ճ͢Ε͹ָʹରࡦ͕Ͱ͖Δ
    ·ͱΊ

    View Slide

  41. ɾηΩϡϦςΟʔʹ͍ͭͯ༨Γ஌͕ࣝͳ͔͕ͬͨ
    .JEEMFXBF΍ϥΠϒϥϦʔΛ௥Ճ͢Ε͹ָʹରࡦ͕Ͱ͖Δ
    ɾೝূपΓ͸-BSBWFMͱൺ΂Δͱ-VNFO͸ਏ͍ɻ
    ·ͱΊ

    View Slide

  42. ͓΍
    ·ͱΊ

    View Slide

  43. ݎ࿚ͳ"1*Λઃܭ͢Δ
    ɾ08"414FDVSF)FBEFSTͷઃఆ
    ɾϒϥ΢βܦ༝Ͱͷ"1*ΞΫηεʹΑΔ߈ܸରࡦ
    ɾ5ISPUUMFΛ࢖ͬͨΞΫηε੍ݶͷઃఆ
    ɾ%P4߈ܸରࡦ
    wϥΠϒϥϦΛ࢖ͬͨೝূ
    ɾKXUBVUI
    ࣌ؒͷ౎߹্঺հͰ͖ͳ͔ͬͨ෦෼-VNFOͰग़དྷΔηΩϡϦςʔରԠʹؔ͢Δ5JQT

    View Slide

  44. ݎ࿚ͳ"1*Λઃܭ͢Δ
    ɾ08"414FDVSF)FBEFSTͷઃఆ
    ɾϒϥ΢βܦ༝Ͱͷ"1*ΞΫηεʹΑΔ߈ܸରࡦ
    ɾ5ISPUUMFΛ࢖ͬͨΞΫηε੍ݶͷઃఆ
    ɾ%P4߈ܸରࡦ
    wϥΠϒϥϦΛ࢖ͬͨೝূ
    ɾKXUBVUI
    ࣌ؒͷ౎߹্঺հͰ͖ͳ͔ͬͨ෦෼-VNFOͰग़དྷΔηΩϡϦςʔରԠʹؔ͢Δ5JQT
    ͦΕ-BSBWFMͰ΋Ͱ͖ΔΑ

    View Slide

  45. ݎ࿚ͳ"1*Λઃܭ͢Δ
    ɾ08"414FDVSF)FBEFSTͷઃఆ
    ɾϒϥ΢βܦ༝Ͱͷ"1*ΞΫηεʹΑΔ߈ܸରࡦ
    ɾ5ISPUUMFΛ࢖ͬͨΞΫηε੍ݶͷઃఆ
    ɾ%P4߈ܸରࡦ
    wϥΠϒϥϦΛ࢖ͬͨೝূ
    ɾKXUBVUI
    ࣌ؒͷ౎߹্঺հͰ͖ͳ͔ͬͨ෦෼-VNFOͰग़དྷΔηΩϡϦςʔରԠʹؔ͢Δ5JQT
    ͦΕ-BSBWFMͰ
    ඪ४Ͱ͋ΔΑ
    ͦΕ-BSBWFMͰ΋Ͱ͖ΔΑ

    View Slide

  46. ݎ࿚ͳ"1*Λઃܭ͢Δ
    ɾ08"414FDVSF)FBEFSTͷઃఆ
    ɾϒϥ΢βܦ༝Ͱͷ"1*ΞΫηεʹΑΔ߈ܸରࡦ
    ɾ5ISPUUMFΛ࢖ͬͨΞΫηε੍ݶͷઃఆ
    ɾ%P4߈ܸରࡦ
    wϥΠϒϥϦΛ࢖ͬͨೝূ
    ɾKXUBVUI
    ࣌ؒͷ౎߹্঺հͰ͖ͳ͔ͬͨ෦෼-VNFOͰग़དྷΔηΩϡϦςʔରԠʹؔ͢Δ5JQT
    ͦΕ-BSBWFMͰ
    ඪ४Ͱ͋ΔΑ
    ͦΕ-BSBWFMͷ

    1BTTQPSUศརͩΑʂ
    ͦΕ-BSBWFMͰ΋Ͱ͖ΔΑ

    View Slide

  47. ·ͱΊ
    ͋

    View Slide

  48. ԕ౻ଠಙ(.01FQBCP *OD
    1)1FS,BJHJ
    -VNFOͰݎ࿚ͳ"1*Λઃܭ͢Δ

    View Slide

  49. ԕ౻ଠಙ(.01FQBCP *OD
    1)1FS,BJHJ
    -VNFOͰݎ࿚ͳ"1*Λઃܭ͢Δ

    View Slide

  50. ԕ౻ଠಙ(.01FQBCP *OD
    1)1FS,BJHJ
    -VNFOͰݎ࿚ͳ"1*Λઃܭ͢Δ
    -BSBWFMͳΒΑΓݎ࿚ͳ"1*ΛઃܭͰ͖Δ

    View Slide

  51. View Slide

  52. ·ͱΊ
    -BSBWFMͰݎ࿚ͳ"1*Λ࡞Ζ͏

    View Slide

  53. ͦΕͰ΋-VNFOͰݎ࿚ͳ"1*Λ࡞Γ͍ͨํ΁
    -VNFOͰग़དྷΔηΩϡϦςΟରࡦʹؔ͢Δ5JQT

    View Slide

  54. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ
    ϓϩϑΟʔϧGFOEP1SPpMF

    View Slide