什么是 AOP

首先我们先了解一下什么是AOP,AOP(Aspect Orient Programming),直译过来就是面向切面编程。AOP是一种编程思想,是面向对象编程(OOP)的一种补充。面向对象编程将程序抽象成各个层次的对象,而面向切面编程是将程序抽象成各个切面。那么本次实现的鉴权功能就是将鉴权代码作为切面织入到需要使用鉴权功能的代码上.以下功能需要使用SpringAop的通知功能与自定义注解,对SpringAop及自定义注解不熟悉的同学可以先去了解一下

基于SpringAop通知实现鉴权

实现思路

基本实现思路就是

  • 编写自定义注解作为切点
  • 然后在切面类中编写鉴权逻辑代码
  • 最后在需要鉴权的方法前添加自定义注解

这样在调用添加了鉴权注解的方法时就是先执行鉴权切面类的代码来进行鉴权

鉴权切点类代码示例

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Authority {
AuthorityType value() default AuthorityType.Validate; FuncIdType[] funcIdTypes() default {}; ModuleIdType[] moduleIdTypes() default {};
}

鉴权切面类代码示例

@Aspect
@Component
public class AuthorityAspect {
private static Logger logger = LoggerFactory.getLogger(SystemLogAspect.class); // 日志切点
@Pointcut("@annotation(com.hzjd.jdmp.annotation.Authority)")
public void executeService() {
}
// 验证是否具有该功能的权限
private boolean validateFunc(FuncIdType[] funcIdTypes) {
HttpSession session = ActionStackManager.getSession();
SessionStackData sessionStackData = SessionStackData.getSessionStackData(session);
if (sessionStackData.getRl() == null) {
return false;
}
for (int i = 0; i < funcIdTypes.length; i++) {
if (sessionStackData.getRl().haveRight(funcIdTypes[i])) {
return true;
}
}
return false;
} // 验证是否具有该模块的权限
private boolean validateModule(ModuleIdType[] moduleIdTypes) {
HttpSession session = ActionStackManager.getSession();
SessionStackData sessionStackData = SessionStackData.getSessionStackData(session);
if (sessionStackData.getRm() == null) {
return false;
}
for (int i = 0; i < moduleIdTypes.length; i++) {
if (sessionStackData.getRm().haveRight(moduleIdTypes[i])) {
return true;
}
}
return false;
} private Object toError(Boolean responseBody) {
if (responseBody) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("success", false);
map.put("msg", "您没有操作此功能的权限");
String json = JsonUtil.toJsonObject(map).toString();
HttpServletResponse response = ActionStackManager.getResponse();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
try {
response.getWriter().write(json);
} catch (IOException e) {
e.printStackTrace();
}
return null;
} else {
return "errauth";
}
} private Object toLogin() {
HttpServletResponse response = ActionStackManager.getResponse();
String loginUrl = ActionStackManager.getRequest().getContextPath() + "/index";
String str = "<script language='javascript'>alert('会话过期,请重新登录');" + "window.top.location.href='" + loginUrl
+ "';</script>";
response.setContentType("text/html;charset=UTF-8");// 解决中文乱码
PrintWriter writer;
try {
writer = response.getWriter();
writer.write(str);
writer.flush();
} catch (IOException e) {
}
return null;
} @Around(value = "executeService()")
public Object doAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Signature signature = proceedingJoinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method targetMethod = methodSignature.getMethod();
Method realMethod = proceedingJoinPoint.getTarget().getClass()
.getDeclaredMethod(signature.getName(), targetMethod.getParameterTypes());
Authority authority = targetMethod.getAnnotation(Authority.class);
Class<?> clazz = targetMethod.getClass();
logger.debug("realMethod:" + realMethod.getName());
logger.debug("realMethod Class:" + realMethod.getDeclaringClass().getSimpleName());
boolean pass = false;
boolean responseBody = targetMethod.isAnnotationPresent(ResponseBody.class);
if (clazz != null && targetMethod != null) {
if (authority != null) {
if (AuthorityType.NoValidate == authority.value()) {
logger.debug("标记为不验证,放行");
// 标记为不验证,放行
pass = true;
} else if (AuthorityType.NoAuthority == authority.value()) {
logger.debug("不验证权限,验证是否登录");
// 不验证权限,验证是否登录
pass = BaseSearchForm.getLoginUser() != null;
if (!pass) {
return toLogin();
}
} else {
if (BaseSearchForm.getLoginUser() != null) {
logger.debug("正在验证权限");
if (authority.funcIdTypes() != null && authority.funcIdTypes().length > 0) {
pass = validateFunc(authority.funcIdTypes());
}
if (!pass) {
if (authority.moduleIdTypes() != null && authority.moduleIdTypes().length > 0) {
pass = validateModule(authority.moduleIdTypes());
}
}
} else {
return toLogin();
}
}
}
}
if (!pass) {
return toError(responseBody);
} else {
logger.debug("验证权限通过");
}
Object obj = proceedingJoinPoint.proceed();
return obj;
}
}

实现方法示例

@RequestMapping(value = "/updateOwerPwd")
@SystemLogAnnotation
@Authority(value = AuthorityType.NoAuthority)
public String updateOwerPwd(ModelMap model, HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionStackManager.initNewData(model, request, response);
try {
HttpSession session = ActionStackManager.getRequest().getSession();
SysUser sysUser = SessionStackData.getSessionStackData(session).getSysUser();
if (sysUser == null) {
session.invalidate();
return forward("login");
}
sysUser = sysUserService.findById(sysUser.getUserid());
if (sysUser == null) {
session.invalidate();
return forward("login");
}
String oldpassword = getRequestParameter("oldpwd");
String password = getRequestParameter("pwd1");
if (!sysUser.getPassword().equals(MD5Utils.getBASE64MD5(oldpassword)))
return forwardError("旧密码输入不正确,修改密码失败!");
sysUser.setPassword(MD5Utils.getBASE64MD5(password));
if (!sysUserService.update(sysUser))
return forwardError("修改密码失败!");
return forward("includes/main");
} catch (Exception e) {
logError(e);
return forwardError(e);
} finally {
ActionStackManager.removeThreadData();
}
}
05-13 07:30