【发布时间】:2011-10-15 02:58:31
【问题描述】:
我一直在努力解决需要 AOP 知识的配置。
我必须承认 AOP 是我试图获得一段时间但没有成功的那部分。 似乎我的 shiro 注释没有被扫描,因此被忽略了。
我尝试过使用 shiro 1.1.0+ maven3+spring 3.0.5.RELEASE,休眠 3.6.1.Final 和 ZK 5.0.6。 我让我的 hibernaterealm 工作,与数据库交谈,我让身份验证工作,我成功(我相信)加载了角色和权限。
所以为了测试授权方面,我的代码中有这样的地方:
Subject currentUser = SecurityUtils.getSubject();
if (!currentUser.isPermitted("businessaccount:list")) {
throw new AuthorizationException("User not authorized");
}
而且效果很好。
所以我知道我的权限已加载。我使用注释将它放在实现类中对我来说很方便,因为我不打算首先将接口与扩展 ZK GenericForwardController 的控制器类一起使用。
我已经看到了这个bug,并且我决定尝试使用一个带有@RequiresPersmissions on 方法的接口。
显然它仍然无法正常工作,因为它允许访问未经授权的主题。我的日志中没有错误。也许我做错了这里是代码的 sn-p:
@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);
}
}
//.....
}
它的界面吧:
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 注释如何被忽略?它们在您将它们连接到的类中是否为空?
-
您好,我已按您的要求添加了更多详细信息。谢谢
-
这还有兴趣吗?您在使用中是否有其他 AOP 方面(例如事务处理)?
-
是的,请在我的方法服务之上使用@transactional
-
你知道了吗。我也有同样的问题
标签: spring spring-aop shiro