2014-12-04 3 views
1

컨트롤러에 aspectj 기반 보안을 시도했지만 주석을 사용하려고 할 때 작동하지 않습니다. 그러나 더 깊은 서비스 방법에 주석을 달려고 할 때 - 모든 것이 완벽하게 작동합니다.AspectJ가 Spring @Controller 메서드에서 작동하지 않습니다.

컨트롤러 클래스

@Controller 
public class HomeController { 

    @Autowired 
    private AccountService accountService; 

    @Loggable 
    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String index(Principal principal, HttpSession session) { 
     Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
     if (!(authentication instanceof AnonymousAuthenticationToken)) { 
      String email = authentication.getName(); 
      Account acc = accountService.findByEmail(email); 
      session.setAttribute("user", acc); 
     } 
     return principal != null ? accountService.findByEmail(principal.getName()).getRole().equalsIgnoreCase("ROLE_ADMIN") ? "/home/adminSignedIn" : "home/homeSignedIn" : "home/homeNotSignedIn"; 
    } 

주석

@Target(value = {ElementType.METHOD, ElementType.TYPE}) 
@Retention(value=RUNTIME) 
// @Documented 
//@Documented 
@Component 
public @interface Loggable { 

} 

화면 클래스

@Aspect 
@Component 
public class Logger { 

Logger logger = LoggerFactory.getLogger("test"); 

    @Around("@annotation(com.proj.server.aspect.log.Loggable)") 
    public Object traceMethod(ProceedingJoinPoint pjp) throws Throwable { 
     MethodSignature signature = (MethodSignature) pjp.getSignature(); 
     Method method = signature.getMethod(); 
     String methodName = signature.getDeclaringTypeName() + "#" + method.getName(); 
     logger.info("START : " + methodName); 
     Object o = pjp.proceed(); 
     logger.info("END : "); 
     return o; 
    } 
} 

의 pom.xml

<project 
    xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" 
> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.proj.web</groupId> 
    <artifactId>proj</artifactId> 
    <packaging>war</packaging> 
    <version>version</version> 
    <name>proj</name> 
    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>${project.build.sourceEncoding}</project.reporting.outputEncoding> 
     <java-version>1.7</java-version> 
     <org.aspectj-version>1.7.4</org.aspectj-version> 
     <org.slf4j-version>1.7.5</org.slf4j-version> 
     <ch.qos.logback-version>1.0.13</ch.qos.logback-version> 
     <org.thymeleaf-version>2.1.3.RELEASE</org.thymeleaf-version> 
     <org.thymeleaf.extras.springsecurity3-version>2.1.1.RELEASE</org.thymeleaf.extras.springsecurity3-version> 
    </properties> 
    <repositories> 
     <repository> 
      <id>java.net2</id> 
      <name>Repository hosting the jee6 artifacts</name> 
      <url>http://download.java.net/maven/2</url> 
     </repository> 
     <repository> 
      <id>sonatype-oss-repository</id> 
      <url>https://oss.sonatype.org/content/groups/public/</url> 
      <releases> 
       <enabled>true</enabled> 
      </releases> 
      <snapshots> 
       <enabled>true</enabled> 
      </snapshots> 
     </repository> 
     <repository> 
      <id>repository.spring.milestone</id> 
      <name>Spring Milestone Repository</name> 
      <url>http://repo.spring.io/milestone</url> 
     </repository> 
    </repositories> 
    <dependencyManagement> 
     <dependencies> 
      <dependency> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-framework-bom</artifactId> 
       <version>4.0.3.RELEASE</version> 
       <type>pom</type> 
       <scope>import</scope> 
      </dependency> 

