【问题标题】:CDI enabled shiro filter启用 CDI 的 shiro 过滤器
【发布时间】:2019-01-06 09:29:52
【问题描述】:

关于创建启用 CDI 的 shiro 领域的信息已经足够多。就我而言,我需要在自定义 shiro 过滤器中使用 CDI,这可能吗?

public class com.moc.CustFilter {

  @Inject
  private MyUtil myUtil;

  // .... using myUtil

}

shiro.ini

[main]
custFilter = com.moc.CustFilter

[urls]
/web/** = custFilter

【问题讨论】:

标签: cdi shiro


【解决方案1】:

过滤器的构造函数中的这段代码对我来说已经足够了

private SomeUtils utils;

public ApisecAuthcFilter() {
    try {
      BeanManager beanManager = (BeanManager) new InitialContext().lookup("java:comp/BeanManager");
      Bean<SomeUtils> utilsBean = (Bean<SomeUtils>) beanManager.resolve(beanManager.getBeans(SomeUtils.class));
      CreationalContext<SomeUtils> creationalContext = beanManager.createCreationalContext(null);
      utils = utilsBean.create(creationalContext);
    } catch (NamingException e) {
      throw new SomesException();
    }
  }

【讨论】:

猜你喜欢
  • 2015-11-17
  • 2014-06-17
  • 2012-08-28
  • 2015-08-01
  • 2017-03-17
  • 2020-02-08
  • 2022-12-11
  • 2014-02-19
  • 2019-01-04
相关资源
最近更新 更多