最代码广告位
最代码官方的gravatar头像
最代码官方2014-03-06 14:49:37

Struts-Menu角色权限的实例代码权限的实例代码

STRUTS-MENU简介:
STRUTS-MENU支持静态和动态菜单,数据来源和显示方式分开,菜单文字国际化,权限。

<menu:useMenuDisplayer name="Velocity" config="/framework/res/menucomp/templates/xtree.html" bundle="org.apache.struts.action.MESSAGE" repository="userMenuRepository" permissions="rolesAdapter"> 
 <logic:iterate id="menuName" name="userParentMenuNameList" indexId="index"> 
 <menu:displayMenu name='<%=menuName.toString()%> ' /> 
 </logic:iterate> 
 </menu:useMenuDisplayer> 


静态:只须在标签内加上

<menu:displayMenu name="Home" />


动态:在menu:useMenuDisplayer 中加上:repository="userMenuRepository"

显示样式:在menu:useMenuDisplayer 中加上:name="Velocity"

国际化:在menu:useMenuDisplayer 中加上:bundle="org.apache.struts.action.MESSAGE"

权限:在menu:useMenuDisplayer 中加上:permissions="rolesAdapter",如果名字是"rolesAdapter"则会自动调用
默认的net.sf.navigator.menu.RolesPermissionsAdapter:

/*
 * RolesPermissionsAdapter.java
 *
 * Created on December 7, 2002 2:25 PM
 */

package net.sf.navigator.menu;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Pattern;

/**
 * This class used container-managed security to check access
 * to menus.  The roles are set in menu-config.xml.
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
public class RolesPermissionsAdapter implements PermissionsAdapter {
    private Pattern delimiters = Pattern.compile("(?<!\\\\),");
    private HttpServletRequest request;

    public RolesPermissionsAdapter(HttpServletRequest request) {
        this.request = request;
    }

    /**
     * If the menu is allowed, this should return true.
     *
     * @return whether or not the menu is allowed.
     */
    public boolean isAllowed(MenuComponent menu) {
        if (menu.getRoles() == null) {
            return true; // no roles define, allow everyone
        } else {
            // Get the list of roles this menu allows
            String[] allowedRoles = delimiters.split(menu.getRoles());
            for (int i=0; i < allowedRoles.length; i++) {
                if (request.isUserInRole(allowedRoles[i])) {
                    return true;
                }
            }
        }
        return false;
    }
}


在显示菜单项时,会判断菜单所定义的角色是否与用户的角色吻合,是则显示,否则不显示。

如果使用ACEGI作为权限柜架,只须加上securityContextHolderAwareRequestFilter即可。

在前一个项目当中要根据登入用户的权限进行菜单显示:我把其中相关的代码提炼出来.
在系统启动时把菜单信息加载近来.通过扩展ActionServlet进行加载.

/**   
 *加载菜单静态信息  
 */  
private void loadMenuRepository()   
{   
     MenuConstructor menuConstructor=new MenuConstructor();   
             menuConstructor.createMenuRepository();   
}  


数据库表中一级菜单的roles值按2.4.8.16....增加 这样与用户的roles值进行位运算.只要值为1,就可以显示该菜单.

/**  
 * 加载系统菜单入菜单库  
 * @param defaultRepository :缺省库  
 */  
public void createMenuRepository()    
{   
    ServletContext app=ApplicationContext.getContext();   
       MenuRepository defaultRepository = (MenuRepository)app.getAttribute(MenuRepository.MENU_REPOSITORY_KEY);   
    MenuRepository repository = new MenuRepository();   
       repository.setDisplayers(defaultRepository.getDisplayers());   
       List list = getMenuComponents();   
       for (int i=0; i < list.size(); i++)    
       {   
           MenuComponent mc = new MenuComponent();   
           MenuInfo menu=(MenuInfo) list.get(i);   
           String menuName = menu.getName();   
           mc.setName(menuName);   
           String parentName = (String) menu.getParentName();   
           if (parentName != null)    
           {   
               MenuComponent parentMenu = repository.getMenu(parentName);   
               if (parentMenu == null)    
               {   
                   parentMenu = new MenuComponent();   
                   parentMenu.setName(parentName);   
                   repository.addMenu(parentMenu);   
               }   
               mc.setParent(parentMenu);   
           }   
           String title = (String)menu.getTitle();   
           String location = (String) menu.getLocation();   
           String target = (String) menu.getTarget();   
           String description = (String) menu.getDescription();   
           String action=(String)menu.getAction();   
           String roles=(String)menu.getRoles();   
           mc.setTitle(title);                           
           mc.setLocation(location);   
           mc.setAction(action);   
           mc.setTarget(target);               
           mc.setDescription(description);   
           mc.setRoles(roles);   
           repository.addMenu(mc);   
       }   
       constant.setMenuRepository(repository);   
       //因为加载出来的菜单信息被打乱了.   
       //按照库表菜单顺序保存一级菜单信息,在struts-menu显示时要用   
       app.setAttribute("OrderMenuList",getOrderMenuList());   
}   
  
