欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Laravel API跨域访问的实现步骤

程序员文章站 2022-05-22 16:53:08
本篇文章给大家带来的内容是关于Laravel API跨域访问的实现步骤,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。 服务器A请求服务器B的接口,那么一般会出现跨域问题。 1 XMLHttpRequest cannot load http://api.console.vms3.co ......

本篇文章给大家带来的内容是关于laravel api跨域访问的实现步骤,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

服务器a请求服务器b的接口,那么一般会出现跨域问题。

1

xmlhttprequest cannot load http://api.console.vms3.com/api/user. no 'access-control-allow-origin' header is present on the requested resource. origin 'http://localhost:8080' istherefore not allowed access.

意思就是服务器响应不允许跨域访问.

那我们就需要让服务器支持跨域访问, 也就是在响应头部中添加

1

'access-control-allow-origin: *'

第一步: 创建中间件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

创建 `app/http/middleware/accesscontrolalloworigin.php` middleware 把 'access-control-allow-origin: *' 写入头部.

app/http/middleware/accesscontrolalloworigin.php

<?php

 

namespace app\http\middleware;

 

use closure;

use illuminate\support\facades\auth;

 

class accesscontrolalloworigin

{

    /**

     *

     * handle an incoming request.

     *

     * @param  \illuminate\http\request  $request

     * @param  \closure  $next

     * @return mixed

     */

    public function handle($request, closure $next)

    {

        header('access-control-allow-origin: *');

        header("access-control-allow-credentials: true");

        header("access-control-allow-methods: *");

        header("access-control-allow-headers: content-type,access-token");

        header("access-control-expose-headers: *");

 

        return $next($request);

    }

 

}

第二步: 注册路由

注册这个 middleware 到 kernel 中. 
分别在 protected $middleware 数组中和 protected $routemiddleware 数组中
添加我们刚才创建的那个文件class名, 使用 cors 这个别名.

第三步: 设置中间件保护接口

然后在设置它保护 api , 就是$middlewaregroups['api'] 的数组中添加它的别名, 本文中是 'cors'
app/http/kernel.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

<?php

 

namespace app\http;

 

use illuminate\foundation\http\kernel as httpkernel;

 

class kernel extends httpkernel

{

    /**

     * the application's global http middleware stack.

     *

     * these middleware are run during every request to your application.

     *

     * @var array

     */

    protected $middleware = [

        \illuminate\foundation\http\middleware\checkformaintenancemode::class,

        \illuminate\foundation\http\middleware\validatepostsize::class,

        \app\http\middleware\trimstrings::class,

        \illuminate\foundation\http\middleware\convertemptystringstonull::class,

        \app\http\middleware\accesscontrolalloworigin::class,

    ];

 

    /**

     * the application's route middleware groups.

     *

     * @var array

     */

    protected $middlewaregroups = [

        'web' => [

            \app\http\middleware\encryptcookies::class,

            \illuminate\cookie\middleware\addqueuedcookiestoresponse::class,

            \illuminate\session\middleware\startsession::class,

            // \illuminate\session\middleware\authenticatesession::class,

            \illuminate\view\middleware\shareerrorsfromsession::class,

            \app\http\middleware\verifycsrftoken::class,

            \illuminate\routing\middleware\substitutebindings::class,

        ],

 

        'api' => [

            'throttle:60,1',

            'bindings',

            'cors'

        ],

    ];

 

    /**

     * the application's route middleware.

     *

     * these middleware may be assigned to groups or used inpidually.

     *

     * @var array

     */

    protected $routemiddleware = [

        'auth' => \illuminate\auth\middleware\authenticate::class,

        'auth.basic' => \illuminate\auth\middleware\authenticatewithbasicauth::class,

        'bindings' => \illuminate\routing\middleware\substitutebindings::class,

        'can' => \illuminate\auth\middleware\authorize::class,

        'guest' => \app\http\middleware\redirectifauthenticated::class,

        'throttle' => \illuminate\routing\middleware\throttlerequests::class,

        'cors' => \app\http\middleware\accesscontrolalloworigin::class,

    ];

}

第四步:在路由中添加路由

1

2

3

route::middleware('cors')->group(function () {

    //

});

以上就是laravel api跨域访问的实现步骤的详细内容。