Olbyer 2022-03-10 10:40
浏览 230
已结题

SpringBoot +Shiro的WebUtils.issueRedirect() 问题

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");报跨域问题怎么解决,看有小伙伴遇到这种情况或者谁能解决?谢谢!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 3月18日
    • 创建了问题 3月10日

    悬赏问题

    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c