Acegi保护对方法的呼叫

Acegi保护对方法的呼叫
2008年05月12日 星期一 10:23
Acegi保护对方法的呼叫

Acegi是专为 Spring 设计的安全框架,藉由Spring所提供的AOP功能,
可以使用org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor来对方法呼叫进行拦截,
对方法的呼叫设定权限保护。

举个实际的例子来说,假设您设计了以下的介面与方法:

* ISome.java

package onlyfun.caterpillar;

public interface ISome {
public void doNormal();
public void doSupervisor();
}


* Some.java

package onlyfun.caterpillar;

public class Some implements ISome {
public void doNormal() {
System.out.println("do normal...");
}

public void doSupervisor() {
System.out.println("do supervisor...");
}
}


假设某个请求下,会对Some的实例之方法进行呼叫,例如某个Servlet:

* SomeServlet.java

package onlyfun.caterpillar;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SomeServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");

ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(
request.getSession().getServletContext());
ISome some = (ISome) ctx.getBean("some");
some.doNormal();
some.doSupervisor();

PrintWriter out = response.getWriter();
out.print("process successfully...");
out.close();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

public String getServletInfo() {
return "Short description";
}
}


在web.xml中增加SomeServlet的定义:
<servlet>
<servlet-name>SomeServlet</servlet-name>
<servlet-class>onlyfun.caterpillar.SomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SomeServlet</servlet-name>
<url-pattern>/SomeServlet</url-pattern>
</servlet-mapping>

在不设限的情况下,请求SomeServlet,会呼叫Some实例的doNormal()方法与doSecurity()方法,
现在假设您想让 doSecurity()只让ROLE_SUPERVISOR的使用者来呼叫,则您可以在acegi-config.xml中加入:

* acegi-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
...

<bean />

<bean
>
<property >
<ref bean="authenticationManager"/>
</property>
<property >
<ref bean="accessDecisionManager"/>
</property>
<property >
<value>onlyfun.caterpillar.ISome.doSupervisor=ROLE_SUPERVISOR</value>
</property>
</bean>

<bean
>
<property >
<list>
<value>some</value>
</list>
</property>
<property >
<list>
<value>methodSecurityInterceptor</value>
</list>
</property>
</bean>
</beans>


完成以下设定,如果再次请求SomeServlet,可以在控制台中看到doNormal()执行完成,
但doSecurity()必须是 ROLE_SUPERVISOR才可以存取,因此您会被送往acegilogin.jsp进行登入,
如果登入正确,就会执行doSecurity(),如果登入为非ROLE_SUPERVISOR,则会发生授权失败的例外。

本篇日志被作者设置为禁止发表新评论


©2008 Baidu



引文来源 Acegi保护对方法的呼叫_熊熊之家
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值