关于shiro授权 This subject is anonymous - it does not have any identifying principals and authorization

在进行shiro前后分离授权时,报错如下。

This subject is anonymous - it does not have any identifying principals and authorization operations require an identity to check against.  A Subject instance will acquire these identifying principals automatically after a successful login is performed be executing org.apache.shiro.subject.Subject.login(AuthenticationToken) or when 'Remember Me' functionality is enabled by the SecurityManager.  This exception can also occur when a previously logged-in Subject has logged out which makes it anonymous again.  Because an identity is currently not known due to any of these conditions, authorization is denied.

找了很久,尝试过很多配置,不报这个错又会有新的错误,最后还是无法解决shiro前端后端分离时无法授权的问题。
但是在查阅资料的时候大概了解了一些,应该是前后分离shiro的主体(subject )传递需要用到cookie 。
于是解决方法如下:
1、前端基于Vue 所以在发起ajax请求时带上cookie需要进行如下配置,可以配置在全局axios中,或者配置在main.js中

import axios from 'axios'

axios.defaults.withCredentials=true;//让ajax携带cookie

2、配置好携带cookie可能会报跨域问题,在后端跨域过滤器需要配置如下

resp.setHeader("Access-Control-Allow-Origin", "http://localhost:8088");//这里一定要指定源地址,不能为*

resp.setHeader("Access-Control-Allow-Credentials", "true");//允许cookie

关键代码就以上两步,给大家做个参考,遇到类似问题可以安装这个思路尝试!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值