论坛首页 Java版 Spring

spring AOP能否从session取数据

浏览 4357 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (9) :: 隐藏帖 (0)
作者 正文
时间:2008-07-20
大家好,对于spring 我只会基本的应用,这几天碰到这个需求不知道自己的思路是否正确。
spring aop目前我只是用它的事务管理器而已,会跑个简单的AOP。现在做一个小的权限系统,比如我的切面是叫做
PermissionManager, 调用的是我的 ACLManager中的 hasPermission(int userID,String sn)这个方法来判断某个用户对某个模块是否有操作权限。在不改变原有代码的基础上,我试着拦截所有的ADD方法,通过这个permissionManager 来实现拦截,没有权限就把异常向上抛出。
现在的问题是,我用某个用户登陆,我发现我的切面不能获取当前登陆用户的ID,也就是说,我不知道使用AOP的时候怎么跟WEB交互。一个service 类怎么去获取session 对象。查了点资料,还是没有什么头绪。但是我觉得这个需求应该很多人都碰到过吧。希望大家指点 。
   
时间:2008-07-20
忘了说,我的框架是ssh struts2还没怎么用过
   
0 请登录后投票
时间:2008-07-20
自己写一个servlet filter把session丢到threadlocal里面
   
0 请登录后投票
时间:2008-07-20
谢谢楼上的
我new 一个类SystemContext
然后new一个ThreadLocal
private static ThreadLocal _session = new ThreadLocal();
public static HttpSession get_session() {
HttpSession session =(HttpSession)_session.get();
return session;
}

public static void set_session(HttpSession session) {
_session.set(session);
}


public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

SystemContext.set_session(httpRequest.getSession());


然后在程序中 service类使用SystemContext.get_session();就行了
是这样么??
   
1 请登录后投票
时间:2008-07-20
eseking 写道
大家好,对于spring 我只会基本的应用,这几天碰到这个需求不知道自己的思路是否正确。
spring aop目前我只是用它的事务管理器而已,会跑个简单的AOP。现在做一个小的权限系统,比如我的切面是叫做
PermissionManager, 调用的是我的 ACLManager中的 hasPermission(int userID,String sn)这个方法来判断某个用户对某个模块是否有操作权限。在不改变原有代码的基础上,我试着拦截所有的ADD方法,通过这个permissionManager 来实现拦截,没有权限就把异常向上抛出。
现在的问题是,我用某个用户登陆,我发现我的切面不能获取当前登陆用户的ID,也就是说,我不知道使用AOP的时候怎么跟WEB交互。一个service 类怎么去获取session 对象。查了点资料,还是没有什么头绪。但是我觉得这个需求应该很多人都碰到过吧。希望大家指点 。


可以获取的权限表的ID来进行判断···可以在action里面获取的得到权限表的某个字段的·进行SQL的操作,当你加载一个模块的时候,进行逻辑的判断,0和 1 分别表示,有权限,没有权限,本人是这样做的···?不知道其他人是怎么实现··不过大部门都是这样处理的。(前提的是你要写一个公共的权限分配方法和一个权限模块加载方法)
   
0 请登录后投票
时间:2008-07-20
wm920 写道
eseking 写道
大家好,对于spring 我只会基本的应用,这几天碰到这个需求不知道自己的思路是否正确。
spring aop目前我只是用它的事务管理器而已,会跑个简单的AOP。现在做一个小的权限系统,比如我的切面是叫做
PermissionManager, 调用的是我的 ACLManager中的 hasPermission(int userID,String sn)这个方法来判断某个用户对某个模块是否有操作权限。在不改变原有代码的基础上,我试着拦截所有的ADD方法,通过这个permissionManager 来实现拦截,没有权限就把异常向上抛出。
现在的问题是,我用某个用户登陆,我发现我的切面不能获取当前登陆用户的ID,也就是说,我不知道使用AOP的时候怎么跟WEB交互。一个service 类怎么去获取session 对象。查了点资料,还是没有什么头绪。但是我觉得这个需求应该很多人都碰到过吧。希望大家指点 。


