springmvc中shiro拦截权限动态写入功能

1.一开始,springmvc中shiro拦截配置内容,为静态写入方式,对这里大家都不陌生如: springmvc中shiro拦截权限动态写入功能
这样所有权限添加时都需要在数据库和配置文件中进行修改和添加,不利于后期的维护。再后来的项目修改中,
我们添加了权限管理的数据库表,实现了权限的动态录入,那么同样这里的拦截也要和数据库对应,所以修改了拦截配置,实现动态写入的方法。
2.实现动态写入拦截配置
       a.配置中动态调用写入方法
springmvc中shiro拦截权限动态写入功能
      b.定义实现类,注意和配置文件中你写的调用的实现类、方法一致
         @Service("authorityServiceImpl")
@Transactional
public class AuthorityServiceImpl extends BaseServiceImpl
              implements AuthorityService {
       private final static String crlf = "\r\n";
       @Autowired
       private AuthorityDao authorityDao;
       @Resource
       private ShiroFilterFactoryBean shiroFilterFactoryBean;

       @Resource(name = "authorityDaoImpl")
       public void setBaseDao(AuthorityDao authorityDao) {
              super.setBaseDao(authorityDao);
       }
     
     
       @Override
       @Cacheable("authority")
       public String loadFilterChainDefinitio ns() {
              StringBuffer sb = new StringBuffer("");
              sb.append(getFixeRole());
              sb.append(getDynaAuth());
              sb.append("/admin
       private String getDynaAuth() {

              StringBuffer sb = new StringBuffer("");
              List authlist = findByOrder();
              for (Authorities auth : authlist) {
                     String url = auth.getShiroUrl();
                     if (!url.startsWith("/")) {
                            url = "/" + url;
                     }
                     sb.append(url).append("=").append("perms["")
                                   .append(auth.getEnglishName()).append(""]").append(crlf);
              }
              return sb.toString();
       }

     
       private String getFixeRole() {
              StringBuffer sb = new StringBuffer("");
              ClassPathResource cpr = new ClassPathResource("auth.properties");
              Properties pro = new Properties();
              try {
                     pro.load(cpr.getInputStream());
              } catch (Exception e) {
                     e.printStackTrace();
              }
              for (Iteratorit = pro.keySet().iterator(); it.hasNext();) {
                     String key = (String) it.next();
                     sb.append(key).append("=").append(pro.getProperty(key).trim())
                                   .append(crlf);
              }
              return sb.toString();
       }

}
这样就完成了一个拦截路径配置的写入
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值