Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwujing committed May 8, 2020
1 parent 606a9e4 commit f369fb0
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 98 deletions.
6 changes: 5 additions & 1 deletion springboot-aspect/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand Down
79 changes: 0 additions & 79 deletions springboot-aspect/springboot-actuator.iml

This file was deleted.

1 change: 1 addition & 0 deletions springboot-aspect/springboot-aspect.iml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-aop:2.2.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.2.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.9.5" level="project" />
<orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.68" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-devtools:2.2.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.2.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.2.6.RELEASE" level="project" />
Expand Down
68 changes: 51 additions & 17 deletions springboot-aspect/src/main/java/com/pancm/aspect/ParamAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@


import com.pancm.pojo.User;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.pancm.result.ResultBody;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.Base64;


/**
* @Author pancm
Expand All @@ -20,38 +25,67 @@
@Component
public class ParamAspect {

private final Logger log = LoggerFactory.getLogger(this.getClass());

@Pointcut("execution(public * com.pancm.web.*.*(..))")
public void doOperation() {

log.info("参数检验AOP");
}

@Around("doOperation()")
public Object doBefore(ProceedingJoinPoint joinPoint) throws Throwable {

/**
* @Title: before
* @Description: 前置通知处理方法
* 在处理之前调用,比如参数、权限校验
* @param joinPoint
*/
@Before("doOperation()")
public void before(JoinPoint joinPoint) throws Throwable{
Object[] objs = joinPoint.getArgs();
for (Object obj : objs) {
User user = (User) obj;
System.out.println("请求的user:"+user);
user.setAge(17);
return joinPoint.proceed(objs);
user.setName(base64DeStr(user.getName()));
}
return joinPoint.proceed(objs);
}




// @Around("doOperation()")
// public Object doBefore(ProceedingJoinPoint joinPoint) throws Throwable {
// Object[] objs = joinPoint.getArgs();
// for (Object obj : objs) {
// User user = (User) obj;
// System.out.println("请求的user:"+user);
// user.setAge(17);
// return joinPoint.proceed(objs);
// }
// return joinPoint.proceed(objs);
// }



@AfterReturning(returning = "object", pointcut = "doOperation()")
public void doAfterReturning(Object object) {
System.out.println("=="+object);
ResultBody resultBody = (ResultBody) object;
String str =null;
try {
str=base64EnStr(resultBody.getResult());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
resultBody.setResult(str);
}


public String base64EnStr(String str) throws UnsupportedEncodingException {
return Base64.getEncoder().encodeToString(str.getBytes("UTF-8"));
}

// RequestVo responseVo = (RequestVo) object;
// byte[] a = Base64Utils.encode(String.valueOf(responseVo.getScont()).getBytes());
// responseVo.setMsg(new String(a ));
// responseVo.setScont("123");

// log.info("请求返回值【{}】", object.toString());
public static String base64DeStr(String encodeStr) throws UnsupportedEncodingException {
byte[] decodeStr = Base64.getDecoder().decode(encodeStr);
return new String(decodeStr, "UTF-8");
}

}
4 changes: 3 additions & 1 deletion springboot-aspect/src/main/java/com/pancm/pojo/User.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.pancm.pojo;


import com.alibaba.fastjson.JSONObject;

/**
*
* Title: User
Expand Down Expand Up @@ -60,7 +62,7 @@ public void setAge(Integer age) {
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
return JSONObject.toJSONString(this);
}


Expand Down
55 changes: 55 additions & 0 deletions springboot-aspect/src/main/java/com/pancm/result/ResultBody.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.pancm.result;

/**
*
* @Title: ResultBody
* @Description: 返回格式
* @Version:1.0.0
* @author pancm
* @date 2018年3月7日
*/
public class ResultBody {
/**
* 响应代码
*/
private String code;

/**
* 响应消息
*/
private String message;

/**
* 响应结果
*/
private String result;

public ResultBody() {
}



public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getResult() {
return result;
}

public void setResult(String result) {
this.result = result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.pancm.web;

import com.pancm.pojo.User;
import com.pancm.result.ResultBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
*
* @Title: UserRestController
* @Description:
* 用户控制层
* @Version:1.0.0
* @author pancm
* @date 2018年3月19日
*/
@RestController
@RequestMapping(value = "/api")
public class UserRestController {


@GetMapping("/user")
public ResultBody findByUser(User user) {
ResultBody resultBody = new ResultBody();
List<User> userList =new ArrayList<>();
User user2=new User();
user2.setId(1L);
user2.setName("xuwujing");
user2.setAge(18);
userList.add(user2);
resultBody.setCode("0");
resultBody.setResult(userList.toString());
return resultBody;
}




}
Loading

0 comments on commit f369fb0

Please sign in to comment.