Bonjour,
Je suis en train de tester la sécurité et gestion des permissions avec shiro.
Je me suis basé sur l’excellent article de BalusC : apache shiro is it ready for java ee 6
La partie qui m’intéresse et que je n'arrive pas mettre en place est "Declarative restriction in bean methods".
J'ai donc :
L'intercepteur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 @Inherited @InterceptorBinding @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) public @interface Secured { // }
Mon bean :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 @Interceptor @Secured public class ShiroSecuredInterceptor implements Serializable { private static final long serialVersionUID = 1L; @AroundInvoke public Object interceptShiroSecurity(InvocationContext context) throws Exception { Subject subject = SecurityUtils.getSubject(); Class<?> c = context.getTarget().getClass(); Method m = context.getMethod(); if (!subject.isAuthenticated() && hasAnnotation(c, m, RequiresAuthentication.class)) { throw new UnauthenticatedException("Authentication required"); } if (subject.getPrincipal() != null && hasAnnotation(c, m, RequiresGuest.class)) { throw new UnauthenticatedException("Guest required"); } if (subject.getPrincipal() == null && hasAnnotation(c, m, RequiresUser.class)) { throw new UnauthenticatedException("User required"); } RequiresRoles roles = getAnnotation(c, m, RequiresRoles.class); if (roles != null) { subject.checkRoles(Arrays.asList(roles.value())); } RequiresPermissions permissions = getAnnotation(c, m, RequiresPermissions.class); if (permissions != null) { subject.checkPermissions(permissions.value()); } return context.proceed(); } private static boolean hasAnnotation(Class<?> c, Method m, Class<? extends Annotation> a) { return m.isAnnotationPresent(a) || c.isAnnotationPresent(a) || c.getSuperclass().isAnnotationPresent(a); } private static <A extends Annotation> A getAnnotation(Class<?> c, Method m, Class<A> a) { return m.isAnnotationPresent(a) ? m.getAnnotation(a) : c.isAnnotationPresent(a) ? c.getAnnotation(a) : c.getSuperclass().getAnnotation(a); } }
Mon bean.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 @ManagedBean(name = "myctrl") @RequestScoped @Secured public class MyController { private static ArrayList<Value> myLst = new ArrayList<>(); static { for (int i = 0; i < 2; i++) { myLst.add(new Value(i)); } } @RequiresPermissions("test:list") public List<Value> getMyLst() { Random rand = new Random(); int num = rand.nextInt(1000); myLst.add(new Value(num)); return myLst; } @RequiresPermissions("test:setlist") public void setMyLst(ArrayList<Value> lst) { myLst = lst; } @RequiresRoles("admin") public String launchPush() { PushContext pushContext = PushContextFactory.getDefault() .getPushContext(); pushContext.push("/majStr", "update"); return null; } @RequiresPermissions("adm:action") public String actionAdm() { return "mon action d'administtration"; } }
J'ai essayé et le filtrage ne marche pas !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd" > <interceptors> <class>com.test.interceptor.ShiroSecuredInterceptor</class> </interceptors> </beans>
J'ai du raté quelque chose, mais je ne vois pas quoi ?
Quelqu'un aurait il une idée de ce qui ne va pas ?
Merci d'avance !
Partager