简体   繁体   English

重定向到 laravel 中的外部 url 时如何保留 session 数据

[英]how to keep session data when redirect to external url in laravel

I am new on PHP.我是 PHP 的新手。 I have a laravel api and laravel webui in different servers.我在不同的服务器上有一个 laravel api 和 laravel webui。 When i make a request for login in webui, sent it to api and if result is success return laravel/passport token to webui.当我在 webui 中发出登录请求时,将其发送到 api,如果结果成功,则将 laravel/passport 令牌返回给 webui。 I stored token in session(in webui auth controller).我将令牌存储在会话中(在 webui 身份验证控制器中)。

AuthController;授权控制器;

Session::put('token', $value['token']);

My CustomAuth Middleware;我的 CustomAuth 中间件;

    class CustomAuth extends Middleware {
        public function handle($request, Closure $next, $guard = null){
            if (Session::has('token')) {
                return $next($request);
            } else {
                return response(view('pages.unauthorized'));
            }
        }
    }

Payment method;付款方式;

return redirect()->away($redirectUrl);

And then, when payment is success/fail wirecard returning to my site(callbackUrl).然后,当付款成功/失败时,wirecard 返回到我的网站(callbackUrl)。 In this section, session data is lost and user redirect to login page.在本节中,session 数据丢失,用户重定向到登录页面。 I am not sure whether I am wrong in the auth part or use the session incorrectly.我不确定我在 auth 部分是错的还是错误地使用了 session。 Can i store session data when i redirect?我可以在重定向时存储 session 数据吗? Or how can i change auth part?或者我该如何更改身份验证部分?

Note: success and fail routes has to be in auth middleware.注意:成功和失败路由必须在 auth 中间件中。 And my all routes in web middleware group.还有我在 web 中间件组中的所有路由。 In app/Http/Kernel.php, this line added in 'web'在 app/Http/Kernel.php 中,在 'web' 中添加了这一行

\Illuminate\Session\Middleware\StartSession::class,

Ideally when you are building an API, we are not using web middleware group but api middleware group.理想情况下,当您构建 API 时,我们不使用web中间件组,而是使用api中间件组。 Thus if all your routes are in api.php (they should be here) then the session is not activated or it won't work because you are using api middleware group and api guard here. Thus if all your routes are in api.php (they should be here) then the session is not activated or it won't work because you are using api middleware group and api guard here.

Another thing is, you have already generated a token using passport so you don't need to store the token in session .另一件事是,您已经使用passport生成了一个令牌,因此您不需要将令牌存储在session中。 That's the awesome thing about Json Web Token.这就是 Json Web 令牌的厉害之处。 It can be parsed when your WebUI pass the token back to backend.当您的WebUI将令牌传递回后端时,可以对其进行解析。 The backend/API can just look at it and see if the token is authenticated, no need to check session or anything like that when you are handling token.后端/API 只需查看它并查看令牌是否经过身份验证,在处理令牌时无需检查 session 或类似的东西。 To do so you have to pass through auth:api middleware for your api routes.为此,您必须为您的 api 路由通过auth:api中间件。

Lastly, you have to make sure that the WebUI is sending back the token in correct form(eg Bearer header, basic auth etc.).最后,您必须确保WebUI以正确的形式发回令牌(例如 Bearer header、基本身份验证等)。

Good luck!祝你好运!

One possible solution: when working with payment callback you should always remember the session data for addreses including https, http, www and none-www are different.一种可能的解决方案:在处理付款回调时,您应该始终记住 session 数据的地址,包括 https、http、www 和 none-www 是不同的。 You should always always force (www or none-www) and (https or http).您应该始终强制(www 或 none-www)和(https 或 http)。 in this way you can always be sure that user will always come back to the address that user session data is stored.这样,您始终可以确保用户将始终返回到存储用户 session 数据的地址。

according to web server you are using, the approach to do this will be different.根据您使用的 web 服务器,执行此操作的方法会有所不同。

for example if you are using apache, you can use following config in htaccess:例如,如果您使用的是 apache,您可以在 htaccess 中使用以下配置:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # remove wwww.
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,QSA,NC,L]

    # redirect to https
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 session 超时时如何配置重定向 URL - Laravel 5.8? - How to configure the redirect URL when session timeout - Laravel 5.8? 使用 Laravel 重定向到预期()时如何 flash session 数据? - How to flash session data when redirect to intended() using Laravel? 在 Laravel 8 中存储数据后如何保持会话? - How to keep session after storing data in Laravel 8? 重定向到外部 URL,并在 Laravel 中返​​回 - Redirect to external URL with return in laravel 无论Laravel是否提供http或https,如何重定向到外部链接(url) - How to redirect to an external link ( url ) no matter the http or https are given or not in Laravel Laravel 5.4 外部 url 重定向无法正常工作 - Laravel 5.4 Redirect Not Working Correctly for external url 使用 Laravel 重定向到外部 URL(跨域) - Redirect to external URL (cross domain) with Laravel Laravel5 使用参数重定向到外部 URL - Laravel5 Redirect to external URL with params Laravel 5:如何将数据重定向到外部资源表单控制器 - Laravel 5: how to redirect with data to external resource form controller 如何在不使用Session的情况下使用Laravel重定向将数据发送到视图? - How to send data to the view using redirect with Laravel without using Session?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM