如何禁用guzzle SSL验证laravel



我想用gmail登录,我创建laravel认证,我添加按钮登录与谷歌,我安装laravel socialite包由命令composer要求laravel/socialite,我添加配置在配置/服务:

'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => 'http://localhost:8000/laravel-socialite/public/login/google/callback',
],

我在routes/web.php中添加了routes:

Route::get ('login/google',[AppHttpControllersAuthLoginController::class, 'redirectToGoogle'])->name('login.google');
Route::get('login/google/callback',[AppHttpControllersAuthLoginController::class,'handleGoogleCallback']);

我添加了"login with google"按钮;url:

<a href="{{ route('login.google') }}" class="btn btn-danger btn-block">login with Google</a>

我把id和变量key放在。env文件中:

GOOGLE_CLIENT_ID="759714452408-gcbbdff9d7tp6215vgla9qhvrf60juh8.apps.googleusercontent.com"
GOOGLE_CLIENT_SECRET="ZKgJSqgibBNcHzD9vAMB80cw"

我创建了_registerOrLoginUser函数在loginController:

protected function _registerOrLoginUser ($ data)
{
$ user = User :: where ('email', '=', $ data-> email) -> first ();
if (! $ user) {
$ user = new User ();
$ user-> name = $ data-> name;
$ user-> email = $ data-> email;
$ user-> provider_id = $ data-> provider_id;
$ user-> avatar = $ data-> avatar;
$ user-> save ();
}
Auth :: login ($ user);
}

我在LoginController.php中添加了两个函数,并调用_registerOrLoginUser函数并重定向到"home"

public function redirectToGoogle () {
return Socialite :: driver ('google') -> redirect ();
}
public function handleGoogleCallback () {
$ user = Socialite :: driver ('google') -> user ();
$ this -> _ registerOrLoginUser ($ user);
return redirect () -> route ('home');
}

我创建了google应用程序来获取id和secret。

数据库/表用户迁移:

public function up ()
{
Schema :: create ('users', function (Blueprint $ table) {
$ table-> id ();
$ table-> string ('name');
$ table-> string ('email');
$ table-> timestamp ('email_verified_at') -> nullable ();
$ table-> string ('password');
$ table-> string ('provider_id') -> nullable ();
$ table-> string ('avatar') -> nullable ();
$ table-> rememberToken ();
$ table-> timestamps ();
});
}

最后,当我点击按钮"login with google"它打开gmail,我选择我的帐户,它给我错误:cURL error 77: error setting certificate verify locations。即使我已经下载了cacert。. pem文件拷贝到根目录D:wampbinphp php7.3.5extrasssl,修改php.ini

我发现解决方案是禁用guzzle ssl,所以添加代码:

$user->get('/url', ['verify' => false]);

但我不知道在哪里。

要回答您的问题,您的本地开发环境没有提供SSL证书。根据您的设置,您可以对此进行配置,但在开发过程中忽略它确实更容易。你离答案很近了。下面是如何配置它。我只在网站没有在生产环境中提供服务时才应用它。

public function handleGoogleCallback () {
if (app()->environment('local')) {
$user = Socialite::driver('google')
->setHttpClient(new GuzzleHttpClient(['verify' => false]))
->user();
} else {
$user = Socialite::driver('google')->user();
}
$this->_registerOrLoginUser($user);

return redirect()->route('home');
}

另外,当你定义你的重定向URL时,你可以将它指定为一个相对URL,这样它就不会硬编码你的本地URL结构。

'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => '/laravel/socialite/public/login/google/callback',
],

在Google Cloud Console中,您可以编辑您的OAuth凭据并列出尽可能多的允许重定向url。只需为每个域名添加一个,您将为您的网站提供服务。由于重定向在浏览器本地发生,所以添加代理域或localhost就可以了。只要您的本地系统配置为解析该地址,它将在OAuth流期间返回给浏览器。

最新更新