Spring AOP和ApacheShiro配置。未扫描批注 [英] Spring AOP and apache shiro configuration.Annotations not been scanned

查看:0
本文介绍了Spring AOP和ApacheShiro配置。未扫描批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为需要具备AOP知识的配置而苦苦挣扎。

我必须承认,AOP是我在一段时间内试图获得的部分,但没有成功。 我的Shiro注释似乎没有被扫描,因此被忽略了。

我试过用Shiro 1.1.0+maven3+Spring3.0.5.RELEASE,Hibernate3.6.1,最后用ZK 5.0.6。 我让我的休眠机制工作了,我和数据库交谈了,我让身份验证工作了,我成功地(我相信)加载了角色和权限。

为了测试授权端,我的代码中有以下内容:

  Subject currentUser = SecurityUtils.getSubject();
   if (!currentUser.isPermitted("businessaccount:list")) {
    throw new AuthorizationException("User not authorized");
  }

并且工作正常。
所以我知道我的权限已经加载了。我将使用注释来方便我将其放在实现类中,因为我一开始并不打算将接口与我的控制器类一起使用,这些控制器类正在扩展ZK GenericForwardController。

我已经看到了这个bug,我决定尝试使用一个方法上带有@RequiresPersmittions的接口。

显然它仍然不起作用,因为它允许未经授权的主体访问。我的日志中没有错误。可能我做错了什么,下面是代码片段:

@Component("layouteventhandler")
public class LayoutEventHandlerImpl extends GenericForwardComposer implements     LayoutEventHandler {

Logger logger = Logger.getLogger(LayoutEventHandlerImpl.class);
Menuitem logout;

//...


@Override
public void onClick$pAccounts() {
    try {
        execution.sendRedirect("/accounts/personal/list");
    } catch (Exception ex) {
        logger.info("Error redirecting to personal accounts", ex);
    }
}


@Override
public void onClick$bAccounts() {
  try {
        execution.sendRedirect("/accounts/business/list");
    } catch (Exception ex) {
        logger.info("Error redirecting to business accounts", ex);
    }
}
//.....
} 

其接口it:

public interface LayoutEventHandler {

@RequiresPermissions(value="personalaccount:list")
public void onClick$pAccounts();

@RequiresPermissions(value="businessaccount:list")
public void onClick$bAccounts();
//.....

 }

这是我的Shiro应用程序上下文

<bean id="hibernateRealm" class="com.personal.project.admin.webapp.security.DatabaseRealm" />
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <property name="realm" ref="hibernateRealm" />
</bean>

<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
      depends-on="lifecycleBeanPostProcessor">
 <!--          <property name="proxyTargetClass" value="true" />-->
</bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    <property name="securityManager" ref="securityManager"/>
</bean>

<!-- Secure Spring remoting:  Ensure any Spring Remoting method invocations can be associated
     with a Subject for security checks. -->
<bean id="secureRemoteInvocationExecutor" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">
    <property name="securityManager" ref="securityManager"/>
</bean>
<!-- ... -->

里面有什么我应该做的吗?感谢您的阅读和帮助

推荐答案

我不知道Shiro,但我猜您已经在实现接口的Bean类上添加了注释,然后将它们用于安全、事务和/或其他方面。当发生这种情况时,返回的对象是一个JDK动态代理,它不是您的Bean的具体类的实例,而只是它实现的接口的实例。因此,任何依赖于具体类中的批注的批注扫描都找不到它们。

这篇关于Spring AOP和ApacheShiro配置。未扫描批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