/**  
 * 从数据库中读取菜单信息  
 * @return List :菜单信息  
 */  
   private static List getMenuComponents()    
   {   
    PersistenceService service = (PersistenceService)ApplicationContextProvider.getBean("persistenceService");   
       String hql=QlConstructor.getSqlContent("getMenuInfo");   
    List menuInfoList = service.list(hql);   
    return menuInfoList;   
   }   
      
   /**  
    * 将菜单表中的一级菜单按照显示顺序取出来  
    * @return List:一级菜单信息  
    */  
   private static List getOrderMenuList()   
   {   
       List orderMenuList=new ArrayList();   
       PersistenceService service = (PersistenceService)ApplicationContextProvider.getBean("persistenceService");   
       String hql=QlConstructor.getSqlContent("getParentmenuSql");   
       orderMenuList = service.list(hql);   
       return orderMenuList;   
   }  
/**  
 * 根据userId获取用户所能看到的菜单信息  
 * @param   authSet         :用户权限集  
 * @return  MenuRepository  : 用户所能看到的菜单库  
 */  
public MenuRepository getUserMenuRepository(long authSet)    
{   
    Constant constant=Constant.getInstance();   
    //获取系统所有菜单   
    ServletContext app=ApplicationContext.getContext();   
    MenuRepository defaultRepository = (MenuRepository)app.getAttribute(MenuRepository.MENU_REPOSITORY_KEY);   
    MenuRepository menuRepository=constant.getMenuRepository();   
    MenuRepository userMenuRepository=new MenuRepository();   
    userMenuRepository.setDisplayers(defaultRepository.getDisplayers());   
    Set menuSet=menuRepository.getMenuNames();   
    Iterator itor=menuSet.iterator();   
    MenuComponent menuComponent =null;   
    //查找满足用户权限的菜单项   
    while(itor.hasNext())   
    {   
        String menuName=(String)itor.next();   
        menuComponent = menuRepository.getMenu(menuName);   
        long roles=Long.parseLong(menuComponent.getRoles());   
        if((roles&authSet)!=0)   
        {   
            userMenuRepository.addMenu(menuComponent);                 
        }   
    }   
    //TODO:下面生成用户所能看到的菜单   
    userMenuRepository=menuRepository;   
    return userMenuRepository;   
}   
  
   /**  
    * 获取用户所能看到的一级菜单列表,在画面展示时struts-menu标签需要用到  
    * @param userMenuRepository    :用户自己的菜单库  
    * @return  List: 用户能看到的一级菜单名称数组  
    */  
public List getParentMenuNameList(MenuRepository userMenuRepository) throws Exception   
{           
       ServletContext app=ApplicationContext.getContext();   
       List orderMenuList=(List)app.getAttribute("OrderMenuList");   
       List parentMenuNameList=new ArrayList();   
       for(int i=0;i<orderMenuList.size();i++)   
       {   
           String menuName=((MenuInfo)orderMenuList.get(i)).getName();   
           if(userMenuRepository.getMenu(menuName)!=null)  parentMenuNameList.add(menuName);   
       }          
    return parentMenuNameList;   
}   


显示菜单:

<menu:useMenuDisplayer name="Velocity" config="/framework/res/menucomp/templates/xtree.html" bundle="org.apache.struts.action.MESSAGE" repository="userMenuRepository" permissions="menuPermession"> 
 <logic:iterate id="menuName" name="userParentMenuNameList" indexId="index"> 
 <menu:displayMenu name='<%=menuName.toString()%>' /> 
 </logic:iterate> 
 </menu:useMenuDisplayer> 

打赏

顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友