SpringBoot +Shiro + vue 基于PathMatchingFilter的权限认证
1.这是当前用户没有访问权限,然后跳转对应URL:
public class URLPathMatchingFilter extends PathMatchingFilter {
@Override
protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
log.info("当前用户没有访问接口" + requestAPI + "的权限");
WebUtils.issueRedirect(request, response, "/authentication/unauthorized");
// vue的devServer 配置 host: 'localhost',port: 8888,
// WebUtils.issueRedirect(request, response, "http://localhost:8888/api/authentication/unauthorized");
return false;
}
2.这是跳转异常处理
@Controller
@RequestMapping("/authentication")
public class Authentication {
@ResponseBody
@GetMapping(value = "/unauthorized")
public Result unauthorized(){
throw new UnauthorizedException();
}
}
3.然后统一处理
@ExceptionHandler({UnauthorizedException.class})
@ResponseBody
public Result unauthorizedExceptionHandler(UnauthorizedException e) {
log.error("资源不允许访问!", e);
return Result.error(ExceptionEnum.UNAUTHORIZED);
}
4运行结果及报错内容
WebUtils.issueRedirect(request, response, "/authentication/unauthorized");
当是这样跳转的话,会报跨域问题的错误
Access to XMLHttpRequest at 'http://localhost:8081/authentication/unauthorized' (redirected from 'http://localhost:8888/api/admin/userManagement/basic/getUserById') from origin 'http://localhost:8888' has been blocked by CORS policy: Request header field access-control-allow-methods is not allowed by Access-Control-Allow-Headers in preflight response.
但是我跨域都已经配置好了,访问其他页面都正常,就是WebUtils.issueRedirect跳转会报跨域的问题。
5.我的解答思路和尝试过的方法
WebUtils.issueRedirect(request, response, "http://localhost:8888/api/authentication/unauthorized");
这是我尝试的解决办法,可以正常跳转到异常处理。但总感觉不怎么好,这是vue配置localhost:8888/api
6.我想要达到的结果
我想要WebUtils.issueRedirect(request, response, "/authentication/unauthorized");报跨域问题怎么解决,看有小伙伴遇到这种情况或者谁能解决?谢谢!