diff --git a/src/main/java/org/layuiframework/generator/config/LayuiConfigBuilder.java b/src/main/java/org/layuiframework/generator/config/LayuiConfigBuilder.java index 90ff98d93b77c408a531b9ad4f107c11e978e8a6..eea3350f41a55cae084ec80a1f458cfc0f6920e6 100644 --- a/src/main/java/org/layuiframework/generator/config/LayuiConfigBuilder.java +++ b/src/main/java/org/layuiframework/generator/config/LayuiConfigBuilder.java @@ -42,6 +42,7 @@ public class LayuiConfigBuilder extends ConfigBuilder { private String dao_path = "dao"; private String task_path = "task"; private String es_path = "elasticsearch"; + private String stereotype_path = "stereotype"; public LayuiConfigBuilder(PackageConfig packageConfig, DataSourceConfig dataSourceConfig, StrategyConfig strategyConfig, TemplateConfig template, GlobalConfig globalConfig) { super(packageConfig, dataSourceConfig, strategyConfig, (TemplateConfig)Optional.ofNullable(template).orElseGet(LayuiTemplateConfig::new), globalConfig); @@ -77,6 +78,7 @@ public class LayuiConfigBuilder extends ConfigBuilder { this.packageInfo.put("Dao", this.joinPackage(config.getParent(), dao_path)); this.packageInfo.put("Task", this.joinPackage(config.getParent(), task_path)); this.packageInfo.put("Es", this.joinPackage(config.getParent(), es_path)); + this.packageInfo.put("Stereotype", this.joinPackage(config.getParent(), stereotype_path)); Map configPathInfo = config.getPathInfo(); if (null != configPathInfo) { @@ -99,6 +101,7 @@ public class LayuiConfigBuilder extends ConfigBuilder { this.setPathInfo(this.getPathInfo(), LayuiTemplateConfig.class.cast(template).getSpring_elasticsearch_dao(), outputDir, "dao_path", "Dao"); this.setPathInfo(this.getPathInfo(), LayuiTemplateConfig.class.cast(template).getSpring_elasticsearch_fulltask(), outputDir, "task_path", "Task"); this.setPathInfo(this.getPathInfo(), LayuiTemplateConfig.class.cast(template).getSpring_elasticsearch_handler(), outputDir, "es_path", "Es"); + this.setPathInfo(this.getPathInfo(), "Stereotype", outputDir, "stereotype_path", "Stereotype"); } } diff --git a/src/main/java/org/layuiframework/generator/engine/FreemarkerTemplateEngine.java b/src/main/java/org/layuiframework/generator/engine/FreemarkerTemplateEngine.java index 5e852d7187ec9e7934cff5bb81ef9c0f79fabd19..022a9ce1110d93a1593d7800dabf2cd1f67fe58b 100644 --- a/src/main/java/org/layuiframework/generator/engine/FreemarkerTemplateEngine.java +++ b/src/main/java/org/layuiframework/generator/engine/FreemarkerTemplateEngine.java @@ -425,6 +425,14 @@ public class FreemarkerTemplateEngine extends AbstractTemplateEngine { } } + public boolean isCreate(FileType fileType, String filePath) { + return super.isCreate(fileType, filePath); + } + + public void writerFile(Map objectMap, String templatePath, String outputFile) throws Exception { + super.writerFile(objectMap, templatePath, outputFile); + } + public void open() { String outDir = this.getConfigBuilder().getGlobalConfig().getOutputDir(); if (this.getConfigBuilder().getGlobalConfig().isOpen() && StringUtils.isNotBlank(outDir)) { @@ -455,6 +463,11 @@ public class FreemarkerTemplateEngine extends AbstractTemplateEngine { ConfigBuilder config = this.getConfigBuilder(); objectMap.put("package", config.getPackageInfo()); Map pathInfo = this.getConfigBuilder().getPathInfo(); + + String voPath = (String)pathInfo.get("stereotype_path"); + // 输出stereotype文件 + StereotypePath.generator(voPath,objectMap, this); + // 输出验证码控制器 String controllerPath = String.format((String)pathInfo.get("controller_path") + File.separator + "IndexController" + this.suffixJavaOrKt(), ""); try { diff --git a/src/main/java/org/layuiframework/generator/engine/LayuiTableInfoLoader.java b/src/main/java/org/layuiframework/generator/engine/LayuiTableInfoLoader.java index 912d984319f276764d1595bce0e236c9e2de1c94..0114a5f665b1ba514c3ba98b7ef1fd1e1f802135 100644 --- a/src/main/java/org/layuiframework/generator/engine/LayuiTableInfoLoader.java +++ b/src/main/java/org/layuiframework/generator/engine/LayuiTableInfoLoader.java @@ -6,7 +6,7 @@ import com.fasterxml.jackson.databind.type.TypeFactory; import freemarker.template.Configuration; import freemarker.template.Template; import org.layuiframework.generator.config.po.LayuiTableInfo; -import org.layuiframework.stereotype.util.FileUtil; +import org.layuiframework.generator.util.FileUtil; import java.io.File; import java.io.FileOutputStream; diff --git a/src/main/java/org/layuiframework/generator/engine/StereotypePath.java b/src/main/java/org/layuiframework/generator/engine/StereotypePath.java new file mode 100644 index 0000000000000000000000000000000000000000..d6a4c0f0386b09e0af8e085058bf0ea1a553115a --- /dev/null +++ b/src/main/java/org/layuiframework/generator/engine/StereotypePath.java @@ -0,0 +1,71 @@ +package org.layuiframework.generator.engine; + +import com.baomidou.mybatisplus.generator.config.rules.FileType; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.io.File; +import java.util.HashMap; + +@Getter +@AllArgsConstructor +public enum StereotypePath { + + ADVICE_EXCEPTION("ControllerExceptionAdvice", "/templates/stereotype/advice/exception.java", "advice" + File.separator), + ANNOTATION_ADVICE("NotControllerResponseAdvice", "/templates/stereotype/annotation/advice.java", "annotation" + File.separator), + ANNOTATION_EXCEL("ImportFromFile", "/templates/stereotype/annotation/excel.java", "annotation" + File.separator), + AOP_EXCEL("ImportFileAopByEasyExcel", "/templates/stereotype/aop/excel.java", "aop" + File.separator), + CONFIG_SPRINGMVC("SpringMvcConfig", "/templates/stereotype/config/springmvc.java", "config" + File.separator), + CONFIG_ELASTICSEARCH("ElasticsearchConfiguration", "/templates/stereotype/elasticsearch/config.java", "elasticsearch" + File.separator), + HANDLER_ELASTICSEARCH("ElasticsearchHandler", "/templates/stereotype/elasticsearch/handler.java", "elasticsearch" + File.separator), + HANDLERS_ELASTICSEARCH("ElasticsearchHandlers", "/templates/stereotype/elasticsearch/handlers.java", "elasticsearch" + File.separator), + RECORD_ELASTICSEARCH("ElasticsearchRecord", "/templates/stereotype/elasticsearch/record.java", "elasticsearch" + File.separator), + VO_ELASTICSEARCH("ElasticsearchVo", "/templates/stereotype/elasticsearch/vo.java", "elasticsearch" + File.separator), + EXCEPTION_API("APIException", "/templates/stereotype/exception/api.java", "exception" + File.separator), + EXCEPTION_UTIL("ThrowUtils", "/templates/stereotype/exception/util.java", "exception" + File.separator), + HANDLER_METAOBJECT("MyMetaObjectHandler", "/templates/stereotype/handler/metaobject.java", "handler" + File.separator), + SECURITY_AUTHENTICATION_SUCCESS("AuthenticationSuccess", "/templates/stereotype/security/authentication/success.java", "security" + File.separator + "authentication" + File.separator), + SECURITY_AUTHENTICATION_FAILURE("AuthenticationFailure", "/templates/stereotype/security/authentication/failure.java", "security" + File.separator + "authentication" + File.separator), + SECURITY_BEAN_KEYPAIR("AbstractKeyPair", "/templates/stereotype/security/bean/keypair.java", "security" + File.separator + "bean" + File.separator), + SECURITY_BEAN_LOGINUSER("LoginUser", "/templates/stereotype/security/bean/loginuser.java", "security" + File.separator + "bean" + File.separator), + SECURITY_BEAN_USER("AbstractUser", "/templates/stereotype/security/bean/user.java", "security" + File.separator + "bean" + File.separator), + SECURITY_FILTER_JWT("JwtAuthorizationFilter", "/templates/stereotype/security/filter/jwt.java", "security" + File.separator + "filter" + File.separator), + SECURITY_FILTER_VERIFY("VerifyCodeFilter", "/templates/stereotype/security/filter/verify.java", "security" + File.separator + "filter" + File.separator), + SECURITY_HANDLER_ACCESS("RestfulAccessDeniedHandler", "/templates/stereotype/security/handler/access.java", "security" + File.separator + "handler" + File.separator), + SECURITY_HANDLER_AJAX("AjaxAuthenticationEntryPoint", "/templates/stereotype/security/handler/ajax.java", "security" + File.separator + "handler" + File.separator), + SECURITY_HANDLER_LOGOUT("LogoutSuccess", "/templates/stereotype/security/handler/logout.java", "security" + File.separator + "handler" + File.separator), + SECURITY_IMPL_CACHE("CacheServiceImpl", "/templates/stereotype/security/impl/cache.java", "security" + File.separator + "impl" + File.separator), + SECURITY_IMPL_LOGIN("LoginAttemptService", "/templates/stereotype/security/impl/login.java", "security" + File.separator + "impl" + File.separator), + SECURITY_IMPL_PROVIDER("UserAuthenticationProvider", "/templates/stereotype/security/impl/provider.java", "security" + File.separator + "impl" + File.separator), + SECURITY_IMPL_USERGETTER("AbstractUserGetter", "/templates/stereotype/security/impl/usergetter.java", "security" + File.separator + "impl" + File.separator), + SECURITY_IMPL_VERIFYCODE("VerifyCode", "/templates/stereotype/security/impl/verifycode.java", "security" + File.separator + "impl" + File.separator), + UTIL_JWT("JwtUtil", "/templates/stereotype/util/jwt.java", "util" + File.separator), + UTIL_RSA("RSAUtils", "/templates/stereotype/util/rsa.java", "util" + File.separator), + UTIL_SPRING("SpringContextUtil", "/templates/stereotype/util/spring.java", "util" + File.separator), + UTIL_VALID("ValidGroup", "/templates/stereotype/util/valid.java", "util" + File.separator), + VO_APPCODE("AppCode", "/templates/stereotype/vo/appcode.java", "vo" + File.separator), + VO_LAYUI("LayuiTableVo", "/templates/stereotype/vo/layui.java", "vo" + File.separator), + VO_PAGEINFO("PageInfo", "/templates/stereotype/vo/pageinfo.java", "vo" + File.separator), + VO_RESULTCODE("ResultCode", "/templates/stereotype/vo/resultcode.java", "vo" + File.separator), + VO_RESULTVO("ResultVo", "/templates/stereotype/vo/resultvo.java", "vo" + File.separator), + VO_STATUSCODE("StatusCode", "/templates/stereotype/vo/statuscode.java", "vo" + File.separator), + ; + + private String name; + private String path; + private String route; + + public static void generator(String path, HashMap objectMap, FreemarkerTemplateEngine engine){ + StereotypePath [] enums = values(); + for (StereotypePath e: enums) { + if (engine.isCreate(FileType.OTHER, path + File.separator + e.getRoute() + File.separator + e.getName() + ".java")) { + try { + engine.writerFile(objectMap, e.getPath() + ".ftl", path + File.separator + e.getRoute() + File.separator + e.getName() + ".java"); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } + } + +} diff --git a/src/main/java/org/layuiframework/stereotype/util/FileUtil.java b/src/main/java/org/layuiframework/generator/util/FileUtil.java similarity index 97% rename from src/main/java/org/layuiframework/stereotype/util/FileUtil.java rename to src/main/java/org/layuiframework/generator/util/FileUtil.java index 93937ca8f856fc9be65c81e576b7f40877baff5d..521b45d807992b002b1c3f219b7b69db8d1b5e2a 100644 --- a/src/main/java/org/layuiframework/stereotype/util/FileUtil.java +++ b/src/main/java/org/layuiframework/generator/util/FileUtil.java @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.util; +package org.layuiframework.generator.util; import java.io.*; diff --git a/src/main/java/org/layuiframework/stereotype/elasticsearch/ElasticsearchHandlers.java b/src/main/java/org/layuiframework/stereotype/elasticsearch/ElasticsearchHandlers.java deleted file mode 100644 index f902deb6ddf0007a85280e400b180c1d19209c2b..0000000000000000000000000000000000000000 --- a/src/main/java/org/layuiframework/stereotype/elasticsearch/ElasticsearchHandlers.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.layuiframework.stereotype.elasticsearch; - -import java.util.List; -import java.util.Map; - -public abstract class ElasticsearchHandlers { - public abstract Map> search(ElasticsearchVo vo); -} diff --git a/src/main/java/org/layuiframework/stereotype/security/impl/AbstractUserGetter.java b/src/main/java/org/layuiframework/stereotype/security/impl/AbstractUserGetter.java deleted file mode 100644 index eceb1a5de6d69589b11e5a2ed779a504b06b59b5..0000000000000000000000000000000000000000 --- a/src/main/java/org/layuiframework/stereotype/security/impl/AbstractUserGetter.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.layuiframework.stereotype.security.impl; - -import org.layuiframework.stereotype.security.bean.AbstractUser; - -public abstract class AbstractUserGetter { - - public abstract T getUser(String username); - -} diff --git a/src/main/java/org/layuiframework/stereotype/util/RSAUtils.java b/src/main/java/org/layuiframework/stereotype/util/RSAUtils.java deleted file mode 100644 index 2f03184861af2e0718d93ba9294ec094e2f48875..0000000000000000000000000000000000000000 --- a/src/main/java/org/layuiframework/stereotype/util/RSAUtils.java +++ /dev/null @@ -1,80 +0,0 @@ -package org.layuiframework.stereotype.util; - -import org.apache.tomcat.util.codec.binary.Base64; -import org.layuiframework.stereotype.security.bean.AbstractKeyPair; -import org.springframework.stereotype.Component; - -import javax.annotation.Resource; -import javax.crypto.Cipher; -import java.security.KeyFactory; -import java.security.interfaces.RSAPrivateKey; -import java.security.interfaces.RSAPublicKey; -import java.security.spec.PKCS8EncodedKeySpec; -import java.security.spec.X509EncodedKeySpec; - -@Component -public class RSAUtils { - - @Resource - AbstractKeyPair abstractKeyPair; - - /** - * RSA公钥加密 - * - * @param str - * 加密字符串 - * 公钥 - * @return 密文 - * @throws Exception - * 加密过程中的异常信息 - */ - public String encrypt( String str ) throws Exception{ - //base64编码的公钥 - byte[] decoded = Base64.decodeBase64(abstractKeyPair.getPublicKey()); - RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); - //RSA加密 - Cipher cipher = Cipher.getInstance("RSA"); - cipher.init(Cipher.ENCRYPT_MODE, pubKey); - String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8"))); - return outStr; - } - - /** - * RSA私钥解密 - * - * @param str - * 加密字符串 - * @return 铭文 - * @throws Exception - * 解密过程中的异常信息 - */ - public String decrypt(String str) { - try { - //64位解码加密后的字符串 - byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8")); - //base64编码的私钥 - byte[] decoded = Base64.decodeBase64(abstractKeyPair.getPrivateKey()); - RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded)); - //RSA解密 - Cipher cipher = Cipher.getInstance("RSA"); - cipher.init(Cipher.DECRYPT_MODE, priKey); - String outStr = new String(cipher.doFinal(inputByte)); - return outStr; - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return null; - } - - public static void main(String[] args) throws Exception { -// String presentedPassword = "Qw123456@"; -// // 这里是传入校验的密码 -// System.out.println(encrypt(presentedPassword)); -// PasswordEncoder encode = new BCryptPasswordEncoder(); -// String bc = encode.encode(presentedPassword); -// // 这里是数据库里面的密码 -// System.out.println("数据库里面保存的密码.........."); -// System.out.println(bc); - } -} diff --git a/src/main/java/org/ymf/LayuiGeneratorApplication.java b/src/main/java/org/ymf/LayuiGeneratorApplication.java index dcae71660892921a76fa14a1e60a9dadc6ee4f5a..6015a4d4ae552000a1d1de33dc9df4ff2de9733c 100644 --- a/src/main/java/org/ymf/LayuiGeneratorApplication.java +++ b/src/main/java/org/ymf/LayuiGeneratorApplication.java @@ -3,8 +3,10 @@ package org.ymf; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; +import org.springframework.scheduling.annotation.EnableScheduling; @EnableCaching +@EnableScheduling @SpringBootApplication public class LayuiGeneratorApplication { diff --git a/src/main/resources/templates/config/advice.java.ftl b/src/main/resources/templates/config/advice.java.ftl index cc1f7353ac740d4409b42f44608124ba73ce2ff7..4130666cae06e8dd956537c016302fe475928d84 100644 --- a/src/main/resources/templates/config/advice.java.ftl +++ b/src/main/resources/templates/config/advice.java.ftl @@ -1,14 +1,9 @@ package ${package.Advice}; import com.github.pagehelper.PageInfo; -import org.layuiframework.stereotype.advice.ControllerExceptionAdvice; -import org.layuiframework.stereotype.annotation.NotControllerResponseAdvice; -import org.layuiframework.stereotype.aop.ImportFileAopByEasyExcel; -import org.layuiframework.stereotype.config.SpringMvcConfig; -import org.layuiframework.stereotype.util.SpringContextUtil; -import org.layuiframework.stereotype.vo.LayuiTableVo; -import org.layuiframework.stereotype.vo.ResultVo; -import org.springframework.context.annotation.Import; +import ${package.Stereotype}.annotation.NotControllerResponseAdvice; +import ${package.Stereotype}.vo.LayuiTableVo; +import ${package.Stereotype}.vo.ResultVo; import org.springframework.core.MethodParameter; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; @@ -25,7 +20,6 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; * @author ${author} * @since ${date} */ -@Import({ControllerExceptionAdvice.class, ImportFileAopByEasyExcel.class, SpringContextUtil.class, SpringMvcConfig.class}) @RestControllerAdvice(basePackages = {"${package.Controller}"}) public class ${table.adviceName} implements ResponseBodyAdvice{ diff --git a/src/main/resources/templates/config/datasource.java.ftl b/src/main/resources/templates/config/datasource.java.ftl index 2f4eb75a1e4d18166eb0dc41864e0b2fbc416d35..c1ea1ada653157d53e49c5b735d18e9225d4f9d9 100644 --- a/src/main/resources/templates/config/datasource.java.ftl +++ b/src/main/resources/templates/config/datasource.java.ftl @@ -3,7 +3,7 @@ package ${package.Advice}; import com.alibaba.druid.pool.DruidDataSource; import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; -import org.layuiframework.stereotype.advice.MyMetaObjectHandler; +import ${package.Stereotype}.handler.MyMetaObjectHandler; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; diff --git a/src/main/resources/templates/controller.java.ftl b/src/main/resources/templates/controller.java.ftl index 64b1c6e1dc59b62ad67bfd605c5bc0f3ac6a37e5..d0b6e07c5bcd436f0e3587ce8d57b98678097625 100644 --- a/src/main/resources/templates/controller.java.ftl +++ b/src/main/resources/templates/controller.java.ftl @@ -11,7 +11,7 @@ import com.github.pagehelper.PageInfo; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.layuiframework.stereotype.util.ValidGroup; +import ${package.Stereotype}.util.ValidGroup; import java.util.ArrayList; import java.util.List; import ${package.Service}.${table.serviceName}; diff --git a/src/main/resources/templates/entity.java.ftl b/src/main/resources/templates/entity.java.ftl index 8cd971a1f8aa42a5c2c208ab247b34a5c4367add..c21faa1bd7da94c775a89082e4ec54e6fecbec61 100644 --- a/src/main/resources/templates/entity.java.ftl +++ b/src/main/resources/templates/entity.java.ftl @@ -12,7 +12,7 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; <#if ES_URIS??> -import org.layuiframework.stereotype.elasticsearch.ElasticsearchRecord; +import ${package.Stereotype}.elasticsearch.ElasticsearchRecord; <#if entityLombokModel> import lombok.Data; diff --git a/src/main/resources/templates/es/eshandler.java.ftl b/src/main/resources/templates/es/eshandler.java.ftl index 9252a374d1b953c5edad17b06ee483256b7f7dc7..601b3a07b86d655605930cef52328ccc20e44067 100644 --- a/src/main/resources/templates/es/eshandler.java.ftl +++ b/src/main/resources/templates/es/eshandler.java.ftl @@ -3,9 +3,9 @@ package ${package.Es}; import javax.annotation.Resource; import com.github.pagehelper.PageInfo; import lombok.extern.slf4j.Slf4j; -import org.layuiframework.stereotype.elasticsearch.ElasticsearchHandler; -import org.layuiframework.stereotype.elasticsearch.ElasticsearchRecord; -import org.layuiframework.stereotype.elasticsearch.ElasticsearchVo; +import ${package.Stereotype}.elasticsearch.ElasticsearchHandler; +import ${package.Stereotype}.elasticsearch.ElasticsearchRecord; +import ${package.Stereotype}.elasticsearch.ElasticsearchVo; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import ${package.Service}.${table.serviceName}; diff --git a/src/main/resources/templates/security/controller.java.ftl b/src/main/resources/templates/security/controller.java.ftl index 87d026fef0370d8b2fd40fdecf142221ed32dd1b..c165f50ad083ec67c69ad6f389cb47f9c0c79c32 100644 --- a/src/main/resources/templates/security/controller.java.ftl +++ b/src/main/resources/templates/security/controller.java.ftl @@ -1,11 +1,11 @@ package ${package.Controller}; import lombok.extern.slf4j.Slf4j; -import org.layuiframework.stereotype.security.VerifyCode; -import org.layuiframework.stereotype.security.bean.AbstractUser; -import org.layuiframework.stereotype.security.bean.LoginUser; -import org.layuiframework.stereotype.security.impl.AbstractUserGetter; -import org.layuiframework.stereotype.security.impl.CacheServiceImpl; +import ${package.Stereotype}.security.impl.VerifyCode; +import ${package.Stereotype}.security.bean.AbstractUser; +import ${package.Stereotype}.security.bean.LoginUser; +import ${package.Stereotype}.security.impl.AbstractUserGetter; +import ${package.Stereotype}.security.impl.CacheServiceImpl; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -19,10 +19,10 @@ import java.util.HashMap; import java.util.Map; <#if ES_URIS??> import org.springframework.context.annotation.Import; -import org.layuiframework.stereotype.elasticsearch.ElasticsearchConfiguration; -import org.layuiframework.stereotype.elasticsearch.ElasticsearchHandlers; -import org.layuiframework.stereotype.elasticsearch.ElasticsearchRecord; -import org.layuiframework.stereotype.elasticsearch.ElasticsearchVo; +import ${package.Stereotype}.elasticsearch.ElasticsearchConfiguration; +import ${package.Stereotype}.elasticsearch.ElasticsearchHandlers; +import ${package.Stereotype}.elasticsearch.ElasticsearchRecord; +import ${package.Stereotype}.elasticsearch.ElasticsearchVo; import org.springframework.web.bind.annotation.RequestBody; import java.util.List; diff --git a/src/main/resources/templates/security/keypair.java.ftl b/src/main/resources/templates/security/keypair.java.ftl index ac04696626673eef98b8f903a7546ec49b58da52..084d11b06998ac4bc80e3ea80b15c19808378201 100644 --- a/src/main/resources/templates/security/keypair.java.ftl +++ b/src/main/resources/templates/security/keypair.java.ftl @@ -1,7 +1,7 @@ package ${package.Security}; import org.apache.tomcat.util.codec.binary.Base64; -import org.layuiframework.stereotype.security.bean.AbstractKeyPair; +import ${package.Stereotype}.security.bean.AbstractKeyPair; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Component; diff --git a/src/main/resources/templates/security/securityconfig.java.ftl b/src/main/resources/templates/security/securityconfig.java.ftl index 8d31938982e0b444ad497876cf69cde241fad817..c1404b368b1d58758bc46268ae68db7367119f38 100644 --- a/src/main/resources/templates/security/securityconfig.java.ftl +++ b/src/main/resources/templates/security/securityconfig.java.ftl @@ -1,16 +1,16 @@ package ${package.Security}; import lombok.extern.slf4j.Slf4j; -import org.layuiframework.stereotype.security.UserAuthenticationProvider; -import org.layuiframework.stereotype.security.bean.AbstractUser; -import org.layuiframework.stereotype.security.impl.AbstractUserGetter; -import org.layuiframework.stereotype.security.impl.LoginAttemptService; -import org.layuiframework.stereotype.util.RSAUtils; +import ${package.Stereotype}.security.filter.JwtAuthorizationFilter; +import ${package.Stereotype}.security.filter.VerifyCodeFilter; +import ${package.Stereotype}.security.handler.RestfulAccessDeniedHandler; +import ${package.Stereotype}.security.impl.CacheServiceImpl; +import ${package.Stereotype}.security.bean.AbstractUser; +import ${package.Stereotype}.security.impl.AbstractUserGetter; import org.springframework.beans.BeanUtils; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; import org.springframework.core.annotation.Order; import org.springframework.security.authentication.CachingUserDetailsService; import org.springframework.security.config.annotation.SecurityConfigurerAdapter; @@ -33,14 +33,6 @@ import org.springframework.security.web.authentication.AuthenticationSuccessHand import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; import org.springframework.util.Assert; -import org.layuiframework.stereotype.security.authentication.AuthenticationFailure; -import org.layuiframework.stereotype.security.authentication.AuthenticationSuccess; -import org.layuiframework.stereotype.security.filter.JwtAuthorizationFilter; -import org.layuiframework.stereotype.security.filter.VerifyCodeFilter; -import org.layuiframework.stereotype.security.handler.LogoutSuccess; -import org.layuiframework.stereotype.security.handler.AjaxAuthenticationEntryPoint; -import org.layuiframework.stereotype.security.handler.RestfulAccessDeniedHandler; -import org.layuiframework.stereotype.security.impl.CacheServiceImpl; import javax.annotation.Resource; import java.lang.reflect.Constructor; @@ -65,7 +57,6 @@ import java.util.concurrent.ConcurrentHashMap; */ @Slf4j @Configuration -@Import({AuthenticationSuccess.class, AuthenticationFailure.class, AjaxAuthenticationEntryPoint.class, VerifyCodeFilter.class, CacheServiceImpl.class, LogoutSuccess.class, RestfulAccessDeniedHandler.class, UserAuthenticationProvider.class, RSAUtils.class, LoginAttemptService.class}) @EnableWebSecurity @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) public class WebSecurityConfig { diff --git a/src/main/resources/templates/security/webuser.java.ftl b/src/main/resources/templates/security/webuser.java.ftl index 362041db42dcb84ac5b0b06c7713940e5b33e10e..a925925783630c2ce572288ecca9feb0d5b6fb76 100644 --- a/src/main/resources/templates/security/webuser.java.ftl +++ b/src/main/resources/templates/security/webuser.java.ftl @@ -3,7 +3,7 @@ package ${package.Security}; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; -import org.layuiframework.stereotype.security.bean.AbstractUser; +import ${package.Stereotype}.security.bean.AbstractUser; import java.util.ArrayList; import java.util.List; diff --git a/src/main/resources/templates/serviceImpl.java.ftl b/src/main/resources/templates/serviceImpl.java.ftl index 46adfbeb99e1991a39c53d745b42d41c476a5d01..d68b9260a406aa2f2e3e4872e8cc6865feaf03f9 100644 --- a/src/main/resources/templates/serviceImpl.java.ftl +++ b/src/main/resources/templates/serviceImpl.java.ftl @@ -26,8 +26,8 @@ import com.alibaba.excel.EasyExcel; import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageHelper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import org.layuiframework.stereotype.exception.ThrowUtils; -import org.layuiframework.stereotype.vo.ResultCode; +import ${package.Stereotype}.exception.ThrowUtils; +import ${package.Stereotype}.vo.ResultCode; import ${package.Entity}.${entity}; import ${package.Mapper}.${table.mapperName}; import ${package.Service}.${table.serviceName}; @@ -36,7 +36,7 @@ import ${package.Vo}.${table.voName}; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; -import org.layuiframework.stereotype.annotation.ImportFromFile; +import ${package.Stereotype}.annotation.ImportFromFile; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; diff --git a/src/main/java/org/layuiframework/stereotype/advice/ControllerExceptionAdvice.java b/src/main/resources/templates/stereotype/advice/exception.java.ftl similarity index 73% rename from src/main/java/org/layuiframework/stereotype/advice/ControllerExceptionAdvice.java rename to src/main/resources/templates/stereotype/advice/exception.java.ftl index 4572c8a69e32e5b2e4b192db2b3df5204f15aa3a..cf4354cc3822a4726a13ba8ff72ee845b43fa08f 100644 --- a/src/main/java/org/layuiframework/stereotype/advice/ControllerExceptionAdvice.java +++ b/src/main/resources/templates/stereotype/advice/exception.java.ftl @@ -1,13 +1,16 @@ -package org.layuiframework.stereotype.advice; +package ${package.Stereotype}.advice; -import org.layuiframework.stereotype.exception.APIException; -import org.layuiframework.stereotype.vo.ResultCode; -import org.layuiframework.stereotype.vo.ResultVo; +import ${package.Stereotype}.exception.APIException; +import ${package.Stereotype}.vo.ResultCode; +import ${package.Stereotype}.vo.ResultVo; import org.springframework.validation.BindException; import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; +/** + * spring boot 全局异常处理,统一返回 {@linkplain ResultVo 返回对象} + */ @RestControllerAdvice public class ControllerExceptionAdvice { @@ -18,7 +21,7 @@ public class ControllerExceptionAdvice { } @ExceptionHandler({APIException.class}) - public ResultVo MethodApiExceptionHandler(APIException e){ + public ResultVo MethodApiExceptionHandler(APIException e){ return new ResultVo(e.getCode(), e.getMsg(), e.getMessage()); } diff --git a/src/main/java/org/layuiframework/stereotype/annotation/NotControllerResponseAdvice.java b/src/main/resources/templates/stereotype/annotation/advice.java.ftl similarity index 88% rename from src/main/java/org/layuiframework/stereotype/annotation/NotControllerResponseAdvice.java rename to src/main/resources/templates/stereotype/annotation/advice.java.ftl index 98a4b37e163617edf52d0d1f5932b73f0add7119..5a36ed708eeefecb84ed13ec3abb350e0897db2f 100644 --- a/src/main/java/org/layuiframework/stereotype/annotation/NotControllerResponseAdvice.java +++ b/src/main/resources/templates/stereotype/annotation/advice.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.annotation; +package ${package.Stereotype}.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/src/main/java/org/layuiframework/stereotype/annotation/ImportFromFile.java b/src/main/resources/templates/stereotype/annotation/excel.java.ftl similarity index 88% rename from src/main/java/org/layuiframework/stereotype/annotation/ImportFromFile.java rename to src/main/resources/templates/stereotype/annotation/excel.java.ftl index 1f550897d1054048a14cb1317922b3724ddcc00d..2e5e76718b51becbaec10ca7023b6a5877fea657 100644 --- a/src/main/java/org/layuiframework/stereotype/annotation/ImportFromFile.java +++ b/src/main/resources/templates/stereotype/annotation/excel.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.annotation; +package ${package.Stereotype}.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/src/main/java/org/layuiframework/stereotype/aop/ImportFileAopByEasyExcel.java b/src/main/resources/templates/stereotype/aop/excel.java.ftl similarity index 91% rename from src/main/java/org/layuiframework/stereotype/aop/ImportFileAopByEasyExcel.java rename to src/main/resources/templates/stereotype/aop/excel.java.ftl index 40270e31280ec656df33a5fa2139fa07c1178dcc..4f2f612194c83d0221283f74af5dd0bbf7a94fc2 100644 --- a/src/main/java/org/layuiframework/stereotype/aop/ImportFileAopByEasyExcel.java +++ b/src/main/resources/templates/stereotype/aop/excel.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.aop; +package ${package.Stereotype}.aop; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.read.listener.PageReadListener; @@ -9,8 +9,8 @@ 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.layuiframework.stereotype.exception.APIException; -import org.layuiframework.stereotype.util.SpringContextUtil; +import ${package.Stereotype}.exception.APIException; +import ${package.Stereotype}.util.SpringContextUtil; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; @@ -26,7 +26,7 @@ import java.util.concurrent.atomic.AtomicBoolean; @Slf4j public class ImportFileAopByEasyExcel { - @Pointcut("@annotation(org.layuiframework.stereotype.annotation.ImportFromFile)") + @Pointcut("@annotation(${package.Stereotype}.annotation.ImportFromFile)") public void importFromFile(){ } @@ -48,9 +48,6 @@ public class ImportFileAopByEasyExcel { */ @Before(value = "importFromFile()") public void beforeImportFileAdvice(JoinPoint point){ - /** - * 1. 获取mybaitsplus的service实现类中的dto泛型 - */ /** * 检查类是否是mybaitsplus的service实现类 * 如果不是就不继续处理 @@ -121,18 +118,19 @@ public class ImportFileAopByEasyExcel { } } +<#assign T = 'T' /> /** * 从excel文件里面查出来信息,然后返回到集合中 * @param file 文件数据源 * @param clazz dto类 - * @param dto泛型 + * @param <${T}> dto泛型 * @return dto集合 */ - public static List doRead(MultipartFile file, Class clazz){ + public static <${T}> List<${T}> doRead(MultipartFile file, Class<${T}> clazz){ AtomicBoolean flag = new AtomicBoolean(true); - List list = new ArrayList<>(); + List<${T}> list = new ArrayList<>(); try { - EasyExcel.read(file.getInputStream(), clazz, new PageReadListener(dataList -> { + EasyExcel.read(file.getInputStream(), clazz, new PageReadListener<${T}>(dataList -> { list.addAll(dataList); })).sheet().headRowNumber(3).doRead();; }catch (IOException e){ @@ -141,5 +139,4 @@ public class ImportFileAopByEasyExcel { } return flag.get() ? list : new ArrayList<>(); } - } diff --git a/src/main/java/org/layuiframework/stereotype/config/SpringMvcConfig.java b/src/main/resources/templates/stereotype/config/springmvc.java.ftl similarity index 62% rename from src/main/java/org/layuiframework/stereotype/config/SpringMvcConfig.java rename to src/main/resources/templates/stereotype/config/springmvc.java.ftl index adb3d4e16365b2fff4ed2ba32ca7bb4332e5eb8b..6e1fc0353cb86f6636cfb6b1835a34d60b2877e3 100644 --- a/src/main/java/org/layuiframework/stereotype/config/SpringMvcConfig.java +++ b/src/main/resources/templates/stereotype/config/springmvc.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.config; +package ${package.Stereotype}.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; @@ -10,11 +10,11 @@ public class SpringMvcConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") - .allowedHeaders("*") - .allowedMethods("*") - .allowCredentials(true) - .maxAge(1800) - .allowedOriginPatterns("*"); + .allowedHeaders("*") + .allowedMethods("*") + .allowCredentials(true) + .maxAge(1800) + .allowedOriginPatterns("*"); } } diff --git a/src/main/java/org/layuiframework/stereotype/elasticsearch/ElasticsearchConfiguration.java b/src/main/resources/templates/stereotype/elasticsearch/config.java.ftl similarity index 51% rename from src/main/java/org/layuiframework/stereotype/elasticsearch/ElasticsearchConfiguration.java rename to src/main/resources/templates/stereotype/elasticsearch/config.java.ftl index 66f76eeae499bf29f43f07649f9cc7e52e1dc56a..a2f8cc1698c6fa2c4cd8580679df12822467cac5 100644 --- a/src/main/java/org/layuiframework/stereotype/elasticsearch/ElasticsearchConfiguration.java +++ b/src/main/resources/templates/stereotype/elasticsearch/config.java.ftl @@ -1,7 +1,7 @@ -package org.layuiframework.stereotype.elasticsearch; +package ${package.Stereotype}.elasticsearch; -import org.layuiframework.stereotype.exception.APIException; -import org.layuiframework.stereotype.vo.ResultCode; +import ${package.Stereotype}.exception.APIException; +import ${package.Stereotype}.vo.ResultCode; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.util.StringUtils; @@ -16,27 +16,31 @@ import java.util.concurrent.ConcurrentHashMap; @Configuration public class ElasticsearchConfiguration { +<#assign String = 'String' /> +<#assign CompletableFuture = 'CompletableFuture' /> +<#assign ElasticsearchRecord = 'ElasticsearchRecord' /> +<#assign ElasticsearchHandler = 'ElasticsearchHandler' /> @Bean - public ElasticsearchHandlers init(List handlers){ + public ElasticsearchHandlers init(List<${ElasticsearchHandler}> handlers){ return new ElasticsearchHandlers() { @Override - public Map> search(ElasticsearchVo vo) { - Map> res = new ConcurrentHashMap<>(); - List tasks = new ArrayList<>(); + public Map<${String}, List<${ElasticsearchRecord}>> search(ElasticsearchVo vo) { + Map<${String}, List<${ElasticsearchRecord}>> res = new ConcurrentHashMap<>(); + List<${CompletableFuture}> tasks = new ArrayList<>(); for (ElasticsearchHandler e : handlers) { if(e.canDeal(vo)) - tasks.add(CompletableFuture.supplyAsync(() -> { - Map> map = new HashMap<>(); - map.put(e.getType(), e.deal(vo)); - return map; - })); + tasks.add(CompletableFuture.supplyAsync(() -> { + Map<${String}, List<${ElasticsearchRecord}>> map = new HashMap<>(); + map.put(e.getType(), e.deal(vo)); + return map; + })); } if(tasks.isEmpty()) return null; CompletableFuture [] c = new CompletableFuture[tasks.size()]; CompletableFuture.allOf(tasks.toArray(c)).join(); tasks.forEach(t -> { try { - res.putAll((Map>)t.get()); + res.putAll((Map<${String}, List<${ElasticsearchRecord}>>)t.get()); } catch (Exception e) { throw new APIException(ResultCode.NETERROR, e.getMessage()); } diff --git a/src/main/java/org/layuiframework/stereotype/elasticsearch/ElasticsearchHandler.java b/src/main/resources/templates/stereotype/elasticsearch/handler.java.ftl similarity index 44% rename from src/main/java/org/layuiframework/stereotype/elasticsearch/ElasticsearchHandler.java rename to src/main/resources/templates/stereotype/elasticsearch/handler.java.ftl index 66a230f3d29c4faa2eccca433299d43257539129..9adb6cf002fdd2c4fdea663511318ee44461e0c8 100644 --- a/src/main/java/org/layuiframework/stereotype/elasticsearch/ElasticsearchHandler.java +++ b/src/main/resources/templates/stereotype/elasticsearch/handler.java.ftl @@ -1,9 +1,10 @@ -package org.layuiframework.stereotype.elasticsearch; +package ${package.Stereotype}.elasticsearch; import java.util.List; public interface ElasticsearchHandler { String getType(); boolean canDeal(ElasticsearchVo vo); - List deal(ElasticsearchVo vo); + <#assign ElasticsearchRecord = 'ElasticsearchRecord' /> + List<${ElasticsearchRecord}> deal(ElasticsearchVo vo); } diff --git a/src/main/resources/templates/stereotype/elasticsearch/handlers.java.ftl b/src/main/resources/templates/stereotype/elasticsearch/handlers.java.ftl new file mode 100644 index 0000000000000000000000000000000000000000..fc1e61ca7f88529802a062ebda049b6ba5b90e70 --- /dev/null +++ b/src/main/resources/templates/stereotype/elasticsearch/handlers.java.ftl @@ -0,0 +1,10 @@ +package ${package.Stereotype}.elasticsearch; + +import java.util.List; +import java.util.Map; + +public abstract class ElasticsearchHandlers { + <#assign String = 'String' /> + <#assign ElasticsearchRecord = 'ElasticsearchRecord' /> + public abstract Map<${String}, List<${ElasticsearchRecord}>> search(ElasticsearchVo vo); +} diff --git a/src/main/java/org/layuiframework/stereotype/elasticsearch/ElasticsearchRecord.java b/src/main/resources/templates/stereotype/elasticsearch/record.java.ftl similarity index 83% rename from src/main/java/org/layuiframework/stereotype/elasticsearch/ElasticsearchRecord.java rename to src/main/resources/templates/stereotype/elasticsearch/record.java.ftl index 7e59a5ebc2558f5d9b10cbd83ee10ac1da703b08..fd622a53f9f5ebcb4a0e7103ef29d08dc7727123 100644 --- a/src/main/java/org/layuiframework/stereotype/elasticsearch/ElasticsearchRecord.java +++ b/src/main/resources/templates/stereotype/elasticsearch/record.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.elasticsearch; +package ${package.Stereotype}.elasticsearch; import com.baomidou.mybatisplus.annotation.TableField; import lombok.Data; diff --git a/src/main/java/org/layuiframework/stereotype/elasticsearch/ElasticsearchVo.java b/src/main/resources/templates/stereotype/elasticsearch/vo.java.ftl similarity index 55% rename from src/main/java/org/layuiframework/stereotype/elasticsearch/ElasticsearchVo.java rename to src/main/resources/templates/stereotype/elasticsearch/vo.java.ftl index 40bfe438f70f4086d65bf4b71abd6653be178183..8b3532c12ad8843f02a5ab9d0a773aa24f62a26a 100644 --- a/src/main/java/org/layuiframework/stereotype/elasticsearch/ElasticsearchVo.java +++ b/src/main/resources/templates/stereotype/elasticsearch/vo.java.ftl @@ -1,7 +1,7 @@ -package org.layuiframework.stereotype.elasticsearch; +package ${package.Stereotype}.elasticsearch; import lombok.Data; -import org.layuiframework.stereotype.vo.PageInfo; +import ${package.Stereotype}.vo.PageInfo; @Data public class ElasticsearchVo extends PageInfo { diff --git a/src/main/java/org/layuiframework/stereotype/exception/APIException.java b/src/main/resources/templates/stereotype/exception/api.java.ftl similarity index 77% rename from src/main/java/org/layuiframework/stereotype/exception/APIException.java rename to src/main/resources/templates/stereotype/exception/api.java.ftl index 88ed7e2e052044e3b5fb837ef5ecb55fa02e414d..fd4fe7f872131e532826ae51511955b310250f15 100644 --- a/src/main/java/org/layuiframework/stereotype/exception/APIException.java +++ b/src/main/resources/templates/stereotype/exception/api.java.ftl @@ -1,8 +1,8 @@ -package org.layuiframework.stereotype.exception; +package ${package.Stereotype}.exception; import lombok.Getter; -import org.layuiframework.stereotype.vo.AppCode; -import org.layuiframework.stereotype.vo.ResultCode; +import ${package.Stereotype}.vo.AppCode; +import ${package.Stereotype}.vo.ResultCode; @Getter public class APIException extends RuntimeException { diff --git a/src/main/java/org/layuiframework/stereotype/exception/ThrowUtils.java b/src/main/resources/templates/stereotype/exception/util.java.ftl similarity index 78% rename from src/main/java/org/layuiframework/stereotype/exception/ThrowUtils.java rename to src/main/resources/templates/stereotype/exception/util.java.ftl index 680fb55cdb4afb38ea3004e55bd0485d8349ceb4..006b7fbc437b669b7047aff9b8ff1423fbe7c923 100644 --- a/src/main/java/org/layuiframework/stereotype/exception/ThrowUtils.java +++ b/src/main/resources/templates/stereotype/exception/util.java.ftl @@ -1,12 +1,11 @@ -package org.layuiframework.stereotype.exception; +package ${package.Stereotype}.exception; -import org.layuiframework.stereotype.vo.ResultCode; +import ${package.Stereotype}.vo.ResultCode; /** * 抛异常工具类 - * - * @author 程序员鱼皮 - * @from 编程导航知识星球 + * "https://github.com/liyupi" 程序员鱼皮 + * "https://yupi.icu" 编程导航知识星球 */ public class ThrowUtils { diff --git a/src/main/java/org/layuiframework/stereotype/advice/MyMetaObjectHandler.java b/src/main/resources/templates/stereotype/handler/metaobject.java.ftl similarity index 72% rename from src/main/java/org/layuiframework/stereotype/advice/MyMetaObjectHandler.java rename to src/main/resources/templates/stereotype/handler/metaobject.java.ftl index 838e18b6f8ffa7d6e6ded13b0cdbadf2db4e5b22..1453a4feab409903aa2eb4d0fce936a08a530927 100644 --- a/src/main/java/org/layuiframework/stereotype/advice/MyMetaObjectHandler.java +++ b/src/main/resources/templates/stereotype/handler/metaobject.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.advice; +package ${package.Stereotype}.handler; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import lombok.extern.slf4j.Slf4j; @@ -7,6 +7,14 @@ import org.springframework.stereotype.Component; import java.util.Date; +/** + * maybatis-plus 配置: 自动填充属性,仅对设置了fill属性的字段 + * - 插入时 createtime和updatetime均取当前时间 + * - 更新时 updatetime 变为当前时间 + * @desc + * 在数据库操作时配置一些默认填充的行为 + * 在es的场景下更加建议使用updatetime的填充 + */ @Component @Slf4j public class MyMetaObjectHandler implements MetaObjectHandler { diff --git a/src/main/java/org/layuiframework/stereotype/security/authentication/AuthenticationFailure.java b/src/main/resources/templates/stereotype/security/authentication/failure.java.ftl similarity index 84% rename from src/main/java/org/layuiframework/stereotype/security/authentication/AuthenticationFailure.java rename to src/main/resources/templates/stereotype/security/authentication/failure.java.ftl index 4331d0d40fccbe428fb99f46bc9d7b9caf560284..35fc4f9122db7b2dee6e36937a6ea8a52439440f 100644 --- a/src/main/java/org/layuiframework/stereotype/security/authentication/AuthenticationFailure.java +++ b/src/main/resources/templates/stereotype/security/authentication/failure.java.ftl @@ -1,9 +1,8 @@ -package org.layuiframework.stereotype.security.authentication; +package ${package.Stereotype}.security.authentication; import com.fasterxml.jackson.databind.ObjectMapper; -import org.layuiframework.stereotype.security.impl.LoginAttemptService; -import org.layuiframework.stereotype.vo.ResultVo; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import ${package.Stereotype}.security.impl.LoginAttemptService; +import ${package.Stereotype}.vo.ResultVo; import org.springframework.security.authentication.LockedException; import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.authentication.AuthenticationFailureHandler; @@ -19,7 +18,6 @@ import java.io.IOException; * 认证登录失败处理 */ @Component -@ConditionalOnMissingBean(AuthenticationFailureHandler.class) public class AuthenticationFailure implements AuthenticationFailureHandler { @Resource diff --git a/src/main/java/org/layuiframework/stereotype/security/authentication/AuthenticationSuccess.java b/src/main/resources/templates/stereotype/security/authentication/success.java.ftl similarity index 79% rename from src/main/java/org/layuiframework/stereotype/security/authentication/AuthenticationSuccess.java rename to src/main/resources/templates/stereotype/security/authentication/success.java.ftl index 45fcd47974b0fb0dea78f0ddc054af57c6d7779f..b96e1f31db83c06b889397f04198eb5570ec1e1a 100644 --- a/src/main/java/org/layuiframework/stereotype/security/authentication/AuthenticationSuccess.java +++ b/src/main/resources/templates/stereotype/security/authentication/success.java.ftl @@ -1,13 +1,12 @@ -package org.layuiframework.stereotype.security.authentication; +package ${package.Stereotype}.security.authentication; import com.fasterxml.jackson.databind.ObjectMapper; -import org.layuiframework.stereotype.security.bean.AbstractUser; -import org.layuiframework.stereotype.security.bean.LoginUser; -import org.layuiframework.stereotype.security.impl.CacheServiceImpl; -import org.layuiframework.stereotype.security.impl.LoginAttemptService; -import org.layuiframework.stereotype.util.JwtUtil; -import org.layuiframework.stereotype.vo.ResultVo; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import ${package.Stereotype}.security.bean.AbstractUser; +import ${package.Stereotype}.security.bean.LoginUser; +import ${package.Stereotype}.security.impl.CacheServiceImpl; +import ${package.Stereotype}.security.impl.LoginAttemptService; +import ${package.Stereotype}.util.JwtUtil; +import ${package.Stereotype}.vo.ResultVo; import org.springframework.http.HttpStatus; import org.springframework.security.core.Authentication; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; @@ -25,7 +24,6 @@ import java.util.UUID; * 认证登录成功处理 */ @Component -@ConditionalOnMissingBean(AuthenticationSuccessHandler.class) public class AuthenticationSuccess implements AuthenticationSuccessHandler { @Resource @@ -46,7 +44,9 @@ public class AuthenticationSuccess implements AuthenticationSuccessHandler { userToken.setTokenId(UUID.randomUUID().toString().replaceAll("-", "")); AbstractUser user = (AbstractUser) authentication.getPrincipal(); String token = JwtUtil.sign(user.getSecurityUsername(), user.getSecurityPassword(), userToken.getTokenId()); - HashMap map = new HashMap<>(); +<#assign String = 'String' /> +<#assign Object = 'Object' /> + HashMap<${String}, ${Object}> map = new HashMap<>(); map.put("username", user.getSecurityUsername()); map.put("permission", authentication.getAuthorities()); map.put("token", token); diff --git a/src/main/java/org/layuiframework/stereotype/security/bean/AbstractKeyPair.java b/src/main/resources/templates/stereotype/security/bean/keypair.java.ftl similarity index 71% rename from src/main/java/org/layuiframework/stereotype/security/bean/AbstractKeyPair.java rename to src/main/resources/templates/stereotype/security/bean/keypair.java.ftl index 6cf1b2c7daa23629f0e6158e0e40e17162f67790..05af10160aaa49270eb30ffa9ccf7d7ae81b089c 100644 --- a/src/main/java/org/layuiframework/stereotype/security/bean/AbstractKeyPair.java +++ b/src/main/resources/templates/stereotype/security/bean/keypair.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.security.bean; +package ${package.Stereotype}.security.bean; public abstract class AbstractKeyPair { public abstract String getPublicKey(); diff --git a/src/main/java/org/layuiframework/stereotype/security/bean/LoginUser.java b/src/main/resources/templates/stereotype/security/bean/loginuser.java.ftl similarity index 75% rename from src/main/java/org/layuiframework/stereotype/security/bean/LoginUser.java rename to src/main/resources/templates/stereotype/security/bean/loginuser.java.ftl index f5dc785badff66f9f2ad297e8fd92ee35d7390f4..167fb7cfa80e42692b314a5ad71e64e99f27ff62 100644 --- a/src/main/java/org/layuiframework/stereotype/security/bean/LoginUser.java +++ b/src/main/resources/templates/stereotype/security/bean/loginuser.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.security.bean; +package ${package.Stereotype}.security.bean; import lombok.Data; @@ -22,7 +22,8 @@ public class LoginUser { private String pwd;//密码 - private List roleIds;//角色id + <#assign String = 'String' /> + private List<${String}> roleIds;//角色id private String token;//用户token信息 diff --git a/src/main/java/org/layuiframework/stereotype/security/bean/AbstractUser.java b/src/main/resources/templates/stereotype/security/bean/user.java.ftl similarity index 56% rename from src/main/java/org/layuiframework/stereotype/security/bean/AbstractUser.java rename to src/main/resources/templates/stereotype/security/bean/user.java.ftl index 25df8ade83ec3e42f8e957257c5bf501e89d9326..ad72c0b1a994b59d1452b678f97ac82271e1178b 100644 --- a/src/main/java/org/layuiframework/stereotype/security/bean/AbstractUser.java +++ b/src/main/resources/templates/stereotype/security/bean/user.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.security.bean; +package ${package.Stereotype}.security.bean; import java.util.List; @@ -6,6 +6,7 @@ public abstract class AbstractUser { public abstract String getSecurityUsername(); public abstract String getSecurityPassword(); - public abstract List getSecurityRole(); +<#assign String = 'String' /> + public abstract List<${String}> getSecurityRole(); } diff --git a/src/main/java/org/layuiframework/stereotype/security/filter/JwtAuthorizationFilter.java b/src/main/resources/templates/stereotype/security/filter/jwt.java.ftl similarity index 86% rename from src/main/java/org/layuiframework/stereotype/security/filter/JwtAuthorizationFilter.java rename to src/main/resources/templates/stereotype/security/filter/jwt.java.ftl index 4074984370d36bb0ea22f84f6965d140840b18bb..704cd06a38caf98b21eb1797bf038bb3d9a850c6 100644 --- a/src/main/java/org/layuiframework/stereotype/security/filter/JwtAuthorizationFilter.java +++ b/src/main/resources/templates/stereotype/security/filter/jwt.java.ftl @@ -1,8 +1,8 @@ -package org.layuiframework.stereotype.security.filter; +package ${package.Stereotype}.security.filter; -import org.layuiframework.stereotype.security.bean.LoginUser; -import org.layuiframework.stereotype.security.impl.CacheServiceImpl; -import org.layuiframework.stereotype.util.JwtUtil; +import ${package.Stereotype}.security.bean.LoginUser; +import ${package.Stereotype}.security.impl.CacheServiceImpl; +import ${package.Stereotype}.util.JwtUtil; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; @@ -42,7 +42,7 @@ public class JwtAuthorizationFilter extends BasicAuthenticationFilter { Authentication token = getAuthentication(request); //SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(null,null,null)); if (token != null) { - SecurityContextHolder.getContext().setAuthentication(token); + SecurityContextHolder.getContext().setAuthentication(token); } chain.doFilter(request, response); } @@ -56,7 +56,8 @@ public class JwtAuthorizationFilter extends BasicAuthenticationFilter { if (user == null) return null; if (!JwtUtil.verify(token, username, user.getPwd())) return null; cache.setLoginUser(user); - List grantedAuthorities = new ArrayList<>(); + <#assign GrantedAuthority = 'GrantedAuthority' /> + List<${GrantedAuthority}> grantedAuthorities = new ArrayList<>(); if (user != null) user.getRoleIds().stream().forEach(roleId -> grantedAuthorities.add(new SimpleGrantedAuthority(roleId))); return new UsernamePasswordAuthenticationToken(user.getUserName(), user.getPwd(), grantedAuthorities); diff --git a/src/main/java/org/layuiframework/stereotype/security/filter/VerifyCodeFilter.java b/src/main/resources/templates/stereotype/security/filter/verify.java.ftl similarity index 89% rename from src/main/java/org/layuiframework/stereotype/security/filter/VerifyCodeFilter.java rename to src/main/resources/templates/stereotype/security/filter/verify.java.ftl index 5d0024a22cc14d60f2c4c49837866ff8a4c5b03e..4cfcc4496170e41458bbd8be86d69521556198c5 100644 --- a/src/main/java/org/layuiframework/stereotype/security/filter/VerifyCodeFilter.java +++ b/src/main/resources/templates/stereotype/security/filter/verify.java.ftl @@ -1,9 +1,9 @@ -package org.layuiframework.stereotype.security.filter; +package ${package.Stereotype}.security.filter; import com.fasterxml.jackson.databind.ObjectMapper; -import org.layuiframework.stereotype.security.bean.LoginUser; -import org.layuiframework.stereotype.security.impl.CacheServiceImpl; -import org.layuiframework.stereotype.vo.ResultVo; +import ${package.Stereotype}.security.bean.LoginUser; +import ${package.Stereotype}.security.impl.CacheServiceImpl; +import ${package.Stereotype}.vo.ResultVo; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import org.springframework.web.filter.GenericFilterBean; @@ -22,6 +22,7 @@ import java.io.IOException; */ @Component public class VerifyCodeFilter extends GenericFilterBean { + private String defaultFilterProcessUrl = "/index/login"; @Resource @@ -29,7 +30,7 @@ public class VerifyCodeFilter extends GenericFilterBean { @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) - throws IOException, ServletException { + throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; if ("POST".equalsIgnoreCase(request.getMethod()) && request.getRequestURI().endsWith(defaultFilterProcessUrl)) { diff --git a/src/main/java/org/layuiframework/stereotype/security/handler/RestfulAccessDeniedHandler.java b/src/main/resources/templates/stereotype/security/handler/access.java.ftl similarity index 82% rename from src/main/java/org/layuiframework/stereotype/security/handler/RestfulAccessDeniedHandler.java rename to src/main/resources/templates/stereotype/security/handler/access.java.ftl index 43300979a5913392b12456bd2b62337b25c024b8..2a3e463b0b45951a563ee73900d0abf7eb54fb4d 100644 --- a/src/main/java/org/layuiframework/stereotype/security/handler/RestfulAccessDeniedHandler.java +++ b/src/main/resources/templates/stereotype/security/handler/access.java.ftl @@ -1,8 +1,7 @@ -package org.layuiframework.stereotype.security.handler; +package ${package.Stereotype}.security.handler; import com.fasterxml.jackson.databind.ObjectMapper; -import org.layuiframework.stereotype.vo.ResultVo; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import ${package.Stereotype}.vo.ResultVo; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.web.access.AccessDeniedHandler; import org.springframework.stereotype.Component; @@ -17,13 +16,12 @@ import java.io.PrintWriter; * 访问接口没有权限时,自定义返回结果 */ @Component -@ConditionalOnMissingBean(AccessDeniedHandler.class) public class RestfulAccessDeniedHandler implements AccessDeniedHandler { /** * Handles an access denied failure. * - * @param request that resulted in an AccessDeniedException + * @param request that resulted in an AccessDeniedException * @param response so that the user agent can be advised of the failure * @param accessDeniedException that caused the invocation * @throws IOException in the event of an IOException diff --git a/src/main/java/org/layuiframework/stereotype/security/handler/AjaxAuthenticationEntryPoint.java b/src/main/resources/templates/stereotype/security/handler/ajax.java.ftl similarity index 76% rename from src/main/java/org/layuiframework/stereotype/security/handler/AjaxAuthenticationEntryPoint.java rename to src/main/resources/templates/stereotype/security/handler/ajax.java.ftl index 23df5ad0b19b0ece867c052bc87485ef9dd6ad96..a1519fc5caffae2f66688b7a4d5a5431499d6bb0 100644 --- a/src/main/java/org/layuiframework/stereotype/security/handler/AjaxAuthenticationEntryPoint.java +++ b/src/main/resources/templates/stereotype/security/handler/ajax.java.ftl @@ -1,8 +1,7 @@ -package org.layuiframework.stereotype.security.handler; +package ${package.Stereotype}.security.handler; import com.fasterxml.jackson.databind.ObjectMapper; -import org.layuiframework.stereotype.vo.ResultVo; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import ${package.Stereotype}.vo.ResultVo; import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.AuthenticationEntryPoint; import org.springframework.stereotype.Component; @@ -17,7 +16,6 @@ import java.io.IOException; * 如果用户认证或授权过程中出现错误,需要将其重定向到authenticationentrypoint并进行相应的错误处理。 */ @Component -@ConditionalOnMissingBean(AuthenticationEntryPoint.class) public class AjaxAuthenticationEntryPoint implements AuthenticationEntryPoint { @Override @@ -27,8 +25,6 @@ public class AjaxAuthenticationEntryPoint implements AuthenticationEntryPoint { ResultVo responseEntity = new ResultVo(401, "请先登录", null); httpServletResponse.setStatus(401); ObjectMapper mapper=new ObjectMapper(); -// if ("XMLHttpRequest".equals(httpServletRequest.getHeader("X-Requested-With"))) -// httpServletResponse.setStatus(401); httpServletResponse.getWriter().write(mapper.writeValueAsString(responseEntity)); } } diff --git a/src/main/java/org/layuiframework/stereotype/security/handler/LogoutSuccess.java b/src/main/resources/templates/stereotype/security/handler/logout.java.ftl similarity index 77% rename from src/main/java/org/layuiframework/stereotype/security/handler/LogoutSuccess.java rename to src/main/resources/templates/stereotype/security/handler/logout.java.ftl index 2357c150939100a9631b83d78ebb51f2560fedda..9bc431aa7b0f7d31537ea17e757d6e67a28429a9 100644 --- a/src/main/java/org/layuiframework/stereotype/security/handler/LogoutSuccess.java +++ b/src/main/resources/templates/stereotype/security/handler/logout.java.ftl @@ -1,11 +1,10 @@ -package org.layuiframework.stereotype.security.handler; +package ${package.Stereotype}.security.handler; import com.fasterxml.jackson.databind.ObjectMapper; -import org.layuiframework.stereotype.security.bean.LoginUser; -import org.layuiframework.stereotype.security.impl.CacheServiceImpl; -import org.layuiframework.stereotype.util.JwtUtil; -import org.layuiframework.stereotype.vo.ResultVo; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import ${package.Stereotype}.security.bean.LoginUser; +import ${package.Stereotype}.security.impl.CacheServiceImpl; +import ${package.Stereotype}.util.JwtUtil; +import ${package.Stereotype}.vo.ResultVo; import org.springframework.security.core.Authentication; import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; import org.springframework.stereotype.Component; @@ -20,7 +19,6 @@ import java.io.IOException; * 退出登录成功 处理 */ @Component -@ConditionalOnMissingBean(LogoutSuccessHandler.class) public class LogoutSuccess implements LogoutSuccessHandler { @Resource diff --git a/src/main/java/org/layuiframework/stereotype/security/impl/CacheServiceImpl.java b/src/main/resources/templates/stereotype/security/impl/cache.java.ftl similarity index 92% rename from src/main/java/org/layuiframework/stereotype/security/impl/CacheServiceImpl.java rename to src/main/resources/templates/stereotype/security/impl/cache.java.ftl index 140e32acc1e2d8438e450daaf48585ea4c8ce4f9..3928be9f6627362a12d9e35b6a0aa230872c09c4 100644 --- a/src/main/java/org/layuiframework/stereotype/security/impl/CacheServiceImpl.java +++ b/src/main/resources/templates/stereotype/security/impl/cache.java.ftl @@ -1,6 +1,6 @@ -package org.layuiframework.stereotype.security.impl; +package ${package.Stereotype}.security.impl; -import org.layuiframework.stereotype.security.bean.LoginUser; +import ${package.Stereotype}.security.bean.LoginUser; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; diff --git a/src/main/java/org/layuiframework/stereotype/security/impl/LoginAttemptService.java b/src/main/resources/templates/stereotype/security/impl/login.java.ftl similarity index 88% rename from src/main/java/org/layuiframework/stereotype/security/impl/LoginAttemptService.java rename to src/main/resources/templates/stereotype/security/impl/login.java.ftl index dadcbc75bcf7b6e99ec40b79b50ff598f343656e..4c6afb97b9c7dd5d42e5708357b2d63b7d34ef42 100644 --- a/src/main/java/org/layuiframework/stereotype/security/impl/LoginAttemptService.java +++ b/src/main/resources/templates/stereotype/security/impl/login.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.security.impl; +package ${package.Stereotype}.security.impl; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; @@ -20,20 +20,20 @@ public class LoginAttemptService { private final int lockTime = 30 * 60 * 1000; - private final LoadingCache atteptCache; +<#assign String = 'String' /> + private final LoadingCache<${String}, LockUser> atteptCache; public LoginAttemptService() { super(); atteptCache = CacheBuilder - .newBuilder() - .expireAfterWrite(30, TimeUnit.MINUTES)//30分钟重置一次 - .build(new CacheLoader() { - @Override - public LockUser load(String arg0) throws Exception { - return new LockUser(); - } - - }); + .newBuilder() + .expireAfterWrite(30, TimeUnit.MINUTES)//30分钟重置一次 + .build(new CacheLoader<${String}, LockUser>() { + @Override + public LockUser load(String arg0) throws Exception { + return new LockUser(); + } + }); } /** diff --git a/src/main/java/org/layuiframework/stereotype/security/UserAuthenticationProvider.java b/src/main/resources/templates/stereotype/security/impl/provider.java.ftl similarity index 73% rename from src/main/java/org/layuiframework/stereotype/security/UserAuthenticationProvider.java rename to src/main/resources/templates/stereotype/security/impl/provider.java.ftl index 63b7866022b55bbbe7ea9e9ba053b8336fd33ae3..dbede7d542aababa52f3a5860906af719509fbb0 100644 --- a/src/main/java/org/layuiframework/stereotype/security/UserAuthenticationProvider.java +++ b/src/main/resources/templates/stereotype/security/impl/provider.java.ftl @@ -1,9 +1,9 @@ -package org.layuiframework.stereotype.security; +package ${package.Stereotype}.security.impl; -import org.layuiframework.stereotype.security.bean.AbstractUser; -import org.layuiframework.stereotype.security.impl.AbstractUserGetter; -import org.layuiframework.stereotype.security.impl.LoginAttemptService; -import org.layuiframework.stereotype.util.RSAUtils; +import ${package.Stereotype}.security.bean.AbstractUser; +import ${package.Stereotype}.security.impl.AbstractUserGetter; +import ${package.Stereotype}.security.impl.LoginAttemptService; +import ${package.Stereotype}.util.RSAUtils; import org.springframework.context.annotation.Lazy; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.BadCredentialsException; @@ -56,14 +56,15 @@ public class UserAuthenticationProvider implements AuthenticationProvider { else { // 自定义的加密规则,用户名、输的密码和数据库保存的盐值进行加密 if (authentication.getCredentials() == null || !passwordEncoder.matches(presentedPassword, user.getSecurityPassword())) - throw new BadCredentialsException("登录名或密码错误"); + throw new BadCredentialsException("登录名或密码错误"); else { - List grantedAuthorities = new ArrayList<>(); - user.getSecurityRole().stream().forEach(roleId -> grantedAuthorities.add(new SimpleGrantedAuthority(roleId))); - UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(user, authentication.getCredentials(), grantedAuthorities); - result.setDetails(authentication.getDetails()); - return result; - } + <#assign GrantedAuthority = 'GrantedAuthority' /> + List<${GrantedAuthority}> grantedAuthorities = new ArrayList<>(); + user.getSecurityRole().stream().forEach(roleId -> grantedAuthorities.add(new SimpleGrantedAuthority(roleId))); + UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(user, authentication.getCredentials(), grantedAuthorities); + result.setDetails(authentication.getDetails()); + return result; + } } } @@ -73,4 +74,4 @@ public class UserAuthenticationProvider implements AuthenticationProvider { return true; } -} + } diff --git a/src/main/resources/templates/stereotype/security/impl/usergetter.java.ftl b/src/main/resources/templates/stereotype/security/impl/usergetter.java.ftl new file mode 100644 index 0000000000000000000000000000000000000000..7801d9f90a5391c3b03a4a03facc7671adc43689 --- /dev/null +++ b/src/main/resources/templates/stereotype/security/impl/usergetter.java.ftl @@ -0,0 +1,10 @@ +package ${package.Stereotype}.security.impl; + +import ${package.Stereotype}.security.bean.AbstractUser; + +<#assign T = 'T' /> +public abstract class AbstractUserGetter<${T} extends AbstractUser> { + + public abstract T getUser(String username); + +} diff --git a/src/main/java/org/layuiframework/stereotype/security/VerifyCode.java b/src/main/resources/templates/stereotype/security/impl/verifycode.java.ftl similarity index 83% rename from src/main/java/org/layuiframework/stereotype/security/VerifyCode.java rename to src/main/resources/templates/stereotype/security/impl/verifycode.java.ftl index 3d6f127c74377ba1b0f2af7fd5543928793ddd8a..363f344e9191658fc7dea317202178f5c7fcfece 100644 --- a/src/main/java/org/layuiframework/stereotype/security/VerifyCode.java +++ b/src/main/resources/templates/stereotype/security/impl/verifycode.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.security; +package ${package.Stereotype}.security.impl; import org.apache.tomcat.util.codec.binary.Base64; @@ -13,25 +13,26 @@ import java.util.Random; import java.util.concurrent.ConcurrentHashMap; /** - *

+ * * 图片验证码 - *

+ * */ public class VerifyCode { - - public static Map verifyCode = new ConcurrentHashMap(); - - private int width = 120;// 生成验证码图片的宽度 + +<#assign String = 'String' /> + public static Map<${String}, ${String}> verifyCode = new ConcurrentHashMap<${String}, ${String}>(); + + private int width = 120;// 生成验证码图片的宽度 private int height = 50;// 生成验证码图片的高度 private String[] fontNames = { "宋体", "楷体", "隶书", "微软雅黑" }; private Color bgColor = new Color(255, 255, 255);// 定义验证码图片的背景颜色为白色 private Random random = new Random(); private String codes = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ"; private String text;// 记录随机字符串 - + /** * 获取一个随意颜色 - * + * * @return */ private Color randomColor() { @@ -47,18 +48,18 @@ public class VerifyCode { * @throws IOException */ public String getBase64Image(BufferedImage image) throws IOException{ - byte[] imagedata = null; - ByteArrayOutputStream bao=new ByteArrayOutputStream(); - ImageIO.write(image,"png",bao); - imagedata=bao.toByteArray(); - Base64 encoder = new Base64(); - String BASE64IMAGE=encoder.encodeAsString(imagedata).trim(); - BASE64IMAGE = BASE64IMAGE.replaceAll("\r|\n", ""); //删除 \r\n - return BASE64IMAGE; + byte[] imagedata = null; + ByteArrayOutputStream bao=new ByteArrayOutputStream(); + ImageIO.write(image,"png",bao); + imagedata=bao.toByteArray(); + Base64 encoder = new Base64(); + String BASE64IMAGE=encoder.encodeAsString(imagedata).trim(); + BASE64IMAGE = BASE64IMAGE.replaceAll("\r|\n", ""); //删除 \r\n + return BASE64IMAGE; } /** * 获取一个随机字体 - * + * * @return */ private Font randomFont() { @@ -67,19 +68,19 @@ public class VerifyCode { int size = random.nextInt(5) + 35; return new Font(name, style, size); } - + /** * 获取一个随机字符 - * + * * @return */ private char randomChar() { return codes.charAt(random.nextInt(codes.length())); } - + /** * 创建一个空白的BufferedImage对象 - * + * * @return */ private BufferedImage createImage() { @@ -89,7 +90,7 @@ public class VerifyCode { g2.fillRect(0, 0, width, height); return image; } - + public BufferedImage getImage() { BufferedImage image = createImage(); Graphics2D g2 = (Graphics2D) image.getGraphics(); @@ -106,10 +107,10 @@ public class VerifyCode { drawLine(image); return image; } - + /** * 绘制干扰线 - * + * * @param image */ private void drawLine(BufferedImage image) { @@ -125,13 +126,13 @@ public class VerifyCode { g2.drawLine(x1, y1, x2, y2); } } - + public String getText() { return text; } - + public static void output(BufferedImage image, OutputStream out) throws IOException { ImageIO.write(image, "JPEG", out); } - + } diff --git a/src/main/java/org/layuiframework/stereotype/util/JwtUtil.java b/src/main/resources/templates/stereotype/util/jwt.java.ftl similarity index 88% rename from src/main/java/org/layuiframework/stereotype/util/JwtUtil.java rename to src/main/resources/templates/stereotype/util/jwt.java.ftl index 69ba535f850f122873efc4dab273d71b2cce7e09..533c37443452d0fdf4238fdfdca5028cbef29bb9 100644 --- a/src/main/java/org/layuiframework/stereotype/util/JwtUtil.java +++ b/src/main/resources/templates/stereotype/util/jwt.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.util; +package ${package.Stereotype}.util; import com.auth0.jwt.JWT; import com.auth0.jwt.JWTVerifier; @@ -28,10 +28,10 @@ public class JwtUtil { try { Algorithm algorithm = Algorithm.HMAC256(secret); return JWT.create() - .withClaim("username", username) - .withExpiresAt(expireDate) - .withJWTId(tokenId) - .sign(algorithm); + .withClaim("username", username) + .withExpiresAt(expireDate) + .withJWTId(tokenId) + .sign(algorithm); } catch (Exception e) { log.error("生成24h签名失败!", e); return null; @@ -48,9 +48,9 @@ public class JwtUtil { try { Algorithm algorithm = Algorithm.HMAC256(secret); return JWT.create() - .withClaim("username", username) - .withJWTId(tokenId) - .sign(algorithm); + .withClaim("username", username) + .withJWTId(tokenId) + .sign(algorithm); } catch (Exception e) { log.error("生成签名", e); return null; diff --git a/src/main/resources/templates/stereotype/util/rsa.java.ftl b/src/main/resources/templates/stereotype/util/rsa.java.ftl new file mode 100644 index 0000000000000000000000000000000000000000..139ad5cf51807c0de4b2246df383915ee68f6502 --- /dev/null +++ b/src/main/resources/templates/stereotype/util/rsa.java.ftl @@ -0,0 +1,80 @@ +package ${package.Stereotype}.util; + +import org.apache.tomcat.util.codec.binary.Base64; +import ${package.Stereotype}.security.bean.AbstractKeyPair; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import javax.crypto.Cipher; +import java.security.KeyFactory; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; + +@Component +public class RSAUtils { + + @Resource + AbstractKeyPair abstractKeyPair; + + /** + * RSA公钥加密 + * + * @param str + * 加密字符串 + * 公钥 + * @return 密文 + * @throws Exception + * 加密过程中的异常信息 + */ + public String encrypt( String str ) throws Exception{ + //base64编码的公钥 + byte[] decoded = Base64.decodeBase64(abstractKeyPair.getPublicKey()); + RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); + //RSA加密 + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.ENCRYPT_MODE, pubKey); + String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8"))); + return outStr; + } + + /** + * RSA私钥解密 + * + * @param str + * 加密字符串 + * @return 铭文 + * @throws Exception + * 解密过程中的异常信息 + */ + public String decrypt(String str) { + try { + //64位解码加密后的字符串 + byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8")); + //base64编码的私钥 + byte[] decoded = Base64.decodeBase64(abstractKeyPair.getPrivateKey()); + RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded)); + //RSA解密 + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.DECRYPT_MODE, priKey); + String outStr = new String(cipher.doFinal(inputByte)); + return outStr; + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + public static void main(String[] args) throws Exception { + // String presentedPassword = "Qw123456@"; + // // 这里是传入校验的密码 + // System.out.println(encrypt(presentedPassword)); + // PasswordEncoder encode = new BCryptPasswordEncoder(); + // String bc = encode.encode(presentedPassword); + // // 这里是数据库里面的密码 + // System.out.println("数据库里面保存的密码.........."); + // System.out.println(bc); + } +} diff --git a/src/main/java/org/layuiframework/stereotype/util/SpringContextUtil.java b/src/main/resources/templates/stereotype/util/spring.java.ftl similarity index 82% rename from src/main/java/org/layuiframework/stereotype/util/SpringContextUtil.java rename to src/main/resources/templates/stereotype/util/spring.java.ftl index 3f41ebbe414cc866206da6f378b208bc66b61eca..23913fc9ede4a06098fa45beff687453329d9c27 100644 --- a/src/main/java/org/layuiframework/stereotype/util/SpringContextUtil.java +++ b/src/main/resources/templates/stereotype/util/spring.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.util; +package ${package.Stereotype}.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; @@ -23,11 +23,12 @@ public class SpringContextUtil implements ApplicationContextAware { return getApplicationContext().getBean(name); } - public static T getBean(Class clazz) { + <#assign T = 'T' /> + public static <${T}> T getBean(Class<${T}> clazz) { return getApplicationContext().getBean(clazz); } - public static T getBean(String name, Class clazz) { + public static <${T}> T getBean(String name, Class<${T}> clazz) { return getApplicationContext().getBean(name, clazz); } diff --git a/src/main/java/org/layuiframework/stereotype/util/ValidGroup.java b/src/main/resources/templates/stereotype/util/valid.java.ftl similarity index 58% rename from src/main/java/org/layuiframework/stereotype/util/ValidGroup.java rename to src/main/resources/templates/stereotype/util/valid.java.ftl index 0225670835d497269b650880c2d793eae760cf51..6eba5770d822bce7ae6af2d741f4efacc0a8dc6b 100644 --- a/src/main/java/org/layuiframework/stereotype/util/ValidGroup.java +++ b/src/main/resources/templates/stereotype/util/valid.java.ftl @@ -1,7 +1,18 @@ -package org.layuiframework.stereotype.util; +package ${package.Stereotype}.util; import javax.validation.GroupSequence; +/** + * 分组校验对象 + * @Valid与@Validated + * @desc + * 都是做数据校验的,只不过注解位置与用法有点不同。 + * 区别: + * - 后者是spring实现 + * - 前者不能注解在类上,后者不能注解在成员属性上 + * - 后者比起前者提供了分组校验的功能 + * 这里使用的是后者 + */ public class ValidGroup { // 新增使用(配合spring的@Validated功能分组使用) @@ -16,7 +27,7 @@ public class ValidGroup { // 删除使用(配合spring的@Validated功能分组使用) public interface Delete{} - // 查询单个记录 + // 查询单个记录(配合spring的@Validated功能分组使用) public interface SelectOne{} @GroupSequence({Insert.class, Update.class,UpdateState.class,Delete.class, SelectOne.class}) diff --git a/src/main/java/org/layuiframework/stereotype/vo/AppCode.java b/src/main/resources/templates/stereotype/vo/appcode.java.ftl similarity index 85% rename from src/main/java/org/layuiframework/stereotype/vo/AppCode.java rename to src/main/resources/templates/stereotype/vo/appcode.java.ftl index d6f49e6ccf0c3869370313025d263c3fa7570405..f1d71201292f759a0cbbe49744de1aa40e9ce3e1 100644 --- a/src/main/java/org/layuiframework/stereotype/vo/AppCode.java +++ b/src/main/resources/templates/stereotype/vo/appcode.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.vo; +package ${package.Stereotype}.vo; import lombok.Getter; diff --git a/src/main/java/org/layuiframework/stereotype/vo/LayuiTableVo.java b/src/main/resources/templates/stereotype/vo/layui.java.ftl similarity index 91% rename from src/main/java/org/layuiframework/stereotype/vo/LayuiTableVo.java rename to src/main/resources/templates/stereotype/vo/layui.java.ftl index 916a22ec1dac5a7440d2591c4835e15960e6955b..2c284613d8fe3bdd9da06a17ad45541570948b8c 100644 --- a/src/main/java/org/layuiframework/stereotype/vo/LayuiTableVo.java +++ b/src/main/resources/templates/stereotype/vo/layui.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.vo; +package ${package.Stereotype}.vo; import com.github.pagehelper.PageInfo; import lombok.Data; diff --git a/src/main/java/org/layuiframework/stereotype/vo/PageInfo.java b/src/main/resources/templates/stereotype/vo/pageinfo.java.ftl similarity index 75% rename from src/main/java/org/layuiframework/stereotype/vo/PageInfo.java rename to src/main/resources/templates/stereotype/vo/pageinfo.java.ftl index 10e93b0a83372015ee86680c12e1d8b803f96f1e..dd23b62f2ecf3915b23b44220815b20f51548b75 100644 --- a/src/main/java/org/layuiframework/stereotype/vo/PageInfo.java +++ b/src/main/resources/templates/stereotype/vo/pageinfo.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.vo; +package ${package.Stereotype}.vo; import lombok.Data; diff --git a/src/main/java/org/layuiframework/stereotype/vo/ResultCode.java b/src/main/resources/templates/stereotype/vo/resultcode.java.ftl similarity index 91% rename from src/main/java/org/layuiframework/stereotype/vo/ResultCode.java rename to src/main/resources/templates/stereotype/vo/resultcode.java.ftl index df4465e4ba086f7658f1c81e09534c5b25d2fbe6..9f89436dfd4e52931dd563c82183f91282da3823 100644 --- a/src/main/java/org/layuiframework/stereotype/vo/ResultCode.java +++ b/src/main/resources/templates/stereotype/vo/resultcode.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.vo; +package ${package.Stereotype}.vo; import lombok.Getter; diff --git a/src/main/java/org/layuiframework/stereotype/vo/ResultVo.java b/src/main/resources/templates/stereotype/vo/resultvo.java.ftl similarity index 95% rename from src/main/java/org/layuiframework/stereotype/vo/ResultVo.java rename to src/main/resources/templates/stereotype/vo/resultvo.java.ftl index 5824d30370f383f890d56b9fc517139bc6b7e48a..f611089818c56cc13731341b3ae41e46b85031c3 100644 --- a/src/main/java/org/layuiframework/stereotype/vo/ResultVo.java +++ b/src/main/resources/templates/stereotype/vo/resultvo.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.vo; +package ${package.Stereotype}.vo; import lombok.Data; diff --git a/src/main/java/org/layuiframework/stereotype/vo/StatusCode.java b/src/main/resources/templates/stereotype/vo/statuscode.java.ftl similarity index 61% rename from src/main/java/org/layuiframework/stereotype/vo/StatusCode.java rename to src/main/resources/templates/stereotype/vo/statuscode.java.ftl index 798bd6cb948a94aabdae318e657dd88be38a40a9..a4947247ad75a3db9811870dcdec18c1506c471c 100644 --- a/src/main/java/org/layuiframework/stereotype/vo/StatusCode.java +++ b/src/main/resources/templates/stereotype/vo/statuscode.java.ftl @@ -1,4 +1,4 @@ -package org.layuiframework.stereotype.vo; +package ${package.Stereotype}.vo; interface StatusCode { int getCode(); diff --git a/src/main/resources/templates/vo.java.ftl b/src/main/resources/templates/vo.java.ftl index 9922c2f50578c6feed820de096f51c77cdc6ff2f..baa580c019a8310b24ac754ce8515171b7d534e5 100644 --- a/src/main/resources/templates/vo.java.ftl +++ b/src/main/resources/templates/vo.java.ftl @@ -3,7 +3,7 @@ package ${package.Vo}; <#if entityLombokModel> import lombok.Data; -import org.layuiframework.stereotype.vo.PageInfo; +import ${package.Stereotype}.vo.PageInfo; import com.fasterxml.jackson.annotation.JsonFormat; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date;