使用auth护照系统覆盖laravel中api url的auth重定向

时间:2018-05-27 06:15:18

标签: laravel authentication redirect

我已经创建了一些工作正常的api。现在当有人点击那个在Web浏览器上获得请求的受保护的api网址时,它会重定向到auth系统的登录。我该如何覆盖此功能?

完整详情

我在laravel应用程序中工作,我已经实施了护照和身份验证系统。这个项目到目前为止还没有任何网络界面。 这是我的api.php

import tkinter as tk
from tkinter import messagebox
import random

one_flag= True
two_flag= True
three_flag= True
four_flag= True
five_flag= True
six_flag= True
seven_flag= True
eight_flag= True
nine_flag= True
pk = tk.Tk()
frame = tk.Frame(pk)
frame.grid()
def X():
    print("test")
def Y1():
    global one_flag
    if one_flag:
        print("Your Marker Is Now On This Square")
        one.config(text="X",fg='red')
        one_flag = False
def Y2():
    global two_flag
    if two_flag:
        print("Your Marker Is Now On This Square")
        two.config(text="X",fg='red')
        two_flag = False
def Y3():
    global three_flag
    if three_flag:
        print("Your Marker Is Now On This Square")
        three.config(text="X",fg='red')
        three_flag = False
def Y4():
    global four_flag
    if four_flag:
        print("Your Marker Is Now On This Square")
        four.config(text="X",fg='red')
        four_flag = False
def Y5():
    global five_flag
    if five_flag:
        print("Your Marker Is Now On This Square")
        five.config(text="X",fg='red')
        five_flag = False
def Y6():
    global six_flag
    if six_flag:
        print("Your Marker Is Now On This Square")
        six.config(text="X",fg='red')
        six_flag = False
def Y7():
    global seven_flag
    if seven_flag:
        print("Your Marker Is Now On This Square")
        seven.config(text="X",fg='red')
        seven_flag = False
def Y8():
    global eight_flag
    if eight_flag:
        print("Your Marker Is Now On This Square")
        eight.config(text="X",fg='red')
        eight_flag = False
def Y9():
    global nine_flag
    if nine_flag:
        print("Your Marker Is Now On This Square")
        nine.config(text="X",fg='red')
        nine_flag = False
def C1():
    global one_flag
    if one_flag:
        print("The Computer's Marker Is Now On This Square")
        one.config(text="O",fg='blue')
        one_flag = False
def C2():
    global two_flag
    if two_flag:
        print("The Computer's Marker Is Now On This Square")
        two.config(text="O",fg='blue')
        two_flag = False
def C3():
    global three_flag
    if three_flag:
        print("The Computer's Marker Is Now On This Square")
        three.config(text="O",fg='blue')
        three_flag = False
def C4():
    global four_flag
    if four_flag:
        print("The Computer's Marker Is Now On This Square")
        four.config(text="O",fg='blue')
        four_flag = False
def C5():
    global five_flag
    if five_flag:
        print("The Computer's Marker Is Now On This Square")
        five.config(text="O",fg='blue')
        five_flag = False
def C6():
    global six_flag
    if six_flag:
        print("The Computer's Marker Is Now On This Square")
        six.config(text="O",fg='blue')
        six_flag = False
def C7():
    global seven_flag
    if seven_flag:
        print("The Computer's Marker Is Now On This Square")
        seven.config(text="O",fg='blue')
        seven_flag = False
def C8():
    global eight_flag
    if eight_flag:
        print("The Computer's Marker Is Now On This Square")
        eight.config(text="O",fg='blue')
        eight_flag = False
def C9():
    global nine_flag
    if nine_flag:
        print("The Computer's Marker Is Now On This Square")
        nine.config(text="O",fg='blue')
        nine_flag = False

one=tk.Button(pk,
            text="1",
            fg="blue",
            command=Y1)
two=tk.Button(pk,
            text="2",
            fg="blue",
            command=Y2)
three=tk.Button(pk,
            text="3",
            fg="blue",
            command=Y3)
four=tk.Button(pk,
            text="4",
            fg="blue",
            command=Y4)
five=tk.Button(pk,
            text="5",
            fg="blue",
            command=Y5)
six=tk.Button(pk,
            text="6",
            fg="blue",
            command=Y6)
seven=tk.Button(pk,
            text="7",
            fg="blue",
            command=Y7)
eight=tk.Button(pk,
            text="8",
            fg="blue",
            command=Y8)
nine=tk.Button(pk,
            text="9",
            fg="blue",
            command=Y9)

one.grid(row=0,column=0)
two.grid(row=0,column=1)
three.grid(row=0,column=2)
four.grid(row=1,column=0)
five.grid(row=1,column=1)
six.grid(row=1,column=2)
seven.grid(row=2,column=0)
eight.grid(row=2,column=1)
nine.grid(row=2,column=2)


pk.mainloop()
ho=one_flag,two_flag,three_flag,four_flag,five_flag,six_flag,seven_flag,eight_flag,nine_flag

boxes=['one','two','three','four','five','six','seven','eight','nine']
if one_flag==False:
    boxes.remove('one')
if two_flag==False:
    boxes.remove('two')
if three_flag==False:
    boxes.remove('three')
if four_flag==False:
    boxes.remove('four')
if five_flag==False:
    boxes.remove('five')
if six_flag==False:
    boxes.remove('six')
if seven_flag==False:
    boxes.remove('seven')
if eight_flag==False:
    boxes.remove('eight')
if nine_flag==False:
    boxes.remove('nine')

cs=random.choice(boxes)
if cs=='one':
    C1()
if cs=='two':
    C2()
if cs=='three':
    C3()
if cs=='four':
    C4()
if cs=='five':
    C5()
if cs=='six':
    C6()
if cs=='seven':
    C7()
if cs=='eight':
    C8()
if cs=='nine':
    C9()
print(boxes)

这是我的web.php

Route::group(['middleware' => ['auth:api'],'namespace' => 'Api', 'prefix' => '/v1'], function () {
    Route::get('/user/profile', 'UserController@profile');
    Route::get('/logout', 'Auth\LoginController@logout');
});
Route::group(['middleware' => ['api'], 'namespace' => 'Api', 'prefix' => '/v1'], function () {
    Route::post('/auth/register', 'Auth\RegisterController@create');
    Route::post('/auth/login', 'Auth\LoginController@login');
    Route::post('/auth/password/email', 'Auth\ForgotPasswordController@getResetToken');
    Route::post('/auth/password/reset', 'Auth\ResetPasswordController@reset');
});

当我点击

  

{{SITE_URL}} / API / V1 /用户/简档

如果没有在邮递员中传递或使用无效的访问令牌,则会返回以下错误(这是正常的)。

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

但是当我在网络浏览器中点击此网址时,它会将我重定向到登录页面。

1 个答案:

答案 0 :(得分:2)

我通过将这些功能添加到/app/Exceptions/Handler.php来解决了这个问题:

public function unauthenticated($request, AuthenticationException $exception){
    if($request->expectsJson()){
        return response()->json('Please login',401);
    }
    return redirect()->guest('admin/login');
}

public function unauthorized($request, AuthorizationException $exception){
    if($request->expectsJson()){
        return response()->json("You don't have permission to do this",401);
    }
    return redirect()->guest('login');
}