我决定深入研究中间件,因为如果用户是预用户(只是尚未更改密码的用户),我需要检查每个请求,但更改密码路由除外。我的中间件代码如下:
public static void main(String[] args) throws IOException {
Parent p = new Parent();
Header h = new Header();
h.setUser("XXX");
h.setPassword("af962d0ceacdd9af");
h.setTable("licf");
h.setMethod_override("get");
p.setHeader(h);
Test t = new Test();
String jsonInString = t.writeJSON(p);
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
client.addFilter(new LoggingFilter());
WebResource service = client.resource("https://hostedactivation.com/XXXXX/XXXXXXXX.php");
ClientResponse client_response = service.type(MediaType.APPLICATION_JSON).
header("Authorization", "Basic Y3liZXJzcGFI3UGc5").
post(ClientResponse.class,jsonInString);
System.out.println("Status: "+client_response.getEntity(String.class));
client.destroy();
}
private String writeJSON(Parent p) throws JsonGenerationException, JsonMappingException, IOException{
ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(p);
return jsonInString;
}
我的路线名称为<?php
namespace App\Http\Middleware;
use Closure;
class IsPreuser
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (\Auth::user()->is_preuser)
{
if (\Route::getCurrentRoute()->getName() == 'profile.change_password')
{
echo 'hello';
}
//return redirect()->route('profile.change_password');
}
return $next($request);
}
}
。使用上面的代码我收到错误profile.change_password
我不明白。我的目标是:重定向到Call to a member function getName() on null
路由,除非用户已经在此路由中。
我的kernel.php:
profile.change_password
答案 0 :(得分:1)
好的,修好了。正如评论中所建议的那样,问题可能是放置我的中间件。我已将其移至$middlewareGroups
web
,并按预期正常工作,并重定向。
<?php
namespace App\Http\Middleware;
use Closure;
class IsPreuser
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (\Auth::check())
{
if (\Auth::user()->is_preuser)
{
if (! $request->routeIs('profile.change_password')
&& ! $request->routeIs('profile.change_password_process')
&& ! $request->routeIs('logout'))
{
return redirect()->route('profile.change_password');
}
}
}
return $next($request);
}
}