Java 注解 攔截器

場景描述:現在需要對部分Controller或者Controller里面的服務方法進行權限攔截。如果存在我們自定義的注解,通過自定義注解提取所需的權限值,然后對比session中的權限判斷當前用戶是否具有對該控制器或控制器方法的訪問權限。如果沒有相關權限則終止控制器方法執行直接返回。有兩種方式對這種情況進行處理。

方式一:使用SpringAOP中的環繞Around
方式二:使用Spring?web攔截器
標簽:?Spring

代碼片段(4)[全屏查看所有代碼]

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;
????}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:
http://www.pswp.cn/news/388021.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388021.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388021.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

醫療大數據處理流程_我們需要數據來大規模改善醫療流程

醫療大數據處理流程Note: the fictitious examples and diagrams are for illustrative purposes ONLY. They are mainly simplifications of real phenomena. Please consult with your physician if you have any questions.注意&#xff1a;虛擬示例和圖表僅用于說明目的。 …

What's the difference between markForCheck() and detectChanges()

https://stackoverflow.com/questions/41364386/whats-the-difference-between-markforcheck-and-detectchanges轉載于:https://www.cnblogs.com/chen8840/p/10573295.html

ASP.NET Core中使用GraphQL - 第七章 Mutation

ASP.NET Core中使用GraphQL - 目錄 ASP.NET Core中使用GraphQL - 第一章 Hello WorldASP.NET Core中使用GraphQL - 第二章 中間件ASP.NET Core中使用GraphQL - 第三章 依賴注入ASP.NET Core中使用GraphQL - 第四章 GrahpiQLASP.NET Core中使用GraphQL - 第五章 字段, 參數, 變量…

POM.xml紅叉解決方法

方法/步驟 1用Eclipse創建一個maven工程&#xff0c;網上有很多資料&#xff0c;這里不再啰嗦。 2右鍵maven工程&#xff0c;進行更新 3在彈出的對話框中勾選強制更新&#xff0c;如圖所示 4稍等片刻&#xff0c;pom.xml的紅叉消失了。。。

JS前臺頁面驗證文本框非空

效果圖&#xff1a; 代碼&#xff1a; 源代碼&#xff1a; <script type"text/javascript"> function check(){ var xm document.getElementById("xm").value; if(xm null || xm ){ alert("用戶名不能為空"); return false; } return …

python對象引用計數器_在Python中借助計數器對象對項目進行計數

python對象引用計數器前提 (The Premise) When we deal with data containers, such as tuples and lists, in Python we often need to count particular elements. One common way to do this is to use the count() function — you specify the element you want to count …

套接字設置為(非)阻塞模式

當socket 進行TCP 連接的時候&#xff08;也就是調用connect 時&#xff09;&#xff0c;一旦網絡不通&#xff0c;或者是ip 地址無效&#xff0c;就可能使整個線程阻塞。一般為30 秒&#xff08;我測的是20 秒&#xff09;。如果設置為非阻塞模式&#xff0c;能很好的解決這個…

經典問題之「分支預測」

問題 來源 &#xff1a;stackoverflow 為什么下面代碼排序后累加比不排序快&#xff1f; public static void main(String[] args) {// Generate dataint arraySize 32768;int data[] new int[arraySize];Random rnd new Random(0);for (int c 0; c < arraySize; c)data…

vi

vi filename :打開或新建文件&#xff0c;并將光標置于第一行首 vi n filename &#xff1a;打開文件&#xff0c;并將光標置于第n行首 vi filename &#xff1a;打開文件&#xff0c;并將光標置于最后一行首 vi /pattern filename&#xff1a;打開文件&#xff0c;并將光標置…

數字圖像處理 python_5使用Python處理數字的高級操作

數字圖像處理 pythonNumbers are everywhere in our daily life — there are phone numbers, dates of birth, ages, and other various identifiers (driver’s license and social security numbers, for example).電話號碼在我們的日常生活中無處不在-電話號碼&#xff0c;…

05精益敏捷項目管理——超越Scrum

00.我們不是不知道它會給我們帶來麻煩&#xff0c;只是沒想到麻煩會有這么多。——威爾.羅杰斯 01.知識點&#xff1a; a.Scrum是一個強大、特意設計的輕量級框架&#xff0c;器特性就是將軟件開發中在制品的數量限制在團隊層級&#xff0c;使團隊有能力與業務落班一起有效地開…

帶標題的圖片輪詢展示

為什么80%的碼農都做不了架構師&#xff1f;>>> <div> <table width"671" cellpadding"0" cellspacing"0"> <tr height"5"> <td style"back…

linux java 查找進程中的線程

這里對linux下、sun(oracle) JDK的線程資源占用問題的查找步驟做一個小結&#xff1b;linux環境下&#xff0c;當發現java進程占用CPU資源很高&#xff0c;且又要想更進一步查出哪一個java線程占用了CPU資源時&#xff0c;按照以下步驟進行查找&#xff1a;(一)&#xff1a;通過…

定位匹配 模板匹配 地圖_什么是地圖匹配?

定位匹配 模板匹配 地圖By Marie Douriez, James Murphy, Kerrick Staley瑪麗杜里茲(Marie Douriez)&#xff0c;詹姆斯墨菲(James Murphy)&#xff0c;凱里克史塔利(Kerrick Staley) When you request a ride, Lyft tries to match you with the driver most suited for your…

Sprint計劃列表

轉載于:https://www.cnblogs.com/zhs20160715/p/9953586.html

MySQL學習【第十二篇事務中的鎖與隔離級別】

一.事務中的鎖 1.啥是鎖&#xff1f; 顧名思義&#xff0c;鎖就是鎖定的意思 2.鎖的作用是什么&#xff1f; 在事務ACID的過程中&#xff0c;‘鎖’和‘隔離級別’一起來實現‘I’隔離性的作用 3.鎖的種類 共享鎖&#xff1a;保證在多事務工作期間&#xff0c;數據查詢不會被阻…

Android WebKit

這段時間基于項目需要 在開發中與WebView的接觸比較多&#xff0c;前段時間關于HTML5規范塵埃落定的消息出現在各大IT社區頭版上&#xff0c;更有人說&#xff1a;HTML5將顛覆原生App開發 雖然我不太認同這一點 但是關于HTML5JSCSSNative的跨平臺開發模式還是為很多企業節省了開…

jQuery的事件綁定和解綁

1、綁定事件 語法&#xff1a; bind(type,data,fn) 描述&#xff1a;為每一個匹配元素的特定事件&#xff08;像click&#xff09;綁定一個事件處理器函數。 參數解釋&#xff1a; type (String) : 事件類型 data (Object) : (可選) 作為event.data屬性值傳遞給事件對象的額外數…

軟件測試框架課程考試_那考試準備課程值得嗎?

軟件測試框架課程考試By Levi Petty李維佩蒂(Levi Petty) This project uses a public, synthesized exam scores dataset from Kaggle to analyze average scores in Math, Reading, and Writing subject areas, relative to the student’s parents’ level of education an…

開博第一天

開博第一天 紀念一下 轉載于:https://www.cnblogs.com/yang-9654/p/9959388.html