BOS12——多对多添加方法,多对多页面需要字段问题(不多的话直接提供get方法),修改Realm中授权方法(查询数据库),缓存Java对象的方法,加载左侧菜单(ztree提供pId)

 1、多对多添加方法

@Override
public void add(Role model, String functionIds) {

    // 1.先将角色保存到数据库
    roleDao.save(model);
    // 2.为角色添加权限(一定要坚持映射文件中是否inverse)
    if (StringUtils.isNotBlank(functionIds)){
        String[] functionIdList = functionIds.split(",");
        for (String fid : functionIdList) {
            // 可以自己new一个Function对象,也可以用functionId获取Function对象
            Function function = new Function();
            function.setId(fid);
            model.getFunctions().add(function);
        }
    }

}

2、多对多页面需要字段问题(不多的话直接提供get方法)

在多对多关系时,由于比较复杂,所以我们可以为页面指定的字段加上一个get方法

3、修改Realm中授权方法(查询数据库)

多对多连表查询

 1 //授权方法
 2 protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
 3 
 4     SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
 5 
 6     // 获取用户对象
 7     TUser user = (TUser) SecurityUtils.getSubject().getPrincipal();
 8 //        TUser user2 = (TUser) principals.getPrimaryPrincipal();
 9 //        System.out.println(user1 == user2);
10 
11     List<Function> functionList = null;
12     if (user.getUsername().equals("admin")){
13         DetachedCriteria dc = DetachedCriteria.forClass(Function.class);
14         functionList = functionDao.findAll(dc);
15     }else{
16         functionList = functionDao.findByUserId(user.getId());
17     }
18 
19 
20     for (Function f: functionList) {
21         info.addStringPermission(f.getCode());
22     }
23     // 直接(不查数据库)为用户授权
24 //        info.addStringPermission("staff-list");
25 
26     return info;
27 }
BOSRealm
 1 @Override
 2 public List<Function> findByUserId(String id) {
 3 
 4     // distinct==》去重
 5     String hql = "select distinct f from Function f inner join f.roles r inner join r.users u where u.id=?";
 6 
 7     List<Function> list = (List<Function>) super.getHibernateTemplate().find(hql,id);
 8 
 9     return list;
10 }
FunctionDao

但这样有一个缺陷,每次访问一个页面都会重新进行数据库查询;所以要整一个缓存

4、ehcache是专门缓存插件,可以缓存Java对象,提高系统性能。

第一步:在pom.xml文件中引入ehcache的依赖

第二步:在项目中提供ehcache的配置文件

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
    <diskStore path="java.io.tmpdir"/>
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />
</ehcache>

第三步:在spring配置文件中配置缓存管理器对象,并注入给安全管理器对象

 

 1 <!--配置安全管理器-->
 2 <bean name="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
 3     <property name="realm" ref="bosRealm"/>
 4     <!--注入缓存管理器-->
 5     <property name="cacheManager" ref="cacheManager"/>
 6 </bean>
 7 
 8 <!--注册缓存管理器-->
 9 <bean name="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
10     <!--注入ehcache配置文件-->
11     <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
12 </bean>
View Code

5、加载左侧菜单

原文地址:https://www.cnblogs.com/x54256/p/8612776.html