快速构建一个权限项目
Posted My丶Ou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快速构建一个权限项目相关的知识,希望对你有一定的参考价值。
今天我们讲解的是Http请求前后监听工具
首先得创建一个类用来做处理:HttpInterceptor.class:
package cn.oyc.common;
import cn.oyc.util.JsonMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
@Slf4j
public class HttpInterceptor extends HandlerInterceptorAdapter {
private final Logger logger = LoggerFactory.getLogger(HttpInterceptor.class);
private static final String START_TIME = "requestStartTime";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String url = request.getRequestURL().toString();
Map parameterMap = request.getParameterMap();
logger.info("request start. utl:{},params:{}",url, JsonMapper.obj2String(parameterMap));
long start = System.currentTimeMillis();
request.setAttribute(START_TIME,start);
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// String url = request.getRequestURL().toString();
// long start = (Long) request.getAttribute(START_TIME);
// long end = System.currentTimeMillis();
// logger.info("request finished. utl:{},cost:{}",url, end - start);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
String url = request.getRequestURL().toString();
long start = (Long) request.getAttribute(START_TIME);
long end = System.currentTimeMillis();
logger.info("request completed. utl:{},cost:{}",url, end - start);
}
}
做完这些别忘记配置一下spring-servlet.xml,配置一段bean:
<mvc:interceptors> <bean class="cn.oyc.common.HttpInterceptor"/> </mvc:interceptors>
接下来我们这个类就编写完了,大家要记住,这个类可以拦截我们系统的url,可以在url请求之前可以做些事情,然后在请求处理完之后依旧可以做一些事情,这里我们基本的工具就配置好了,接下来就是编写我们
的部门接口的一个实现了,跟着下面来:
首先在param包下编写一个部门类DeptParam:
package cn.oyc.param; import lombok.Getter; import lombok.Setter; import lombok.ToString; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.NotBlank; import javax.validation.constraints.NotNull; @Getter @Setter @ToString public class DeptParam { private Integer id; @NotBlank(message = "部门名称不可以为空") @Length(max = 15,min = 2,message = "部门名称需要在2-15个字之间") private String name; private Integer parentId; @NotNull(message = "展示顺序不可以为空") private Integer seq; @Length(max = 150,message = "备注的长度需要在150个字以内") private String remark; }
在编写一个controller,SysDeptController:
package cn.oyc.controller; import cn.oyc.common.JsonData; import cn.oyc.param.DeptParam; import cn.oyc.service.SysDeptService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; @Controller @RequestMapping("/sys/dept") @Slf4j public class SysDeptController { @Resource private SysDeptService sysDeptService; @RequestMapping("/save.json") public JsonData saveDept(DeptParam param){ sysDeptService.save(param); return JsonData.success(""); } }
在这之前我们还得创建他的Service:
package cn.oyc.service; import cn.oyc.dao.SysDeptMapper; import cn.oyc.entity.SysDept; import cn.oyc.exception.ParamException; import cn.oyc.param.DeptParam; import cn.oyc.util.BeanValidator; import cn.oyc.util.LevelUtil; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Date; @Service public class SysDeptService { @Resource private SysDeptMapper sysDeptMapper; public void save(DeptParam param) { BeanValidator.check(param); if (checkExist(param.getParentId(),param.getName(),param.getId())){ throw new ParamException("同一层级下存在相同名称的部门"); } SysDept dept = SysDept.builder().name(param.getName()).parentId(param.getParentId()) .seq(param.getSeq()).remark(param.getRemark()).build(); dept.setLevel(LevelUtil.calculateLevel(getLevel(param.getParentId()),param.getParentId())); dept.setOperator("system");//TODO: dept.setOperateIp("127.0.0.1"); //TODO dept.setOperateTime(new Date()); sysDeptMapper.insertSelective(dept); } private boolean checkExist(Integer parentId,String deptName,Integer deptId){ // TODD: return true; } private String getLevel(Integer deptId){ SysDept dept = sysDeptMapper.selectByPrimaryKey(deptId); if (dept == null){ return null; } return dept.getLevel(); } }
在创建一个层级状态的类:
package cn.oyc.util; import org.apache.commons.lang3.StringUtils; public class LevelUtil { public final static String SEPARATOR = "."; public final static String ROOT = "0"; //0 //0.1 //0.1.2 //0.1.3 //0.4 public static String calculateLevel(String parentLevel,int parentId){ if (StringUtils.isBlank(parentLevel)){ return ROOT; }else{ return StringUtils.join(parentLevel,SEPARATOR,parentId); } } }
在我们的SysDept实体中添加注解
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ToString
以上是关于快速构建一个权限项目的主要内容,如果未能解决你的问题,请参考以下文章