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

来源:互联网 发布:西交大网络导航 编辑:程序博客网 时间:2024/06/08 21:55
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 loadFilterChainDefinitions() {
        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
原创粉丝点击