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

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

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

原创整理不易,转载请注明出处:Struts-Menu角色权限的实例代码

代码下载地址:http://www.zuidaima.com/share/1724475036945408.htm

 

静态:只须在标签内加上

1 <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:

01 /*
02  * RolesPermissionsAdapter.java
03  *
04  * Created on December 7, 2002 2:25 PM
05  */
06  
07 package net.sf.navigator.menu;
08  
09 import javax.servlet.http.HttpServletRequest;
10 import java.util.regex.Pattern;
11  
12 /**
13  * This class used container-managed security to check access
14  * to menus.  The roles are set in menu-config.xml.
15  *
16  * @author <a href="mailto:[email protected]">Matt Raible</a>
17  */
18 public class RolesPermissionsAdapter implements PermissionsAdapter {
19     private Pattern delimiters = Pattern.compile("(?<!\\\\),");
20     private HttpServletRequest request;
21  
22     public RolesPermissionsAdapter(HttpServletRequest request) {
23         this.request = request;
24     }
25  
26     /**
27      * If the menu is allowed, this should return true.
28      *
29      * @return whether or not the menu is allowed.
30      */
31     public boolean isAllowed(MenuComponent menu) {
32         if (menu.getRoles() == null) {
33             return true// no roles define, allow everyone
34         else {
35             // Get the list of roles this menu allows
36             String[] allowedRoles = delimiters.split(menu.getRoles());
37             for (int i=0; i < allowedRoles.length; i++) {
38                 if (request.isUserInRole(allowedRoles[i])) {
39                     return true;
40                 }
41             }
42         }
43         return false;
44     }
45 }


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

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

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

1 /**  
2  *加载菜单静态信息 
3  */ 
4 private void loadMenuRepository()  
5 {  
6      MenuConstructor menuConstructor=new MenuConstructor();  
7              menuConstructor.createMenuRepository();  
8 }


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

01 /** 
02  * 加载系统菜单入菜单库 
03  * @param defaultRepository :缺省库 
04  */ 
05 public void createMenuRepository()   
06 {  
07     ServletContext app=ApplicationContext.getContext();  
08        MenuRepository defaultRepository = (MenuRepository)app.getAttribute(MenuRepository.MENU_REPOSITORY_KEY);  
09     MenuRepository repository = new MenuRepository();  
10        repository.setDisplayers(defaultRepository.getDisplayers());  
11        List list = getMenuComponents();  
12        for (int i=0; i < list.size(); i++)   
13        {  
14            MenuComponent mc = new MenuComponent();  
15            MenuInfo menu=(MenuInfo) list.get(i);  
16            String menuName = menu.getName();  
17            mc.setName(menuName);  
18            String parentName = (String) menu.getParentName();  
19            if (parentName != null)   
20            {  
21                MenuComponent parentMenu = repository.getMenu(parentName);  
22                if (parentMenu == null)   
23                {  
24                    parentMenu = new MenuComponent();  
25                    parentMenu.setName(parentName);  
26                    repository.addMenu(parentMenu);  
27                }  
28                mc.setParent(parentMenu);  
29            }  
30            String title = (String)menu.getTitle();  
31            String location = (String) menu.getLocation();  
32            String target = (String) menu.getTarget();  
33            String description = (String) menu.getDescription();  
34            String action=(String)menu.getAction();  
35            String roles=(String)menu.getRoles();  
36            mc.setTitle(title);                          
37            mc.setLocation(location);  
38            mc.setAction(action);  
39            mc.setTarget(target);              
40            mc.setDescription(description);  
41            mc.setRoles(roles);  
42            repository.addMenu(mc);  
43        }  
44        constant.setMenuRepository(repository);  
45        //因为加载出来的菜单信息被打乱了.  
46        //按照库表菜单顺序保存一级菜单信息,在struts-menu显示时要用  
47        app.setAttribute("OrderMenuList",getOrderMenuList());  
48 }  
49    
50 /** 
51  * 从数据库中读取菜单信息 
52  * @return List :菜单信息 
53  */ 
54    private static List getMenuComponents()   
55    {  
56     PersistenceService service = (PersistenceService)ApplicationContextProvider.getBean("persistenceService");  
57        String hql=QlConstructor.getSqlContent("getMenuInfo");  
58     List menuInfoList = service.list(hql);  
59     return menuInfoList;  
60    }  
61        
62    /** 
63     * 将菜单表中的一级菜单按照显示顺序取出来 
64     * @return List:一级菜单信息 
65     */ 
66    private static List getOrderMenuList()  
67    {  
68        List orderMenuList=new ArrayList();  
69        PersistenceService service = (PersistenceService)ApplicationContextProvider.getBean("persistenceService");  
70        String hql=QlConstructor.getSqlContent("getParentmenuSql");  
71        orderMenuList = service.list(hql);  
72        return orderMenuList;  
73    }
01 /** 
02  * 根据userId获取用户所能看到的菜单信息 
03  * @param   authSet         :用户权限集 
04  * @return  MenuRepository  : 用户所能看到的菜单库 
05  */ 
06 public MenuRepository getUserMenuRepository(long authSet)   
07 {  
08     Constant constant=Constant.getInstance();  
09     //获取系统所有菜单  
10     ServletContext app=ApplicationContext.getContext();  
11     MenuRepository defaultRepository = (MenuRepository)app.getAttribute(MenuRepository.MENU_REPOSITORY_KEY);  
12     MenuRepository menuRepository=constant.getMenuRepository();  
13     MenuRepository userMenuRepository=new MenuRepository();  
14     userMenuRepository.setDisplayers(defaultRepository.getDisplayers());  
15     Set menuSet=menuRepository.getMenuNames();  
16     Iterator itor=menuSet.iterator();  
17     MenuComponent menuComponent =null;  
18     //查找满足用户权限的菜单项  
19     while(itor.hasNext())  
20     {  
21         String menuName=(String)itor.next();  
22         menuComponent = menuRepository.getMenu(menuName);  
23         long roles=Long.parseLong(menuComponent.getRoles());  
24         if((roles&authSet)!=0)  
25         {  
26             userMenuRepository.addMenu(menuComponent);                
27         }  
28     }  
29     //TODO:下面生成用户所能看到的菜单  
30     userMenuRepository=menuRepository;  
31     return userMenuRepository;  
32 }  
33    
34    /** 
35     * 获取用户所能看到的一级菜单列表,在画面展示时struts-menu标签需要用到 
36     * @param userMenuRepository    :用户自己的菜单库 
37     * @return  List: 用户能看到的一级菜单名称数组 
38     */ 
39 public List getParentMenuNameList(MenuRepository userMenuRepository) throws Exception  
40 {          
41        ServletContext app=ApplicationContext.getContext();  
42        List orderMenuList=(List)app.getAttribute("OrderMenuList");  
43        List parentMenuNameList=new ArrayList();  
44        for(int i=0;i<orderMenuList.size();i++)  
45        {  
46            String menuName=((MenuInfo)orderMenuList.get(i)).getName();  
47            if(userMenuRepository.getMenu(menuName)!=null)  parentMenuNameList.add(menuName);  
48        }         
49     return parentMenuNameList;  
50 }


显示菜单:

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

你可能感兴趣的:(角色权限,Struts-Menu)