Java 注解 拦截器
方式一:使用SpringAOP中的环绕Around
方式二:使用Spring web拦截器
代码片段(4)[全屏查看所有代码]
1. [代码]定义注解
1
2
3
4
5
6
7
8
9
10
11
12
|
@Retention (RetentionPolicy.RUNTIME)
@Target ({ElementType.METHOD,ElementType.TYPE})
@Documented
//最高优先级
@Order (Ordered.HIGHEST_PRECEDENCE)
public @interface RoleControl {
/**
*
* 角色类型,以便决定是否具有相关权限
*/
String value() default "user" ;
}
|
2. [代码]在Controller中使用
1
2
3
4
5
6
7
8
9
10
11
12
|
@RoleControl ( "ADMIN" )
@Controller
public class LoginController {
@Autowired
private UserService uService;
@Autowired
private GlobalConfigService gcService;
@RoleControl ( "" )
@RequestMapping ( "/login" )
public String login(HttpServletRequest request,HttpServletResponse resp, @ModelAttribute ( "user" ) UserDto uDto) {
return ""
}
|
3. [代码]方式一:使用SpringAOP中的环绕Around
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
@Component
@Aspect
public class RoleControlAspect {
/**类上注解情形 */
// @Pointcut("@within(net.xby1993.springmvc.annotation.RoleControl)")
@Pointcut( "execution(* net.xby1993.springmvc.controller..*.*(..)) && @within(net.xby1993.springmvc.annotation.RoleControl)" )
public void aspect(){
}
/**方法上注解情形 */
@Pointcut( "execution(* net.xby1993.springmvc.controller..*.*(..)) && @annotation(net.xby1993.springmvc.annotation.RoleControl)" )
public void aspect2(){
}
/**aop实际拦截两种情形*/
@Around( "aspect() || aspect2()" )
public Object doBefore(ProceedingJoinPoint point) {
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
HttpSession session=request.getSession();
Object target = point.getTarget();
String method = point.getSignature().getName();
Class<?> classz = target.getClass();
Method m = ((MethodSignature) point.getSignature()).getMethod();
try {
if (classz!= null && m != null ) {
boolean isClzAnnotation= classz.isAnnotationPresent(RoleControl. class );
boolean isMethondAnnotation=m.isAnnotationPresent(RoleControl. class );
RoleControl rc= null ;
//如果方法和类声明中同时存在这个注解,那么方法中的会覆盖类中的设定。
if (isMethondAnnotation){
rc=m.getAnnotation(RoleControl. class );
} else if (isClzAnnotation){
rc=classz.getAnnotation(RoleControl. class );
}
String value=rc.value();
Object obj=session.getAttribute(GeneUtil.SESSION_USERTYPE_KEY);
String curUserType=obj== null ? "" :obj.toString();
//进行角色访问的权限控制,只有当前用户是需要的角色才予以访问。
boolean isEquals=StringUtils.checkEquals(value, curUserType);
if (isEquals){
try {
return point.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (Exception e){
}
return null ;
}
}
|
4. [代码]方式二:使用拦截器,推荐 跳至 [1] [2] [3] [4] [全屏预览]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import net.xby1993.springmvc.annotation.RoleControl;
import net.xby1993.springmvc.util.GeneUtil;
import net.xby1993.springmvc.util.PathUtil;
import net.xby1993.springmvc.util.StringUtils;
public class GlobalInterceptor extends HandlerInterceptorAdapter{
private static Logger log=LoggerFactory.getLogger(LoginInterceptor. class );
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
HttpSession s=request.getSession();
s.setAttribute( "host" , PathUtil.getHost());
s.setAttribute( "siteName" , GeneUtil.SITE_NAME);
//角色权限控制访问
return roleControl(request,response,handler);
}
/**角色权限控制访问*/
private boolean roleControl(HttpServletRequest request,HttpServletResponse response, Object handler){
HttpSession session=request.getSession();
System.out.println(handler.getClass().getName());
if (handler instanceof HandlerMethod){
HandlerMethod hm=(HandlerMethod)handler;
Object target=hm.getBean();
Class<?> clazz=hm.getBeanType();
Method m=hm.getMethod();
try {
if (clazz!= null && m != null ) {
boolean isClzAnnotation= clazz.isAnnotationPresent(RoleControl. class );
boolean isMethondAnnotation=m.isAnnotationPresent(RoleControl. class );
RoleControl rc= null ;
//如果方法和类声明中同时存在这个注解,那么方法中的会覆盖类中的设定。
if (isMethondAnnotation){
rc=m.getAnnotation(RoleControl. class );
} else if (isClzAnnotation){
rc=clazz.getAnnotation(RoleControl. class );
}
String value=rc.value();
Object obj=session.getAttribute(GeneUtil.SESSION_USERTYPE_KEY);
String curUserType=obj== null ? "" :obj.toString();
//进行角色访问的权限控制,只有当前用户是需要的角色才予以访问。
boolean isEquals=StringUtils.checkEquals(value, curUserType);
if (!isEquals){
//401未授权访问
response.setStatus(401);
return false ;
}
}
} catch (Exception e){
}
}
return true ;
}
|
Java 注解 拦截器相关推荐
- Springboot + redis + 注解 + 拦截器来实现接口幂等性校验
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:wangzaiplus www.jianshu.com/p/ ...
- springboot + redis + 注解 + 拦截器 实现接口幂等性校验
点击上方"方志朋",选择"设为星标" 做积极的人,而不是积极废人 来源:https://www.jianshu.com/p/6189275403ed 一.概念 ...
- @slf4j注解_SpringBoot + Redis + 注解 + 拦截器 实现接口幂等性校验
一.概念 幂等性, 通俗的说就是一个接口, 多次发起同一个请求, 必须保证操作只能执行一次 比如: 订单接口, 不能多次创建订单 支付接口, 重复支付同一笔订单只能扣一次钱 支付宝回调接口, 可能会多 ...
- Java EE拦截器
历史 我认为重要的是要看一下Java EE中的Interceptor的发展,因为它是从EJB特定的项目开始的,后来又演变成一个单独的规范,现在可供其他Java EE规范扩展,这一事实很简单. 版本1. ...
- redis 判断存在性_springboot + redis + 注解 + 拦截器 实现接口幂等性校验
提醒:后面有些图片模糊,请点击原文查看清晰图片 一.概念 幂等性, 通俗的说就是一个接口, 多次发起同一个请求, 必须保证操作只能执行一次 比如: 订单接口, 不能多次创建订单 支付接口, 重复支付同 ...
- jmeter校验结果_Springboot + redis + 注解 + 拦截器来实现接口幂等性校验
来自:简书,作者:wangzaiplus 链接:https://www.jianshu.com/p/6189275403ed 一.概念 幂等性, 通俗的说就是一个接口, 多次发起同一个请求, 必须保证 ...
- springboot 自定义注解拦截器
springboot 自定义注解拦截器 最近在工作中,发现自定义注解拦截使用起来特别方便,现在来写出来给大家看看 环境springboot 首先写一个自定义注解 package com.study.c ...
- springboot redis token_Spring Boot + Redis + 注解 + 拦截器来实现接口幂等性校验
优质文章,及时送达 作者 | wangzaiplus 链接 | www.jianshu.com/p/6189275403ed 一.概念 幂等性, 通俗的说就是一个接口, 多次发起同一个请求, 必须保证 ...
- springboot幂等性_Spring Boot + Redis + 注解 + 拦截器来实现接口幂等性校验
一.概念 幂等性, 通俗的说就是一个接口, 多次发起同一个请求, 必须保证操作只能执行一次 比如:订单接口, 不能多次创建订单 支付接口, 重复支付同一笔订单只能扣一次钱 支付宝回调接口, 可能会多次 ...
最新文章
- 计算机网络考试有感 2019 山东大学
- 集群节点间的延迟问题
- QPushButton hover配置
- WCF X.509验证
- android代码生成excel,AndroidExcel
- 帝国CMS7.2仿极客网互联网自媒体门户模板
- WORD如何隐藏选中内容?
- 计算机辅助工艺设计主要内容,计算机辅助工艺设计.ppt
- Java PreparedStatement IN子句替代
- text怎么转换html,将Text转换为Html(下)
- ]flume高并发优化——(1)load_balance
- 计算机操作题如何打分,Excel操作题也能自动评分
- 【Python网络蜘蛛 · 1】:网络蜘蛛的基本介绍
- Echarts 中国地图(包括china.js文件)
- c 添加mysql表单的一行数据类型,MySQL InnoDB表行格式及压缩
- vn.py源码解读(八、回测结果计算代码解析)
- ps用画笔工具设计水墨圆环
- 一个26岁没文凭,想去努力自学编程,有机会成为程序员吗?
- 什么是正定矩阵,什么是负定矩阵?判别方法
- 绪论——信息理论学与量子信息学
热门文章
- 【C++基础】模板参数与模板继承
- Java BigInteger类| modInverse()方法与示例
- 第九章 魔法方法、特性和迭代器
- HDU 1874 畅通工程续 (Dijkstra , Floyd , SPFA, Bellman_Ford 四种算法)
- 1346. 检查整数及其两倍数是否存在 golang
- C语言操作符 进阶 (常见错误及细节)
- [数据结构]求解迷宫最短路径问题
- linux-----强大的find
- mkfs.jffs2参数详解
- 【Leetcode | 42】129. 求根到叶子节点数字之和