场景java
如今有个系统,不少接口只须要登陆就能够访问,可是有些接口须要授予并验证权限。若是用注解controller的方式控制接口的权限呢?数据库
一、注解声明代码json
这个注解是要装饰在controller接口上的。cookie
按照通常权限的设计,有用户(user)-角色(role)-权限(permission)三种实体,他们之间都是多对多关系。app
注解声明的时候,能够配置要验证的角色(role)或权限(menu)。因此我这里有两个变量。ide
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @Author: ivan * @Description: * @Date: Created in 19:58 18/5/28 * @Modified By: */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Authentication { long[] role() default {}; long[] menu() default {}; }
二、Authentication权限验证Adviceui
咱们以验证角色为例。spa
第一步,先从controller参数的request cookie里拿到用户登陆信息,获取userId,我这里是card。设计
第二步,查询用户角色数据库,获取该user拥有哪些权限。code
第三部,跟@Authentication里配置的权限进行比较,校验成功返回数据,校验失败返回错误码。
使用localthread记录了权限验证处理时间,用来进行监控。
/** * @Author: ivan * @Description: * @Date: Created in 20:00 18/5/28 * @Modified By: */ @Aspect @Component @Order(-10) public class AuthenticationAspect { private static Logger logger = LoggerFactory.getLogger(AuthenticationAspect.class); @Autowired private AuthDao authDao; ThreadLocal<Long> beginTime = new ThreadLocal<Long>(); @Pointcut("@annotation(authentication)") public void AuthenticationService(Authentication authentication) { } @Around("AuthenticationService(authentication)") public Object doAround(ProceedingJoinPoint joinPoint, Authentication authentication) throws Throwable{ beginTime.set(System.currentTimeMillis()); String card = null; List<Long> roleList = new ArrayList<>(); for (Object arg : joinPoint.getArgs()) { if (arg != null && arg.getClass() == RequestFacade.class) { RequestFacade request = (RequestFacade) arg; card = CookieUtils.getCardFromCookie(request); if (StringUtils.isEmpty(card)) { return JsonResult.buildFailResult(-1, 1000, "权限验证未经过", null); } List<Role> roles = authDao.getRolesByCard(card); for (Role role : roles) { roleList.add(role.getId()); } break; } } logger.info("[authentication] user={}, roles={}", card, roleList); long[] aims = authentication.role(); boolean isPass = false; for (long aim : aims) { if (roleList.contains(aim)) { isPass = true; } } if (isPass) { logger.info("[authentication] authentication pass, cost time: {}", System.currentTimeMillis() - beginTime.get()); return joinPoint.proceed(); } else { beginTime.set(System.currentTimeMillis()); logger.info("[authentication] authentication reject, cost time: {}", System.currentTimeMillis() - beginTime.get()); return JsonResult.buildFailResult(-1, 1000, "权限验证未经过", null); } } }
三、使用方法
一、Authentication直接装饰在controller接口上,参数是role={2},即用户拥有2这个角色的时候拥有访问这个接口的权限。
二、controller第一个参数要是HttpServletRequest request,否则上面从request里面拿用户信息会失败。(这个地方确实不方便)
/** * 审核接口 * * @author ivan * @date 2018/08/02 */ @Controller @RequestMapping("/audit") public class AuditController { private static final Logger LOGGER = LoggerFactory.getLogger(AuditController.class); @Resource private AuditService auditService;
@ResponseBody @PostMapping(value = "/review") @Authentication(role = {2}) public JsonResult review(HttpServletRequest request, @RequestBody AduitDTO aduitDTO) { LOGGER.info("[aduitDTO] video service aduitDTO={}" + aduitDTO); UserInfo userInfo = CookieUtils.getLoginInfoFromCookie(request); aduitDTO.setCard(userInfo.getCard()); int status = 0; aduitDTO.setAuditor(userInfo.getCard()); JsonResult jsonResult = null; if (ListEnum.ZERO.toString().equals(aduitDTO.getType())) { if (null != aduitDTO.getStatus() && ListEnum.ONE.toString().equals(aduitDTO.getStatus())) { status = auditService.review(aduitDTO); jsonResult = JsonResult.buildSuccessResult("审核经过"); } if (null != aduitDTO.getStatus() && ListEnum.TWO.toString().equals(aduitDTO.getStatus())) { if(StringUtils.isEmpty(aduitDTO.getReason())){ return JsonResult.buildFailResult(1, 102, "请添加不经过缘由!", null); } aduitDTO.setAuditTime(DateUtils.parseDateToStr(new Date() ,"yyyy-MM-dd HH:mm:ss")); status = auditService.updateAduit(aduitDTO); jsonResult = JsonResult.buildSuccessResult("审核不经过"); } } return jsonResult; } }