org.mybatis.generator.api.plus.MybatisUtilAuthUser Maven / Gradle / Ivy
package org.mybatis.generator.api.plus;
import java.io.*;
/**
* @author wangfupeng
*/
public class MybatisUtilAuthUser {
public static void writeAuthUserAop(String basePackage, String targetProject) {
File file = null;
OutputStream fileOutputStream = null;
BufferedWriter bufferedWriter = null;
try {
String packageNameFolder = basePackage.replaceAll("\\.", "/");
targetProject = targetProject.replaceAll("\\./", "");
File folder = new File(targetProject + "/" + packageNameFolder + "/aspect/");
folder.mkdirs();
file = new File(targetProject + "/" + packageNameFolder + "/aspect/AuthUser.java");
if (file.exists()) {
return;
}
//如果文件不存在,则创建文件,如果已存在,则覆盖
file.createNewFile();
fileOutputStream = new FileOutputStream(file);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8"));
bufferedWriter.write("package " + basePackage + ".aspect;\n");
bufferedWriter.write("\n" +
"import " + basePackage + ".config.exception.CurrentServerErrorCode;\n" +
"import " + basePackage + ".config.exception.CurrentServerException;\n" +
"import com.gitee.fufu669.service.CacheService;\n" +
"import org.aspectj.lang.JoinPoint;\n" +
"import org.aspectj.lang.annotation.Aspect;\n" +
"import org.aspectj.lang.annotation.Before;\n" +
"import org.slf4j.Logger;\n" +
"import org.slf4j.LoggerFactory;\n" +
"import org.springframework.beans.factory.annotation.Autowired;\n" +
"import org.springframework.core.annotation.Order;\n" +
"import org.springframework.stereotype.Component;\n" +
"\n" +
"import javax.servlet.http.HttpServletRequest;\n" +
"import java.io.UnsupportedEncodingException;\n" +
"import java.net.URLDecoder;\n" +
"/*验证身份的切面*/\n" +
"@Aspect\n" +
"@Component\n" +
"@Order(-69)\n" +
"@SuppressWarnings({\"rawtypes\"})\n" +
"public class AuthUserAop {\n" +
"\n" +
" @Autowired\n" +
" private HttpServletRequest request;\n" +
"\n" +
" public static final Logger logger = LoggerFactory.getLogger(AuthUserAop.class);\n" +
"\n" +
" @Autowired\n" +
" private CacheService cacheService;\n" +
"\n" +
" @Before(\"@annotation(" + basePackage + ".aspect.AuthUser)\")\n" +
" public void beforeMethod(JoinPoint joinPoint) throws Exception {\n" +
" //声明token\n" +
" String token = \"\";\n" +
" //拿到token\n" +
" try {\n" +
" String originalToken = request.getHeader(\"token\");\n" +
" //如果token是空\n" +
" if (originalToken == null) {\n" +
" //抛出异常\n" +
" throw new CurrentServerException(CurrentServerErrorCode.AUTHUSERAOP_TOKEN_NOT_PASSED);\n" +
" }\n" +
" //解码\n" +
" token = URLDecoder.decode(request.getHeader(\"token\"), \"UTF-8\");\n" +
" } catch (UnsupportedEncodingException e) {\n" +
" throw new CurrentServerException(CurrentServerErrorCode.AUTHUSERAOP_TOKEN_DECODE_FAILED);\n" +
" }\n" +
" //do something to token\n" +
" logger.info(\"AuthUser:通过验证:methodName:\" + methodName + \":token:\" + token);\n" +
" }\n" +
"}\n");
bufferedWriter.write("\n");
System.out.println("write to file success : " + file.getPath());
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtilCommon.closeBufferWriterAndFileOutputStream(fileOutputStream, bufferedWriter);
}
}
public static void writeAuthUser(String basePackage, String targetProject) {
File file = null;
OutputStream fileOutputStream = null;
BufferedWriter bufferedWriter = null;
try {
String packageNameFolder = basePackage.replaceAll("\\.", "/");
targetProject = targetProject.replaceAll("\\./", "");
File folder = new File(targetProject + "/" + packageNameFolder + "/aspect/");
folder.mkdirs();
file = new File(targetProject + "/" + packageNameFolder + "/aspect/AuthUser.java");
if (file.exists()) {
return;
}
//如果文件不存在,则创建文件,如果已存在,则覆盖
file.createNewFile();
fileOutputStream = new FileOutputStream(file);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8"));
bufferedWriter.write("package " + basePackage + ".aspect;\n");
bufferedWriter.write("\n" +
"" +
"\n" +
"import java.lang.annotation.ElementType;\n" +
"import java.lang.annotation.Retention;\n" +
"import java.lang.annotation.RetentionPolicy;\n" +
"import java.lang.annotation.Target;\n" +
"\n" +
"/**\n" +
" * 检查用户权限\n" +
" */\n" +
"@Target(ElementType.METHOD)\n" +
"@Retention(RetentionPolicy.RUNTIME)\n" +
"public @interface AuthUser {\n" +
"\n" +
"}\n\n");
bufferedWriter.write("\n");
System.out.println("write to file success : " + file.getPath());
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtilCommon.closeBufferWriterAndFileOutputStream(fileOutputStream, bufferedWriter);
}
}
}