      <dependency> 
       <groupId>org.springframework.security</groupId> 
       <artifactId>spring-security-bom</artifactId> 
       <version>4.0.0.M2</version> 
       <type>pom</type> 
       <scope>import</scope> 
      </dependency> 
     </dependencies> 
    </dependencyManagement> 
    <dependencies> 
     <!-- Spring --> 
     <dependency> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-aop</artifactId> 
     </dependency> 
     <dependency> 
       <groupId>cglib</groupId> 
       <artifactId>cglib</artifactId> 
       <version>3.1</version> 
     </dependency> 
     <dependency> 
       <groupId>aspectj</groupId> 
       <artifactId>aspectjrt</artifactId> 
       <version>1.5.4</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-test</artifactId> 

      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <exclusions> 
       <!-- Exclude Commons Logging in favor of SLF4j --> 
       <exclusion> 
        <groupId>commons-logging</groupId> 
        <artifactId>commons-logging</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
     </dependency> 
     <!-- Security --> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-config</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-web</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-taglibs</artifactId> 
     </dependency> 
     <!-- View --> 
     <dependency> 
      <groupId>org.thymeleaf</groupId> 
      <artifactId>thymeleaf</artifactId> 
      <version>${org.thymeleaf-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.thymeleaf</groupId> 
      <artifactId>thymeleaf-spring4</artifactId> 
      <version>${org.thymeleaf-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.thymeleaf.extras</groupId> 
      <artifactId>thymeleaf-extras-springsecurity3</artifactId> 
      <version>${org.thymeleaf.extras.springsecurity3-version}</version> 
     </dependency> 
     <!-- Persistence --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-jdbc</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-tx</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>4.3.6.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>postgresql</groupId> 
      <artifactId>postgresql</artifactId> 
      <version>9.1-901-1.jdbc4</version> 
     </dependency> 
     <!-- Spring Data --> 
     <dependency> 
      <groupId>org.springframework.data</groupId> 
      <artifactId>spring-data-jpa</artifactId> 
      <version>1.4.1.RELEASE</version> 
     </dependency> 
     <!-- javax.validation (JSR-303) --> 
     <dependency> 
      <groupId>javax.validation</groupId> 
      <artifactId>validation-api</artifactId> 
      <version>1.0.0.GA</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-validator</artifactId> 
      <version>4.3.0.Final</version> 
     </dependency> 
     <!-- AspectJ --> 
     <dependency> 
      <groupId>org.aspectj</groupId> 
      <artifactId>aspectjrt</artifactId> 
      <version>${org.aspectj-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.aspectj</groupId> 
      <artifactId>aspectjweaver</artifactId> 
      <version>1.8.4</version> 
     </dependency> 
     <!-- Logging --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>${org.slf4j-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>jcl-over-slf4j</artifactId> 
      <version>${org.slf4j-version}</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>ch.qos.logback</groupId> 
      <artifactId>logback-classic</artifactId> 
      <version>${ch.qos.logback-version}</version> 
     </dependency> 
     <!-- @Inject --> 
     <dependency> 
      <groupId>javax.inject</groupId> 
      <artifactId>javax.inject</artifactId> 
      <version>1</version> 
     </dependency> 
     <!-- Servlet --> 
     <dependency> 
      <groupId>org.apache.geronimo.specs</groupId> 
      <artifactId>geronimo-servlet_3.0_spec</artifactId> 
      <version>1.0</version> 
      <scope>provided</scope> 
     </dependency> 
     <!-- JSON --> 
     <dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-mapper-asl</artifactId> 
      <version>1.9.9</version> 
     </dependency> 
     <!-- Utilities --> 
     <dependency> 
      <groupId>com.google.guava</groupId> 
      <artifactId>guava</artifactId> 
      <version>14.0.1</version> 
     </dependency> 
     <!-- Test --> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.11</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.assertj</groupId> 
      <artifactId>assertj-core</artifactId> 
      <version>1.7.0</version> 
      <scope>test</scope> 
     </dependency> 

    </dependencies> 


    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.tomcat.maven</groupId> 
       <artifactId>tomcat7-maven-plugin</artifactId> 
       <version>2.2</version> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.3.2</version> 
       <configuration> 
        <source>${java-version}</source> 
        <target>${java-version}</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.1.1</version> 
       <configuration> 
        <warName>test-version</warName> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.17</version> 
       <configuration> 
        <excludes> 
         <exclude>${systest.package}</exclude> 
         <exclude>**/*$*</exclude> 
         <exclude>**/*Test*.java</exclude> 
        </excludes> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-dependency-plugin</artifactId> 
       <executions> 
        <execution> 
         <id>deploy</id> 
         <phase>deploy</phase> 
         <goals> 
          <goal>sources</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-resources-plugin</artifactId> 
       <version>2.5</version> 
       <configuration> 
        <encoding>${project.build.sourceEncoding}</encoding> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

    <profiles> 
     <profile> 
      <id>test-all</id> 
      <build> 
       <plugins> 
        <plugin> 
         <groupId>org.apache.maven.plugins</groupId> 
         <artifactId>maven-surefire-plugin</artifactId> 
         <version>2.17</version> 
         <configuration> 
          <includes> 
           <include>**/WebSuite*</include> 

          </includes> 
          <excludes> 
           <exclude>${systest.package}</exclude> 
           <exclude>**/*$*</exclude> 
           <exclude>**/*Test*.java</exclude> 
          </excludes> 
         </configuration> 
        </plugin> 
       </plugins> 
      </build> 
     </profile> 
    </profiles> 

</project> 
,691,363 (210)

@EnableAspectJAutoProxy (지정된 proxyTargetClass = true)를 대신하여 pointcut 표현의 사용과 같은 @annotation (com.proj.server.aspect.log.Loggable)를 사용 appconfig.class

+0

아마도 Spring AOP 구성이 문제 일 것입니다. 질문을 업데이트하고 Spring 설정 XML 또는 설정 클래스에 주석을 표시하십시오. 저는 Spring 사용자가 아니지만 다른 사용자가 귀하를 도울 수 있습니다. 나는 AspectJ 문법을 잘 알고 있으며, 조언이 약간의 어색함에도 불구하고 너무 많은 반영이 사용 되어도 괜찮아 보인다. 하지만 여기서 문제가되지 않습니다. – kriegaex

+0

안녕하세요 kriegaex,이 문제를 해결하기 위해 설정할 수있는 보안, mvc 컨텍스트에 대한 아이디어가 있습니까? 컨트롤러 클래스에 로거가 오지 않는 것과 비슷한 문제가 있습니다. – Dish

답변

0

여기 다른 컨텍스트에 문제가 있습니다. appconfig.class에 @EnableAspectJAutoProxy (proxyTargetClass = true)가 있습니다. 모두 보안 및 mvc 컨텍스트에서 이와 비슷한 것을 추가 한 후에 모두 작동합니다.

+0

안녕하세요 @midikko, 당신은 보안 및 mvc 컨텍스트에서 추가 한 예제를 제공 할 수 있습니까? AspectJ 로깅과 비슷한 문제에 직면하고있다. – Dish

+0

@Dish 어쩌면 전문가가 아니지만 web mvc config 클래스에서 @EnableAspectJAutoProxy (proxyTargetClass = true) 주석을 추가하기 만하면됩니다. – midikko

0

에서

"@ com.proj .server.aspect.log.Loggable * * (..) "시도해보십시오.

+0

발생 원인 : org.springframework.beans.factory.BeanCreationException : 이름이 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration'인 빈 생성 오류 : 빈 초기화에 실패했습니다. 중첩 예외는 java.lang.IllegalArgumentException : Pointcut이 형식이 올바르지 않습니다. 문자 위치 1에서 'pointcut name'을 예상합니다. @ com.proj.server.aspect.log.Loggable * * (..) – midikko

+0

그것은 @Around입니다 "실행 (@ com.proj.server.aspect.log.Loggable * * (..))") 또는 포인트 컷 식을 형성하는 방법에 대해 약간의 시간을 보냈습니다. 매우 쉽습니다. – ppuskar

+0

아무것도 인쇄되지 않았습니다. 비 컨트롤러 서비스 메소드에 대한 주석은 여전히 ​​작동합니다. – midikko

관련 문제