import orgliacegiTestAcegi;
import orgspringframeworkcontextApplicationContext;
import orgspringframeworkwebcontextsupportWebApplicationContextUtils;
public class TestServlet extends HttpServlet
{
private static final long serialVersionUID = L;
public void doGet(HttpServletRequest request HttpServletResponse response)
throws ServletException IOException
{
responsesetContentType(text/html;charset=GBK);
PrintWriter out = responsegetWriter();
ApplicationContext ctx =
WebApplicationContextUtilsgetRequiredWebApplicationContext(requestgetSession()getServletContext());
TestAcegi test = (TestAcegi)ctxgetBean(testAcegi);
testRole();//访问TestAcegi类的Role方法
outprintln(调用成功);
}
public void doPost(HttpServletRequest request HttpServletResponse response)
throws ServletException IOException
{
doGet(requestresponse);
}
}
准备工作做好了开始配置Acegi
先在Spring里给Acegi做个代理
<bean id=
autoProxyCreator
class=
org
springframework
aop
framework
autoproxy
BeanNameAutoProxyCreator
>
<property name=beanNames>
<list>
<value>testAcegi</value><! 要代理的Bean的id >
</list>
</property>
<property name=interceptorNames>
<list>
<value>methodSecurityInterceptor</value><! 代理为 >
</list>
</property>
</bean>
里面的methodSecurityInterceptor呢配置为
<bean id=
methodSecurityInterceptor
class=
org
acegisecurity
intercept
method
aopalliance
MethodSecurityInterceptor
>
<property name=authenticationManager>
<ref bean=authenticationManager/>
</property>
<property name=accessDecisionManager>
<ref bean=accessDecisionManager/>
</property>
<property name=objectDefinitionSource><! 对代理的类的方法开始配置权限 >
<value>orgliacegiTestAcegiRole=ROLE_USER</value>
</property>
</bean>
[] [] [] [] [] []