【问题标题】:Using Apache Shiro SecurityUtils in a Filter在过滤器中使用 Apache Shiro SecurityUtils
【发布时间】:2017-03-17 03:04:57
【问题描述】:

我在 Spring Boot Web 应用程序中使用过滤器将我的所有用户请求记录到数据库中。我想记录的一件事是用户名,但是当我尝试让当前用户使用时:

SecurityUtils.getSubject().getPrincipal()

我收到以下错误:

No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton. This is an invalid application configuration.

我需要做什么才能让我的过滤器能够检索当前用户?

【问题讨论】:

  • 你确定shiro环境启动成功了吗?请分享您的 shiro.ini 文件

标签: spring-boot servlet-filters shiro


【解决方案1】:

您需要为过滤器映射定义至少一个 url: https://github.com/apache/shiro/blob/1.4.x/samples/spring-boot-web/src/main/java/org/apache/shiro/examples/WebApp.java#L96-L101

当然还有配置的 Shiro servlet 过滤器(我猜你已经有了)

看看上面的例子。

【讨论】:

  • 我有一个配置 bean,它返回一个 AbstractShiroFilter,并配置了 URL。但是,我用来记录用户的过滤器不是 ShiroFilter。有必要吗?我认为我可以在我的应用程序中的任何位置访问 SecurityUtils?
  • 只需将 Shiro 过滤器放在我们自己的过滤器前面,就可以了。
  • 把 Shiro 过滤器放在我的过滤器前面是什么意思?你的意思是 Shiro 过滤器必须在我的过滤器之前执行,还是你的意思是我需要在 Shiro 中包装我的过滤器?如果是前者,如何更改 Shiro 过滤器的排序?
  • Shiro 过滤器需要在过滤器链中先于您的过滤器执行。您应该可以使用org.springframework.boot.web.servlet.FilterRegistrationBean.setOrder() 来配置过滤器的顺序
  • 我已将过滤器上的顺序设置为尽可能大 (Integer.MAX_VALUE),但它仍然不起作用。
猜你喜欢
  • 2014-06-17
  • 2018-01-25
  • 2012-11-04
  • 2014-02-19
  • 1970-01-01
  • 2015-11-17
  • 2010-12-15
  • 2018-05-28
  • 2019-01-04
相关资源
最近更新 更多