大家好,我是烤鸭:

最近使用shiro,遇到如下问题:

严重: Servlet.service() for servlet [dispatcherServlet] in context with path [/etc] threw exception [Request processing failed; nested exception is org.apache.shiro.authz.UnauthenticatedException: 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.] with root cause

1.    场景介绍

项目是前后端分离的,接口用postman自测的时候是没有问题的。但是前端登录后访问有权限限制的接口会报错。

前端是 react 项目,后端是 springboot 项目。

2.    原因猜想

可能是前端每次请求并没有携带cookie。由于前端项目本地启动请求后端项目需要使用代理。

默认访问的域名应该是localhost:3000

3.   解决方式

如果前端联调的是测试环境,建议将前端项目也部署测试环境,并且和后端项目部署在同一个域名下(nginx配置一下就可以了)。这种就不存在跨域和携带cookie的问题了。

如果前端联调的是开发同学的本地环境。需要前后端都做一些修改。

前端:

fetch请求默认不携带cookie

增加

credentials: "include"   

            var myHeaders = new Headers();fetch(url, {method: 'GET',headers: myHeaders,credentials: "include"})

withCredentials: true

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

后端:

如果是和本地联调,肯定存在跨域问题。需要设置  Access-Control-Allow-Origin 为指定ip,不能设置为 * 。

    浏览器的安全角度 如果设置 为 * ,是不能携带cookie的。

本例中如下设置。(Access-Control-Allow-Origin 设置 为 localhost:3000)

@Configuration
public class CorsConfig implements WebMvcConfigurer {private CorsConfiguration buildConfig() {CorsConfiguration corsConfiguration = new CorsConfiguration();corsConfiguration.addAllowedHeader("*"); // 允许任何头corsConfiguration.addAllowedOrigin("localhost:3000"); // 允许任何头corsConfiguration.addAllowedMethod("*"); // 允许任何方法(post、get等)return corsConfiguration;}@Beanpublic CorsFilter corsFilter() {UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", buildConfig()); // 对接口配置跨域设置return new CorsFilter(source);}}
@Overridepublic void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {//每一次的请求先校验cookieHttpServletRequest reqeust = (HttpServletRequest)req;HttpServletResponse response = (HttpServletResponse) res;response.setHeader("Access-Control-Allow-Origin", "localhost:3000");response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");response.setHeader("Access-Control-Max-Age", "3600");response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");chain.doFilter(req, res);}

总结:

最开始前端说后端接口访问不同,想到是shiro的问题,但是第一时间并没有想到cookie的问题。

找到问题比较慢,解决问题也是。最好的方式就是都部署到测试环境,避免跨域的问题出现就好了。

This subject is anonymous - it does not have any identifying principals and authorization operations相关推荐

  1. 关于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 au ...

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

    在进行shiro前后分离授权时,报错如下. This subject is anonymous - it does not have any identifying principals and au ...

  3. redis-shiro session 共享subject中principal 为空

    redis-shiro session共享,登陆后subject中principal 为空 看过我的上一篇文章 redis-shiro session共享,序列化大坑的人,你可能遇到一个新的问题,就是 ...

  4. java 控制台刷屏 dll_控制台被警告刷屏 · Issue #IXD8V · 卓源软件/JeeSite 4.2 - Gitee.com...

    ### 该问题是怎么引起的? debug日志不在控制台打印后,有两个问题: 1.浏览器登录后,长时间没有操作,掉出登录后,浏览器没关,控制台一直被没有权限的警告刷屏 2.前台页面跳转过程中,一直在控制 ...

  5. Shiro报异常org.apache.shiro.authz.AuthorizationException: Not authorized to invoke method:

    异常如下 2021-02-12 13:38:13.388 ERROR 6052 --- [io-8080-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet] ...

  6. Shiro源码学习之二

    接上一篇 Shiro源码学习之一 3.subject.login 进入login public void login(AuthenticationToken token) throws Authent ...

  7. 安装与使用 Trac 来管理项目

    1 概要 以 Windows 平台为例,讲述一下有关 Trac 的基本配置与使用,同时分享一下有关 Trac 的项目管理经验. 2 安装 Trac 前的准备工作 2.1 准备工作 安装 Python ...

  8. Apache Shiro 全面源码解析汇总

    什么是shiro? Apache Shiro官网上对Shiro的解释如下: Apache Shiro (pronounced "shee-roh", the Japanese wo ...

  9. Shiro登录的使用以及原理(一)

    好久没写博客了,这段时间对最近项目做个总结,先从登入下手,话不多说直奔主题,Shiro的登录使用以及原理. 目录 一.Shiro主要作用 二.登录的使用 2.1 SecurityManager的生成与 ...

最新文章

  1. BPW34F红外光电传感器
  2. 几个小例子告诉你, 一行Python代码能干哪些事
  3. PyTorch框架学习十三——优化器
  4. Kubernetes Node Controller源码分析之配置篇
  5. HOWTO: 如何在InstallShield的MSI工程中调用Merge Module的Custom Action
  6. 海康—SADP激活(设备网络搜索)
  7. 自回归滑动平均(ARMA)模型
  8. 英雄联盟修复服务器,快速修复win10专业版玩英雄联盟连接不上服务器方法
  9. 高级程序员和普通程序员有哪些区别?
  10. Zynga公布2021年第二季度财务业绩
  11. 全民奇迹局域网php版,手游全民奇迹8.3镜像端-配套客户端+局域网ip架设+安卓端...
  12. 安装Kali Linux渗透系统
  13. 计算机文化起源于,计算机文化.pps
  14. 2019高考(高中)数学必背重点公式大全
  15. python鼠标绘图_python opencv入门 鼠标绘图(4)
  16. 备受推崇的《R语言实战》真的值得如此好评吗?
  17. Go语言GoFrame开发框架
  18. CVPR 2021 自动驾驶相关论文解读
  19. C语言学习(四)字符串和格式化输入/输出
  20. 【Copula】基于二元Frank-Copula函数的风光出力场景生成方法【考虑风光出力的不确定性和相关性】(Matlab代码实现)

热门文章

  1. 前端学习(3133):react-hello-react之高阶函数
  2. [html] 如何让<p>测试 空格</p>这两个词之间的空格变大?
  3. [vue] 说说你对provide和inject的理解
  4. [css] 你有用过clip-path吗?说说你对它的理解和它都有哪些运用场景?
  5. 前端学习(2025)vue之电商管理系统电商系统之渲染订单列表数据
  6. 前端学习(1897)vue之电商管理系统电商系统之实现搜索功能
  7. 前端学习(1840):前端面试题之小程序入门
  8. 前端学习(1797):前端调试之html/css介绍
  9. “睡服”面试官系列第二十二篇之class的继承(建议收藏学习)
  10. 前端学习(833):操作元素小结