1+ package com .glaum .login .configuration ;
2+
3+ import java .io .IOException ;
4+ import java .lang .reflect .Field ;
5+ import java .lang .reflect .Method ;
6+ import java .util .Arrays ;
7+ import java .util .List ;
8+
9+ import javax .servlet .http .HttpServletRequest ;
10+ import javax .servlet .http .HttpServletResponse ;
11+ import javax .servlet .http .HttpSession ;
12+
13+ import org .aspectj .lang .JoinPoint ;
14+ import org .aspectj .lang .annotation .Aspect ;
15+ import org .aspectj .lang .annotation .Before ;
16+ import org .aspectj .lang .reflect .MethodSignature ;
17+ import org .springframework .beans .factory .annotation .Autowired ;
18+ import org .springframework .stereotype .Component ;
19+
20+ import com .glaum .login .entity .permission ;
21+ import com .glaum .login .service .AuthorizationImpl ;
22+ import com .google .common .collect .Lists ;
23+
24+ import groovy .util .logging .Slf4j ;
25+
26+ @ Slf4j
27+ @ Aspect
28+ @ Component
29+ public class AuthAspect {
30+ @ Autowired
31+ AuthorizationImpl authBean ;
32+
33+
34+
35+ @ Before ("@annotation(Authorized) && args(httpSession,request,res) " )
36+ public static void before (JoinPoint jt ,HttpSession httpSession ,HttpServletRequest request ,HttpServletResponse res ){
37+
38+ boolean authper =false ;
39+ String roleid ="" ;
40+ List <permission > lstobj =Lists .newArrayList ();
41+ int bit =-1 ;
42+
43+ MethodSignature signature = (MethodSignature ) jt .getSignature ();
44+ Method method = signature .getMethod ();
45+
46+ Authorized objauth =method .getAnnotation (Authorized .class );
47+ String [] arrofpermission = objauth .keys ();
48+ System .out .println ("printing aspect val" +((Arrays .toString (arrofpermission ))));
49+
50+
51+
52+
53+ for (Field f : httpSession .getClass ().getDeclaredFields ()) {
54+ f .setAccessible (true );
55+ Object o ;
56+ try {
57+ HttpSession s = (HttpSession ) f .get (httpSession );
58+ lstobj = (List <com .glaum .login .entity .permission >) s .getAttribute ("permissionval" );
59+ roleid =s .getAttribute ("roleid" ).toString ();
60+ } catch (Exception e ) {
61+ o = e ;
62+ }
63+ }
64+ // System.out.println("role"+roleid);
65+ for (String s :arrofpermission )
66+ {
67+ for (permission per : lstobj ) {
68+ if (per .getname ().equalsIgnoreCase (s )) {
69+ bit =per .getbit ();
70+ if ((Integer .parseInt (roleid ) & bit ) == bit )
71+ {
72+ authper =true ;
73+ }
74+ }
75+ }
76+ }
77+ // System.out.println((Integer.parseInt(roleid) & bit) == bit);
78+ // authper=((Integer.parseInt(roleid) & bit) == bit);
79+ if (!authper )
80+ try {
81+ res .sendRedirect (request .getContextPath () + "/accessdenied" );
82+ } catch (IOException e ) {
83+ // TODO Auto-generated catch block
84+ e .printStackTrace ();
85+ }
86+
87+ }
88+ //
89+
90+
91+
92+
93+
94+ }
0 commit comments