1 springmvc 权限控制中写的一个小小的工具类1
程序员文章站
2022-04-25 13:42:45
...
package com.er07.book.springmvc.util; import java.io.File; import java.lang.reflect.Method; import java.net.URL; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.er07.book.springmvc.service.RightService; /** * 增加权限 * @version 1.0 @author 高元东 * I'm glad to share with you my learning techniques! * @mailto 466862016@qq.com * 2013-3-11 */ public class RegRightUtils { private static final String rootUrl = "/spring"; private static RightService rightService; /*** * * @mailto 466862016@qq.com * @param args * @throws Exception */ public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); rightService = context.getBean(RightService.class); ClassLoader classLoader = RegRightUtils.class.getClassLoader(); URL url = classLoader.getResource("com/er07/book/springmvc/controller"); String rootPath = url.getPath().toString(); File rootFile = new File(rootPath); File [] files = rootFile.listFiles(); for(File f: files) { System.err.println(f.getName()); String className = f.getName().substring(0, f.getName().indexOf(".class")); Class clazz = Class.forName("com.er07.book.springmvc.controller."+className); parseClass(clazz); //解析 出url } } /** * * 规定 第一个 路径目录为调用的 方法 * 解析 出此类上的requestMapping 注解的value 值 * @mailto 466862016@qq.com * @param clazz */ public static void parseClass(Class clazz) { if(clazz.isAnnotationPresent(Controller.class)) { //是否为一个controller ? String classUrl =""; if(clazz.isAnnotationPresent(RequestMapping.class)) { RequestMapping requestMapping_clazz = (RequestMapping) clazz.getAnnotation(RequestMapping.class); classUrl = requestMapping_clazz.value()[0]; if(classUrl.equals("/")) { //如果是"/" 的话 制空 classUrl =""; } } Method [] ms = clazz.getDeclaredMethods(); for(Method m :ms) { if(m.isAnnotationPresent(RequestMapping.class)) { RequestMapping requestMapping_method = m.getAnnotation(RequestMapping.class); String methodUrl = requestMapping_method.value()[0]; int index = methodUrl.indexOf("/"); index = methodUrl.indexOf("/",index+1); if(index!=-1) { //如果存在 则进行截取前面的url methodUrl = methodUrl.substring(0, index); } String resultUrl = rootUrl+classUrl+methodUrl; rightService.appendRightByUrl(resultUrl); } } } } }
package com.er07.book.springmvc.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.er07.book.springmvc.dao.RightDao; import com.er07.book.springmvc.model.Right; import com.er07.book.springmvc.service.RightService; import com.er07.book.springmvc.util.StringUtil; import com.er07.book.springmvc.util.ValidateUtil; /** * 权限管理 * @version 1.0 @author 高元东 * I'm glad to share with you my learning techniques! * @mailto 466862016@qq.com * 2013-3-11 */ @Service public class RightServiceImpl implements RightService { @Resource private RightDao rightDao; /** * 保存或者更新权限 根据要求生成权限码 * @mailto 466862016@qq.com * @param right */ public void saveOrUpdateRight(Right right) { long rightCode =1l; int rightPos =0; //是否插入 if(right.getId()==null) { String hql ="select max(r.rightPos),max(r.rightCode) from Right r where r.rightPos = (select max(rr.rightPos) from Right rr)"; Object[] obj = (Object[]) this.rightDao.uniqueResult(hql); Integer topPos = (Integer) obj[0]; Long topCode = (Long) obj[1]; if(topPos==null) { rightCode = 1; rightPos = 0; } else { //达到没有最大值吗? if(topCode>=1l<<60) {//达到最大值 rightPos = topPos+1; rightCode =1l; } else { //没有达到最大值 rightCode = topCode<<1; rightPos = topPos; } } //设置 权限位和权限码 保存在数据库中 right.setRightCode(rightCode); right.setRightPos(rightPos); } this.rightDao.saveOrUpdateEntity(right); } /** * 通过 url 增加或修改权限 */ public void appendRightByUrl(String url) { //查询 此url 在数据库中是否存在 此权限 String hql = "from Right where rightUrl =?"; Right r = (Right) this.rightDao.uniqueResult(hql, url); if(r==null) { //不存在 , 插入数据库中 Right newRight = new Right(); newRight.setRightUrl(url); this.saveOrUpdateRight(newRight); } } /** * 查找在指定范围内的 权限集合 */ public List<Right> findRangeRight(Integer[] ownRightIds) { if(!ValidateUtil.isValid(ownRightIds)) { return null; } else { String hql = "from Right where id in("+StringUtil.arr2Str(ownRightIds)+")"; List<Right> rights = this.rightDao.findEntityByHQL(hql); return rights; } } /*** * 查找所有权限 */ public List<Right> findAllRights() { return this.rightDao.findEntityByHQL("from Right"); } /** * 查找最大的权限位 */ public int findMaxRightPos() { String hql ="select max(rightPos) from Right"; Integer max = (Integer) this.rightDao.uniqueResult(hql); return max==null?0:max; } }
下一篇: JAVA 入坑教程 | 章节三 变量类型