2013-12-20 5 views
0

Для проекта я пытаюсь реализовать свою аннотацию @Secured (уровень безопасности), которая должна проверять уровень безопасности пользователя и проверять метод в зависимости от того, является ли пользователь имеет «разрешение» или нет. Я попытался построить его, но по какой-то причине код моего аспекта, похоже, не срабатывает. В журнале также нет ошибок. Я не смог найти правильное решение проблемы здесь, в Stackoverflow.Spring AOP по аннотации не запускается

Я мог бы использовать весеннюю безопасность, но из-за характера проекта, настройка его была бы гораздо более сложной, чем просто возможность использовать мою собственную аннотацию. Я использую это в конфигурации spring-mvc.

Соответствующий код:

public enum SecurityRole { 
    ROLE_USER, ROLE_ADMIN 
} 

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface Secured { 
    SecurityRole value(); 
} 




@Controller 
public class HomeController { 

    @Autowired 
    private UserService userService; 

    @RequestMapping("/") 
    @Secured 
    public String listContacts(Map<String, Object> map, HttpSession session) { 
     if (session.isNew()) 
      session.setAttribute("test", 123); 
     session.setAttribute("test", (Integer) session.getAttribute("test") + 1); 
     System.out.println(session.getAttribute("test")); 
     map.put("user", new User()); 
     map.put("userList", userService.listUser()); 
     return "user"; 
    } 
} 



@Aspect 
public class SecurityAspect { 

    @Pointcut(value = "execution(public * *(..))") 
    public void anyPublicMethod() { 
    } 

    @Around("anyPublicMethod() && @annotation(securityRole)") 
    public Object secure(ProceedingJoinPoint pjp, Secured securityRole) 
      throws Throwable { 
     System.out.println("called secured!"); 
     System.out.println(securityRole.value()); 
     return pjp.proceed(); 
    } 
} 

(корневой context.xml)

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" 
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
    <!-- Root Context: defines shared resources visible to all other web components --> 
    <aop:aspectj-autoproxy /> 

    <bean id="securityMonitor" class="com.blafoo.usersystem.aspects.SecurityAspect" /> 
</beans> 

ответ

2

Декларация

<aop:aspectj-autoproxy /> 

относится только к бобам, определенных в том же контексте. В вашем root-context.xml у вас есть только один компонент, securityMonitor. Поэтому ваш бонус @Controller (возможно, объявленный в контексте сервера) не рекомендуется.

Смежные вопросы