可以获取的权限表的ID来进行判断···可以在action里面获取的得到权限表的某个字段的·进行SQL的操作,当你加载一个模块的时候,进行逻辑的判断,0和 1 分别表示,有权限,没有权限,本人是这样做的···?不知道其他人是怎么实现··不过大部门都是这样处理的。(前提的是你要写一个公共的权限分配方法和一个权限模块加载方法)



import com.xjj.oa.base.BeanUtil;
import com.xjj.oa.base.Page;
import com.xjj.oa.mainframe.MFInterface;
import com.xjj.oa.mainframe.MFInvalidDataException;
import com.xjj.oa.mainframe.MainframeException;
import com.xjj.oa.myletter.model.MyLetter;
import com.xjj.oa.myletter.service.MyLetterService;
import com.xjj.oa.right.vo.RightVO;
import com.xjj.oa.util.debug;
import com.xjj.oa.valueobject.DAOFactory;
import com.xjj.oa.valueobject.DAOObject;
import com.xjj.oa.workplace.common.AbsInfoListImp;
import com.xjj.oa.workplace.common.SearchTO;
import com.xjj.oa.workplace.common.vo.CommonInfoVO;
import com.xjj.webframe.address.dao.AddressListDAOImpl;

public class XjjMyLetterMFInfaceImpl extends AbsInfoListImp implements MFInterface {

public void addModuleAdmin(Vector user) throws MFInvalidDataException,
MainframeException {
String code = "MyLetter";
if (user == null)
throw new MFInvalidDataException(
"XjjMyLetterMFInfaceImpl.java invalid data in addModuleAdmin() user=null");
int i = user.size();
int ai[] = new int[i];
AddressListDAOImpl addresslistdaoimpl = new AddressListDAOImpl();
DAOObject RightDao = null;
try {
RightDao = DAOFactory.getRightDAO();
RightVO rvo = new RightVO();
rvo.setModelName(code);
rvo.setRight(0);
RightDao.delete(rvo);
} catch (Exception e) {
debug.println("清空权限表出错\n" + e.toString());
}
for (int j = 0; j < i; j++) {
ai[j] = ( (Integer) user.elementAt(j)).intValue();
int userid = addresslistdaoimpl.getUserById(ai[j]).getId();
System.out.println(userid);
RightVO vo = new RightVO();
try {
vo.setModelName(code);
vo.setUserId(userid);
vo.setCreater(userid);
vo.setRight(0); //模块管理员
RightDao.insert(vo);
} catch (Exception e) {
debug.println("设置模块管理员出错:" + e.toString());
}
}
}

public Vector getLinks(Vector arg0) throws MainframeException {
// TODO Auto-generated method stub
Vector vector1 = new Vector();
vector1.add("pubinfo/myletter/index.jsp");
return vector1;
}

public Vector getMessages(Vector arg0) throws MainframeException {
// TODO Auto-generated method stub
return null;
}

public Vector getModuleAdmin(Vector arg0) throws MainframeException {
// TODO Auto-generated method stub
Vector ver = new Vector();
AddressListDAOImpl addresslistdaoimpl = new AddressListDAOImpl();
DAOObject RightDao = null;
String modId = "MyLetter";
try {
RightDao = DAOFactory.getRightDAO();
RightVO rvo = new RightVO();
rvo.setModelName(modId);
rvo.setRight(0); //获取所有模块管理员
rvo.setPageCount(10000000); //获取所有
Iterator it = RightDao.findDataList(rvo).iterator();
int total = ( (Integer) it.next()).intValue();
while (it.hasNext()) {
rvo = (RightVO) it.next();
int j = rvo.getUserId();
ver.addElement(new Integer(j));
}
} catch (Exception exception) {
debug.println("XjjMyLetterMFInfaceImpl in getModuleAdmin()" +
exception.toString());
}
return ver;

}

public Vector getSourceTypes(Vector arg0) throws MainframeException {
// TODO Auto-generated method stub
return new Vector();
}

public void loop(Vector arg0) throws MainframeException {
// TODO Auto-generated method stub

}

public Vector getInfoList(SearchTO search) throws Exception {
Vector infos = new Vector();
MyLetter find = new MyLetter();
find.setUserid(search.getUserId());
if(search.getStartTime()!=null){
find.setStartTime(search.getStartTime());
}
if(search.getEndTime()!=null){
find.setEndTime(search.getEndTime());
}
if(search.getKeyword()!=null&&!search.getKeyword().equals("null")){
find.setTitle(search.getKeyword());
}
find.setPageCount(search.getPageCount());
find.setPageIndex(search.getPageIndex());
MyLetterService svr = (MyLetterService)BeanUtil.getBean("myLetterService");
Page pagedata = svr.getMyLetterList(find);
infos.add(new Integer(pagedata.getTotalSize()));
if(pagedata.getTotalSize()>0){
List list = (List)pagedata.getResult();
for(int i = 0;i<list.size();i++){
MyLetter vo = (MyLetter)list.get(i);
CommonInfoVO info = new CommonInfoVO();
info.setOpenNew(true);
info.setTypeName(getTopic());
info.setInfoTitle(vo.getTitle());
info.setPreeDate(vo.getCreatetime());
info.setUrl("pubinfo/myletter/opennew.jsp?id="+vo.getId());
info.setModelUrl("pubinfo/myletter/index.jsp");
info.setInfoId(vo.getId()+"");
infos.addElement(info);
}
}
return infos;
}
public CommonInfoVO loadInfoById(String keyId,SearchTO find) throws Exception{
CommonInfoVO vo = new CommonInfoVO();
MyLetterService svr = (MyLetterService)BeanUtil.getBean("myLetterService");
MyLetter letter = svr.loadMyLetter(keyId);
vo.setModelName(getTopic());
vo.setTypeName(getTopic());
vo.setInfoTitle(letter.getTitle());
vo.setPreeDate(letter.getCreatetime());
vo.setMainText(letter.getContent());
return vo;
}
public String getTopic() throws Exception {
// TODO Auto-generated method stub
return "*****************8";
}
}
   
