public enum AuthorityType {
// 登录和权限都验证 默认
Validate,
// 不验证
NoValidate,
// 不验证权限
NoAuthority;
}
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface Authority {
// 默认验证
AuthorityType value() default AuthorityType.Validate;
}
/**
* 权限认证拦截器
*
*/
public class AuthorityAnnotationInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod hm = (HandlerMethod) handler;
Class<?> clazz = hm.getBeanType();
Method m = hm.getMethod();
try {
if (clazz != null && m != null) {
boolean isClzAnnotation = clazz.isAnnotationPresent(Authority.class);
boolean isMethondAnnotation = m.isAnnotationPresent(Authority.class);
Authority authority = null;
// 如果方法和类声明中同时存在这个注解,那么方法中的会覆盖类中的设定。
if (isMethondAnnotation) {
authority = m.getAnnotation(Authority.class);
} else if (isClzAnnotation) {
authority = clazz.getAnnotation(Authority.class);
}
int code = -1;
String msg = "";
if (authority != null) {
if (AuthorityType.NoValidate == authority.value()) {
// 标记为不验证,放行
return true;
} else if (AuthorityType.NoAuthority == authority.value()) {
// 不验证权限,验证是否登录
// TODO:
return true;
} else {
// 验证登录及权限
// TODO:
code = 1;
msg = "验证成功!";
return true;
}
}
// //跳转
// String url = "";
// response.getWriter().write("<script>top.location.href='"
// + url + "'</script>");
// return false;
// 未通过验证,返回提示json
Map<String, Object> responseMap = new HashMap<String, Object>();
responseMap.put("code", code);
responseMap.put("msg", msg);
responseMap.put("params", "");
responseMap.put("rows", "");
String json = new Gson().toJson(responseMap);
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
response.getWriter().write(json);
return false;
}
} catch (Exception e) {
}
}
return false;
}
}
<mvc:interceptors>
<!-- 权限认证拦截器 -->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="cn.mayongfa.interceptor.AuthorityAnnotationInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有