2017-09-10 11 views
0

私はカスタムアノテーションを作成しましたが、動作させることができません。エラーはありませんが、出力はありません。AOP with Spring Boot - アスペクトが機能しない

あなたはここに完全なコードを見つけることができます。 https://bitbucket.org/Deviad/springfood/src/f2d87086c47db724eca92e03e008612e30a17e1c/?at=acl_not_working

を----- ----- Acl.java

パッケージcom.davidepugliese.springfood.security。

import java.lang.annotation.ElementType; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.annotation.Target; 

@Retention(RetentionPolicy.RUNTIME) 
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE}) 

public @interface Acl{ 
    String value(); 
} 

----- ----- AclAspect.java

package com.davidepugliese.springfood.security; 
import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.Around; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.aspectj.lang.annotation.Pointcut; 
import org.springframework.stereotype.Component; 

@Component 
@Aspect 
public class AclAspect { 
    @Pointcut(value = "@annotation(com.davidepugliese.springfood.security.Acl)" + "&& args(accLevel)") 
    public void accessControl(Acl accLevel) { 
    } 

    @Around(value = "accessControl(accLevel)", argNames = "joinPoint,accLevel") 
    public void value(ProceedingJoinPoint joinPoint, Acl accLevel) throws Throwable { 
//  Object[] originalArguments = joinPoint.getArgs(); 
// 
//  Object[] newArguments = new Object[1]; 
//  System.out.println(newArguments[0]); 
//  newArguments[0] = ((String)originalArguments[0]).toUpperCase(); 

//  joinPoint.proceed(newArguments); 

      System.out.println("Hello world!"); 
      joinPoint.proceed(); 

    } 
} 

----- ----- UserController.java

package com.davidepugliese.springfood.controllers; 

import com.davidepugliese.springfood.domain.UserDAO; 
import com.davidepugliese.springfood.models.User; 
import com.davidepugliese.springfood.security.Acl; 
import com.davidepugliese.springfood.services.EncryptionUtilities; 
import com.davidepugliese.springfood.adt.IEmail; 
import com.sun.javaws.exceptions.InvalidArgumentException; 
import io.jsonwebtoken.Jwts; 
import io.jsonwebtoken.SignatureAlgorithm; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.dao.DataIntegrityViolationException; 
import org.springframework.http.HttpStatus; 
import org.springframework.http.MediaType; 
import org.springframework.http.ResponseEntity; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.*; 
import java.util.Date; 
import java.util.HashMap; 
import java.util.Map; 

@RestController 
@RequestMapping("/api/user/") 
public class UserController { 
    @Value("${jwt.secret}") 
    private String secretKey; 
    private UserDAO userService; 
    @Autowired 
    public UserController(UserDAO userService) { 
     this.userService = userService; 
    } 




    @RequestMapping(value="/{id}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE) 
    @Acl("asdasdas") 
    public @ResponseBody 
    User getUser(@PathVariable Integer id) { 
     return userService.getUser(id); 
    } 

    @RequestMapping(value="/username/{username:.+}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE) 
    public 
    ResponseEntity getUserByUsername(@PathVariable String username) throws InvalidArgumentException { 

      Object data = userService.getUserByUsername(IEmail.create(username)); 
      Map<String, Object> response = new HashMap<>(); 
      response.put("status", "success"); 
      response.put("data", data); 
      return ResponseEntity.ok(response); 
    } 

    @RequestMapping(value="/add", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE) 
    @ResponseStatus(HttpStatus.CREATED) 
    public 
    ResponseEntity addUser(@RequestBody User data, Model model) { 

     try { 
      User user = new User(); 
      user.setUsername(data.getUsername()); 
      user.setPassword(EncryptionUtilities.encryptPassword(data.getPassword())); 
      this.userService.saveUser(user); 
      Map<String, String> response = new HashMap<>(); 
      response.put("status", "success"); 
      response.put("message", "User created successfully"); 
      return ResponseEntity.ok(response); 
     } catch (DataIntegrityViolationException e) { 
      Map<String, String> response = new HashMap<>(); 
      response.put("status", "fail"); 
      response.put("reason", "Username exists already"); 
      return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(response); 
     } 
    } 
    @RequestMapping(value="/login", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE) 
    @ResponseStatus(HttpStatus.OK) 
    public 
    ResponseEntity login(@RequestBody User login, Model model) { 


      String jwtToken; 

      if (login.getUsername() == null || login.getPassword() == null) { 
       Map<String, String> response = new HashMap<>(); 
       response.put("status", "fail"); 
       response.put("reason", "Insert username and password"); 
       return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(response); 
      } 

      String email = login.getUsername(); 

      String password = login.getPassword(); 

      User user = userService.getUserByUsername(email); 

      if (user == null) { 
       Map<String, String> response = new HashMap<>(); 
       response.put("status", "fail"); 
       response.put("reason", "Username not found"); 
       return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(response); 
      } 

      String pwd = user.getPassword(); 
      if (!EncryptionUtilities.matches(password, pwd)) { 
       Map<String, String> response = new HashMap<>(); 
       response.put("status", "fail"); 
       response.put("reason", "Wrong password"); 
       return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(response); 
      } 
      jwtToken = Jwts.builder().setSubject(email).claim("roles", "user").setIssuedAt(new Date()) 
        .signWith(SignatureAlgorithm.HS256, secretKey).compact(); 
      Map<String, Object> response = new HashMap<>(); 
      response.put("status", "success"); 
      response.put("data", jwtToken); 
      return ResponseEntity.ok(response); 
     } 
} 
+1

あなたの側面は間違っています。周囲のアドバイスを使用しているので、 'void'ではなく' Object'を返す必要があります。次に、 'proceed()'の結果を呼び出し側に返すことは決してありません。基本的に、このアスペクトは、一致するすべてのメソッドが 'null'を返します。これは基本的にあなたのソフトウェアを破壊します。引数のリストに 'joinPoint'を持たないようにしてください。 –

答えて

1

AclAspectを交換してください。これでjavaを試してみて

package com.davidepugliese.springfood.security; 
import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.Around; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.aspectj.lang.annotation.Pointcut; 
import org.springframework.stereotype.Component; 

@Component 
@Aspect 
public class AclAspect { 
    @Pointcut(value = "@annotation(accLevel)") 
    public void accessControl(Acl accLevel) { 
    } 

    @Around(value = "com.davidepugliese.springfood.security.accessControl(accLevel)") 
    public void value(ProceedingJoinPoint joinPoint, Acl accLevel) throws Throwable { 
//  Object[] originalArguments = joinPoint.getArgs(); 
// 
//  Object[] newArguments = new Object[1]; 
//  System.out.println(newArguments[0]); 
//  newArguments[0] = ((String)originalArguments[0]).toUpperCase(); 

//  joinPoint.proceed(newArguments); 

      System.out.println("Hello world!"); 
      joinPoint.proceed(); 

    } 
} 
関連する問題