1 请登录后投票
时间:2008-07-20
哈哈 谢谢了 已经搞定了

非常感谢
   
0 请登录后投票
时间:2008-07-20
可以获取的权限表的ID来进行判断···可以在action里面获取的得到权限表的某个字段的·进行SQL的操作,当你加载一个模块的时候,进行逻辑的判断,0和 1 分别表示,有权限,没有权限,本人是这样做的

这个思路倒是不错

问题又来了 我新加的拦截不知道是不是配置不对
拦截是实现了 可是以前的方法报错了
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
   
0 请登录后投票
时间:2008-07-20
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>

<!-- 配置事务特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>

<!-- 配置哪些类的方法需要进行事务管理 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* com.wenhua.managers.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>

<!-- 配置对ADD操作的权限验证 -->
<aop:config>
<aop:pointcut expression="execution(* com.wenhua.managers.*.addRole*(..)) " id="addMethod"/>

<aop:aspect id="securityAspect" ref="permissionManager">
<aop:before method="checkPermission" pointcut-ref="addMethod"/>
</aop:aspect>
</aop:config>


请指点下配置是哪里的问题
以前这个错是因为包名不对 导致那些方法只有READONLY 权限

<!-- 配置对ADD操作的权限验证 -->
<aop:config>
<aop:pointcut expression="execution(* com.wenhua.managers.*.addRole*(..)) " id="addMethod"/>

<aop:aspect id="securityAspect" ref="permissionManager">
<aop:before method="checkPermission" pointcut-ref="addMethod"/>
</aop:aspect>
</aop:config>


我加了这段 拦截com.wenhua.managers.*.addRole*(..))
addRole 方法 以前的操作都都报READONLY 了
请指点下
   
0 请登录后投票
时间:2008-07-20
貌似是因为所有的service 方法都配了事务管理

现在再次拦截 是不是两次拦截就会出问题?
   
0 请登录后投票
论坛首页 Java版 Spring

跳转论坛:
JavaEye推荐