├── .gitignore ├── .gitlab-ci.yml ├── .travis.yml ├── Dockerfile ├── LICENSE ├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── javabaas │ │ └── server │ │ ├── Main.java │ │ ├── admin │ │ ├── controller │ │ │ ├── ApiStatController.java │ │ │ ├── AppController.java │ │ │ ├── ClazzController.java │ │ │ └── FieldController.java │ │ ├── entity │ │ │ ├── ApiMethod.java │ │ │ ├── ApiStat.java │ │ │ ├── App.java │ │ │ ├── Clazz.java │ │ │ ├── ClazzAcl.java │ │ │ ├── ClazzAclMethod.java │ │ │ ├── ClientPlatform.java │ │ │ ├── Field.java │ │ │ ├── FieldType.java │ │ │ ├── InternalFields.java │ │ │ └── dto │ │ │ │ ├── AppDto.java │ │ │ │ ├── AppExport.java │ │ │ │ ├── ClazzExport.java │ │ │ │ └── FieldExport.java │ │ ├── repository │ │ │ ├── AppRepository.java │ │ │ ├── ClazzRepository.java │ │ │ └── FieldRepository.java │ │ ├── service │ │ │ ├── AppService.java │ │ │ ├── ClazzService.java │ │ │ ├── FieldService.java │ │ │ └── StatService.java │ │ └── util │ │ │ └── swagger │ │ │ └── ParameterUtil.java │ │ ├── cloud │ │ ├── controller │ │ │ ├── CloudController.java │ │ │ └── CloudMasterController.java │ │ ├── entity │ │ │ ├── CloudRequest.java │ │ │ ├── CloudResponse.java │ │ │ ├── CloudResponseCode.java │ │ │ ├── CloudSetting.java │ │ │ ├── HookSetting.java │ │ │ ├── JBRequest.java │ │ │ └── JBResponse.java │ │ ├── service │ │ │ └── CloudService.java │ │ └── util │ │ │ └── SignUtil.java │ │ ├── common │ │ ├── controller │ │ │ ├── EmptyController.java │ │ │ ├── ErrorController.java │ │ │ ├── StatusController.java │ │ │ └── SwaggerController.java │ │ ├── entity │ │ │ ├── SimpleCode.java │ │ │ ├── SimpleError.java │ │ │ └── SimpleResult.java │ │ ├── filter │ │ │ └── SimpleCORSFilter.java │ │ ├── interceptor │ │ │ ├── AdminInterceptor.java │ │ │ ├── AuthInterceptor.java │ │ │ ├── HeaderInterceptor.java │ │ │ └── MasterInterceptor.java │ │ ├── listener │ │ │ └── ApplicationEventListener.java │ │ ├── service │ │ │ ├── MasterService.java │ │ │ └── TimeService.java │ │ ├── sign │ │ │ ├── AuthChecker.java │ │ │ ├── ReplyChecker.java │ │ │ └── SignUtil.java │ │ └── util │ │ │ └── JSONUtil.java │ │ ├── config │ │ ├── AuthConfig.java │ │ ├── BaasConfig.java │ │ ├── controller │ │ │ └── AppConfigController.java │ │ ├── entity │ │ │ ├── AppConfig.java │ │ │ ├── AppConfigEnum.java │ │ │ └── AppConfigs.java │ │ ├── repository │ │ │ └── AppConfigRepository.java │ │ └── service │ │ │ └── AppConfigService.java │ │ ├── file │ │ ├── controller │ │ │ └── FileController.java │ │ ├── entity │ │ │ ├── BaasFile.java │ │ │ ├── FileStoragePlatform.java │ │ │ └── qiniu │ │ │ │ ├── PersistentItem.java │ │ │ │ └── PersistentResult.java │ │ ├── handler │ │ │ ├── IFileHandler.java │ │ │ └── impl │ │ │ │ ├── QiniuFileHandler.java │ │ │ │ └── TestFileHandler.java │ │ └── service │ │ │ └── FileService.java │ │ ├── hook │ │ ├── entity │ │ │ ├── HookEvent.java │ │ │ ├── HookRequest.java │ │ │ ├── HookResponse.java │ │ │ └── HookResponseCode.java │ │ └── service │ │ │ └── HookService.java │ │ ├── log │ │ ├── appender │ │ │ └── LogbackMongoAppender.java │ │ ├── controller │ │ │ └── LogController.java │ │ ├── entity │ │ │ └── BaasLog.java │ │ └── service │ │ │ ├── LogService.java │ │ │ └── MongoConnection.java │ │ ├── object │ │ ├── controller │ │ │ └── ObjectController.java │ │ ├── dao │ │ │ ├── IDao.java │ │ │ └── impl │ │ │ │ └── mongo │ │ │ │ ├── MongoDBConfiguration.java │ │ │ │ ├── MongoDao.java │ │ │ │ └── MongoProperties.java │ │ ├── entity │ │ │ ├── BaasAcl.java │ │ │ ├── BaasInclude.java │ │ │ ├── BaasList.java │ │ │ ├── BaasObject.java │ │ │ ├── BaasOperator.java │ │ │ ├── BaasOperatorEnum.java │ │ │ ├── BaasQuery.java │ │ │ ├── BaasSort.java │ │ │ ├── ClassIds.java │ │ │ ├── IndexType.java │ │ │ └── error │ │ │ │ ├── DuplicateKeyError.java │ │ │ │ ├── FieldRequiredError.java │ │ │ │ ├── FieldTypeError.java │ │ │ │ ├── ObjectInsertError.java │ │ │ │ ├── OperatorWrongTypeError.java │ │ │ │ └── OperatorWrongValueError.java │ │ ├── service │ │ │ ├── AclHandler.java │ │ │ ├── ClazzAclChecker.java │ │ │ ├── ObjectChecker.java │ │ │ ├── ObjectExtractor.java │ │ │ └── ObjectService.java │ │ └── util │ │ │ ├── BaasObjectIdUtil.java │ │ │ └── BaasOperatorUtil.java │ │ ├── push │ │ ├── controller │ │ │ └── PushController.java │ │ ├── entity │ │ │ ├── Push.java │ │ │ ├── PushLog.java │ │ │ ├── PushMessage.java │ │ │ └── PushNotification.java │ │ ├── handler │ │ │ ├── IPushHandler.java │ │ │ └── impl │ │ │ │ ├── JPushHandler.java │ │ │ │ └── TestPushHandler.java │ │ └── service │ │ │ └── PushService.java │ │ ├── role │ │ ├── controller │ │ │ └── RoleController.java │ │ ├── entity │ │ │ └── BaasRole.java │ │ └── service │ │ │ └── RoleService.java │ │ ├── sms │ │ ├── controller │ │ │ └── SmsController.java │ │ ├── entity │ │ │ ├── SmsLog.java │ │ │ └── SmsSendState.java │ │ ├── handler │ │ │ ├── ISmsHandler.java │ │ │ └── impl │ │ │ │ ├── LocalSmsSendHandler.java │ │ │ │ ├── MockSmsHandler.java │ │ │ │ └── aliyun │ │ │ │ ├── AliyunSmsHandler.java │ │ │ │ ├── SendSmsRequest.java │ │ │ │ ├── SendSmsResponse.java │ │ │ │ └── SendSmsResponseUnmarshaller.java │ │ └── service │ │ │ ├── SmsRateLimiter.java │ │ │ └── SmsService.java │ │ └── user │ │ ├── controller │ │ ├── InstallationController.java │ │ └── UserController.java │ │ ├── entity │ │ ├── BaasAuth.java │ │ ├── BaasInstallation.java │ │ ├── BaasPhoneRegister.java │ │ ├── BaasSnsRegister.java │ │ ├── BaasSnsType.java │ │ └── BaasUser.java │ │ ├── service │ │ ├── InstallationService.java │ │ └── UserService.java │ │ └── util │ │ ├── AesCbcUtil.java │ │ ├── SnsAuthUtil.java │ │ └── UUID.java └── resources │ ├── application.properties │ ├── banner.txt │ ├── favicon.ico │ └── static │ ├── css │ ├── element.css │ ├── print.css │ ├── reset.css │ ├── screen.css │ ├── style.css │ └── typography.css │ ├── explorer.html │ ├── fonts │ ├── DroidSans-Bold.ttf │ ├── DroidSans.ttf │ └── element-icons.woff │ ├── images │ ├── collapse.gif │ ├── expand.gif │ ├── explorer_icons.png │ ├── logo.png │ ├── pet_store_api.png │ ├── throbber.gif │ └── wordnik_api.png │ ├── lang │ ├── translator.js │ └── zh-cn.js │ ├── lib │ ├── backbone-min.js │ ├── element.js │ ├── es5-shim.js │ ├── handlebars-4.0.5.js │ ├── highlight.9.1.0.pack.js │ ├── highlight.9.1.0.pack_extended.js │ ├── jquery-1.8.0.min.js │ ├── jquery.ba-bbq.min.js │ ├── jquery.slideto.min.js │ ├── jquery.wiggle.min.js │ ├── js-yaml.min.js │ ├── jsoneditor.min.js │ ├── lodash.min.js │ ├── marked.js │ ├── object-assign-pollyfill.js │ ├── sanitize-html.min.js │ ├── swagger-oauth.js │ ├── vue-resource.min.js │ └── vue.min.js │ ├── o2c.html │ ├── swagger-ui.js │ └── swagger-ui.min.js └── test └── java └── com └── javabaas └── server ├── AclTests.java ├── AppConfigTests.java ├── AppTests.java ├── AuthTests.java ├── ClazzTests.java ├── CloudTests.java ├── FieldTests.java ├── FileTests.java ├── HookTests.java ├── ObjectTests.java ├── PushTests.java ├── RoleTests.java ├── SmsTests.java ├── UserTests.java ├── customer └── CustomerController.java └── util └── MockClient.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | 14 | # Build results 15 | target 16 | 17 | # IDEA files 18 | .idea 19 | *.iml 20 | 21 | # Eclipse files 22 | .settings 23 | .classpath 24 | .project 25 | 26 | test.properties 27 | src/main/.DS_Store 28 | -------------------------------------------------------------------------------- /.gitlab-ci.yml: -------------------------------------------------------------------------------- 1 | image: docker 2 | services: 3 | - mongo 4 | - redis 5 | 6 | stages: 7 | - test 8 | - build 9 | - docker 10 | - deploy 11 | 12 | test: 13 | stage: test 14 | image: maven:alpine 15 | script: 16 | - export spring_data_mongodb_host=mongo 17 | - export spring_redis_host=redis 18 | - mvn test 19 | 20 | build: 21 | stage: build 22 | image: maven:alpine 23 | script: 24 | - mvn package -DskipTests 25 | artifacts: 26 | paths: 27 | - target 28 | 29 | docker: 30 | stage: docker 31 | image: docker 32 | script: 33 | - DATE=`date +%Y%m%d` 34 | - TAG=$DATE-$CI_JOB_ID 35 | - docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} ${DOCKER_REGISTER} 36 | - docker build -t ${DOCKER_REGISTER}/${PROJECT_NAME}:$TAG . 37 | - docker push ${DOCKER_REGISTER}/${PROJECT_NAME}:$TAG 38 | - docker tag ${DOCKER_REGISTER}/${PROJECT_NAME}:$TAG ${DOCKER_REGISTER}/${PROJECT_NAME}:latest 39 | - docker push ${DOCKER_REGISTER}/${PROJECT_NAME}:latest 40 | 41 | #deploy: 42 | # stage: deploy 43 | # image: maven:alpine 44 | # script: 45 | # - curl -u ${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY} -X POST -H 'Accept:application/json' -H 'Content-Type:application/json' -d '{"inServiceStrategy":{"launchConfig":{"environment":{"spring_data_mongodb_host":"mongo","spring_redis_host":"redis"},"imageUuid":"docker:kerust.net:8088/javabaas","kind":"container","labels":{"io.rancher.container.pull_image":"always"},"networkMode":"managed","ports":["9000:8080/tcp"],"startOnCreate":true,"stdinOpen":true,"tty":true,"vcpu":1}}}' 'http://rancher.kerust.net:10084/v2-beta/projects/1a259/services/1s108/?action=upgrade' 46 | # environment: 47 | # name: JBDevelop 48 | # url: http://kerust.net:9000 -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 4 | services: 5 | - mongodb 6 | - redis-server -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM java:8-alpine 2 | 3 | WORKDIR / 4 | RUN apk update \ 5 | && apk add curl \ 6 | && apk add tzdata 7 | ADD target/JavaBaas-boot.jar target/JavaBaas-boot.jar 8 | ENV TZ Asia/Shanghai 9 | 10 | HEALTHCHECK --interval=10s --timeout=5s --retries=3 \ 11 | CMD curl -f http://localhost:8080/ || exit 1 12 | 13 | EXPOSE 8080 14 | CMD java -jar target/JavaBaas-boot.jar -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.javabaas 7 | JavaBaas 8 | 2.1.0-SNAPSHOT 9 | jar 10 | 11 | JavaBaas 12 | JavaBaas 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.0.1.RELEASE 18 | 19 | 20 | 21 | UTF-8 22 | com.javabaas.server.Main 23 | 1.8 24 | javabaas 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-mongodb 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-actuator 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-data-redis 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-web 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-configuration-processor 47 | true 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-starter-test 52 | test 53 | 54 | 55 | org.bouncycastle 56 | bcprov-jdk15 57 | 1.46 58 | 59 | 60 | io.swagger 61 | swagger-core 62 | 1.5.16 63 | 64 | 65 | 66 | com.aliyun 67 | aliyun-java-sdk-core 68 | 3.2.4 69 | 70 | 71 | 72 | com.qiniu 73 | qiniu-java-sdk 74 | 7.2.15 75 | 76 | 77 | 78 | cn.jpush.api 79 | jpush-client 80 | 3.2.20 81 | 82 | 83 | org.slf4j 84 | slf4j-log4j12 85 | 86 | 87 | log4j 88 | log4j 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | org.springframework.boot 98 | spring-boot-maven-plugin 99 | 1.2.6.RELEASE 100 | 101 | boot 102 | JavaBaas 103 | 104 | 105 | 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/Main.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server; 2 | 3 | import com.fasterxml.jackson.annotation.JsonInclude; 4 | import com.fasterxml.jackson.databind.DeserializationFeature; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import com.fasterxml.jackson.databind.module.SimpleModule; 7 | import com.javabaas.server.common.interceptor.AdminInterceptor; 8 | import com.javabaas.server.common.interceptor.AuthInterceptor; 9 | import com.javabaas.server.common.interceptor.HeaderInterceptor; 10 | import com.javabaas.server.common.interceptor.MasterInterceptor; 11 | import com.javabaas.server.object.entity.BaasList; 12 | import com.javabaas.server.object.entity.BaasObject; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.boot.SpringApplication; 15 | import org.springframework.boot.autoconfigure.SpringBootApplication; 16 | import org.springframework.context.annotation.Bean; 17 | import org.springframework.context.annotation.Primary; 18 | import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; 19 | import org.springframework.web.client.RestTemplate; 20 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 21 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 22 | 23 | import java.util.List; 24 | import java.util.Map; 25 | 26 | @SpringBootApplication 27 | public class Main implements WebMvcConfigurer { 28 | 29 | @Autowired 30 | private HeaderInterceptor contentTypeInterceptor; 31 | @Autowired 32 | private AuthInterceptor authInterceptor; 33 | @Autowired 34 | private AdminInterceptor adminInterceptor; 35 | @Autowired 36 | private MasterInterceptor masterInterceptor; 37 | 38 | public static void main(String[] args) { 39 | SpringApplication.run(Main.class, args); 40 | } 41 | 42 | @Override 43 | public void addInterceptors(InterceptorRegistry registry) { 44 | //请求头拦截器,检查Content-Type、platform等 45 | registry.addInterceptor(contentTypeInterceptor).addPathPatterns("/api/**").excludePathPatterns("/api/file/callback", 46 | "/api/file/notify/**", "/customer/**"); 47 | //授权拦截器 48 | registry.addInterceptor(authInterceptor).addPathPatterns("/api/**").excludePathPatterns("/api/file/callback", 49 | "/api/file/notify/**", "/api/admin/**", "/customer/**"); 50 | //超级权限 51 | registry.addInterceptor(adminInterceptor).addPathPatterns("/api/admin/**"); 52 | //管理权限 53 | registry.addInterceptor(masterInterceptor).addPathPatterns("/api/master/**"); 54 | } 55 | 56 | @Bean 57 | public RestTemplate getRest() { 58 | return new RestTemplate(); 59 | } 60 | 61 | @Bean(name = "baasMapper") 62 | public ObjectMapper baasMapper() { 63 | ObjectMapper mapper = new ObjectMapper(); 64 | mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); 65 | SimpleModule module = new SimpleModule(); 66 | module.addAbstractTypeMapping(Map.class, BaasObject.class); 67 | module.addAbstractTypeMapping(List.class, BaasList.class); 68 | return mapper.registerModule(module); 69 | } 70 | 71 | @Bean(name = "objectMapper") 72 | @Primary 73 | public ObjectMapper objectMapper() { 74 | ObjectMapper mapper = new ObjectMapper(); 75 | mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 76 | mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); 77 | return mapper; 78 | } 79 | 80 | /** 81 | * 修改默认json适配器 82 | */ 83 | @Bean 84 | public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { 85 | MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); 86 | ObjectMapper objectMapper = new ObjectMapper(); 87 | objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 88 | objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); 89 | jsonConverter.setObjectMapper(objectMapper); 90 | return jsonConverter; 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/controller/ApiStatController.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.controller; 2 | 3 | import com.javabaas.server.admin.entity.ApiMethod; 4 | import com.javabaas.server.admin.entity.ClientPlatform; 5 | import com.javabaas.server.admin.service.StatService; 6 | import com.javabaas.server.common.entity.SimpleResult; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.web.bind.annotation.*; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * API统计 14 | * Created by Codi on 15/10/20. 15 | */ 16 | @RestController 17 | @RequestMapping(value = "/api/master/apiStat") 18 | public class ApiStatController { 19 | 20 | @Autowired 21 | private StatService statService; 22 | 23 | @RequestMapping(value = "", method = RequestMethod.GET) 24 | @ResponseBody 25 | public SimpleResult getStat(@RequestHeader(value = "JB-AppId") String appId, 26 | @RequestParam(required = false) String method, 27 | @RequestParam(required = false) String clazz, 28 | @RequestParam(required = false) String plat, 29 | @RequestParam String from, 30 | @RequestParam String to) { 31 | ApiMethod apiMethod = ApiMethod.get(method); 32 | ClientPlatform clientPlatform = ClientPlatform.get(plat); 33 | List list = statService.get(appId, apiMethod, clazz, clientPlatform, from, to); 34 | SimpleResult result = SimpleResult.success(); 35 | result.putData("result", list); 36 | return result; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/controller/AppController.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.controller; 2 | 3 | import com.javabaas.server.admin.entity.App; 4 | import com.javabaas.server.admin.entity.dto.AppDto; 5 | import com.javabaas.server.admin.entity.dto.AppExport; 6 | import com.javabaas.server.admin.service.AppService; 7 | import com.javabaas.server.admin.service.StatService; 8 | import com.javabaas.server.common.entity.SimpleResult; 9 | import com.javabaas.server.object.service.ObjectService; 10 | import com.javabaas.server.user.service.UserService; 11 | import org.springframework.beans.BeanUtils; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.web.bind.annotation.*; 14 | 15 | import java.util.LinkedList; 16 | import java.util.List; 17 | 18 | /** 19 | * 应用控制器 20 | * Created by Staryet on 15/9/17. 21 | */ 22 | @RestController 23 | @RequestMapping(value = "/api/admin/app") 24 | public class AppController { 25 | 26 | @Autowired 27 | private AppService appService; 28 | @Autowired 29 | private StatService statService; 30 | @Autowired 31 | private ObjectService objectService; 32 | 33 | @RequestMapping(value = "", method = RequestMethod.POST) 34 | public SimpleResult insert(@RequestBody App app) { 35 | App newApp = appService.insert(app); 36 | SimpleResult result = SimpleResult.success(); 37 | result.putData("result", newApp); 38 | return result; 39 | } 40 | 41 | @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) 42 | @ResponseBody 43 | public SimpleResult delete(@PathVariable String id) { 44 | appService.delete(id); 45 | return SimpleResult.success(); 46 | } 47 | 48 | @RequestMapping(value = "/{id}", method = RequestMethod.GET) 49 | @ResponseBody 50 | public SimpleResult get(@PathVariable String id) { 51 | App app = appService.get(id); 52 | SimpleResult result = SimpleResult.success(); 53 | result.putData("result", app); 54 | return result; 55 | } 56 | 57 | @RequestMapping(value = "", method = RequestMethod.GET) 58 | @ResponseBody 59 | public SimpleResult list() { 60 | List apps = appService.list(); 61 | LinkedList appDTOs = new LinkedList<>(); 62 | apps.forEach(app -> { 63 | AppDto dto = new AppDto(); 64 | BeanUtils.copyProperties(app, dto); 65 | appDTOs.add(dto); 66 | //计算用户总数 67 | dto.setUserCount(objectService.count(app.getId(), UserService.USER_CLASS_NAME, null, null, true)); 68 | //请求数总和 69 | dto.setYesterday(statService.getYesterdayApiCount(app.getId())); 70 | dto.setCurrentMonth(statService.getCurrentMonthApiCount(app.getId())); 71 | }); 72 | SimpleResult result = SimpleResult.success(); 73 | result.putData("result", appDTOs); 74 | return result; 75 | } 76 | 77 | @RequestMapping(value = "/{id}/resetKey", method = RequestMethod.PUT) 78 | @ResponseBody 79 | public SimpleResult resetKey(@PathVariable String id) { 80 | appService.resetKey(id); 81 | return SimpleResult.success(); 82 | } 83 | 84 | @RequestMapping(value = "/{id}/resetMasterKey", method = RequestMethod.PUT) 85 | @ResponseBody 86 | public SimpleResult resetMasterKey(@PathVariable String id) { 87 | appService.resetMasterKey(id); 88 | return SimpleResult.success(); 89 | } 90 | 91 | @RequestMapping(value = "/{id}/export", method = RequestMethod.GET) 92 | @ResponseBody 93 | public SimpleResult export(@PathVariable String id) { 94 | AppExport appExport = appService.export(id); 95 | SimpleResult result = SimpleResult.success(); 96 | result.putData("result", appExport); 97 | return result; 98 | } 99 | 100 | @RequestMapping(value = "/import", method = RequestMethod.POST) 101 | @ResponseBody 102 | public SimpleResult importData(@RequestBody AppExport appExport) { 103 | appService.importData(appExport); 104 | return SimpleResult.success(); 105 | } 106 | 107 | } -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/controller/ClazzController.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.controller; 2 | 3 | import com.javabaas.server.admin.entity.Clazz; 4 | import com.javabaas.server.admin.entity.ClazzAcl; 5 | import com.javabaas.server.admin.entity.dto.ClazzExport; 6 | import com.javabaas.server.admin.service.ClazzService; 7 | import com.javabaas.server.common.entity.SimpleResult; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import java.util.List; 12 | 13 | /** 14 | * 类控制器 15 | * Created by Staryet on 15/6/15. 16 | */ 17 | @RestController 18 | @RequestMapping(value = "/api/master/clazz") 19 | public class ClazzController { 20 | 21 | @Autowired 22 | private ClazzService clazzService; 23 | 24 | @RequestMapping(value = "", method = RequestMethod.POST) 25 | public SimpleResult insert(@RequestHeader(value = "JB-AppId") String appId, @RequestBody Clazz clazz) { 26 | clazzService.insert(appId, clazz); 27 | return SimpleResult.success(); 28 | } 29 | 30 | @RequestMapping(value = "/{name}", method = RequestMethod.DELETE) 31 | @ResponseBody 32 | public SimpleResult delete(@RequestHeader(value = "JB-AppId") String appId, @PathVariable String name) { 33 | clazzService.delete(appId, name); 34 | return SimpleResult.success(); 35 | } 36 | 37 | @RequestMapping(value = "/{name}", method = RequestMethod.PUT) 38 | @ResponseBody 39 | public SimpleResult update(@RequestHeader(value = "JB-AppId") String appId, @PathVariable String name, @RequestBody Clazz clazz) { 40 | clazzService.update(appId, name, clazz); 41 | return SimpleResult.success(); 42 | } 43 | 44 | @RequestMapping(value = "/{name}", method = RequestMethod.GET) 45 | @ResponseBody 46 | public SimpleResult get(@RequestHeader(value = "JB-AppId") String appId, @PathVariable String name) { 47 | Clazz clazz = clazzService.get(appId, name); 48 | SimpleResult result = SimpleResult.success(); 49 | result.putData("result", clazz); 50 | return result; 51 | } 52 | 53 | @RequestMapping(value = "/{name}/export", method = RequestMethod.GET) 54 | @ResponseBody 55 | public SimpleResult export(@RequestHeader(value = "JB-AppId") String appId, @PathVariable String name) { 56 | ClazzExport clazzExport = clazzService.export(appId, name); 57 | SimpleResult result = SimpleResult.success(); 58 | result.putData("result", clazzExport); 59 | return result; 60 | } 61 | 62 | @RequestMapping(value = "/import", method = RequestMethod.POST) 63 | @ResponseBody 64 | public SimpleResult importData(@RequestHeader(value = "JB-AppId") String appId, @RequestBody ClazzExport clazzExport) { 65 | clazzService.importData(appId, clazzExport); 66 | return SimpleResult.success(); 67 | } 68 | 69 | @RequestMapping(value = "", method = RequestMethod.GET) 70 | @ResponseBody 71 | public SimpleResult list(@RequestHeader(value = "JB-AppId") String appId) { 72 | List list = clazzService.list(appId); 73 | SimpleResult result = SimpleResult.success(); 74 | result.putData("result", list); 75 | return result; 76 | } 77 | 78 | @RequestMapping(value = "/{name}/acl", method = RequestMethod.POST) 79 | @ResponseBody 80 | public SimpleResult setAcl(@RequestHeader(value = "JB-AppId") String appId, @PathVariable String name, @RequestBody ClazzAcl acl) { 81 | clazzService.setAcl(appId, name, acl); 82 | return SimpleResult.success(); 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/entity/ApiMethod.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.entity; 2 | 3 | /** 4 | * Created by Staryet on 15/8/17. 5 | */ 6 | public enum ApiMethod { 7 | 8 | INSERT("insert"), 9 | UPDATE("update"), 10 | FIND("find"), 11 | DELETE("delete"); 12 | 13 | private String value; 14 | 15 | ApiMethod(String value) { 16 | this.value = value; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return value; 22 | } 23 | 24 | public static ApiMethod get(String value) { 25 | ApiMethod[] methods = ApiMethod.class.getEnumConstants(); 26 | for (ApiMethod method : methods) { 27 | if (method.value.equals(value)) { 28 | return method; 29 | } 30 | } 31 | return null; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/entity/ApiStat.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.entity; 2 | 3 | import java.text.SimpleDateFormat; 4 | import java.util.Date; 5 | 6 | /** 7 | * Created by Codi on 15/10/20. 8 | */ 9 | public class ApiStat { 10 | 11 | private String appId; 12 | private String plat; 13 | private String clazz; 14 | private ApiMethod method; 15 | private String date; 16 | 17 | public ApiStat(String appId, String plat, String clazz, ApiMethod method, String date) { 18 | this.appId = appId; 19 | this.plat = plat; 20 | this.clazz = clazz; 21 | this.method = method; 22 | this.date = date; 23 | } 24 | 25 | public ApiStat(String appId, String plat, String clazz, ApiMethod method, Date date) { 26 | this.appId = appId; 27 | this.plat = plat; 28 | this.clazz = clazz; 29 | this.method = method; 30 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); 31 | this.date = simpleDateFormat.format(date); 32 | } 33 | 34 | public String getAppId() { 35 | return appId; 36 | } 37 | 38 | public void setAppId(String appId) { 39 | this.appId = appId; 40 | } 41 | 42 | public ApiMethod getMethod() { 43 | return method; 44 | } 45 | 46 | public void setMethod(ApiMethod method) { 47 | this.method = method; 48 | } 49 | 50 | public String getPlat() { 51 | return plat; 52 | } 53 | 54 | public void setPlat(String plat) { 55 | this.plat = plat; 56 | } 57 | 58 | public String getClazz() { 59 | return clazz; 60 | } 61 | 62 | public void setClazz(String clazz) { 63 | this.clazz = clazz; 64 | } 65 | 66 | public String getDate() { 67 | return date; 68 | } 69 | 70 | public void setDate(String date) { 71 | this.date = date; 72 | } 73 | 74 | @Override 75 | public String toString() { 76 | return "stat" + "_" + appId + "_" + plat + "_" + clazz + "_" + method.toString() + "_" + date; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/entity/App.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.entity; 2 | 3 | import com.javabaas.server.cloud.entity.CloudSetting; 4 | import org.springframework.data.mongodb.core.mapping.Document; 5 | 6 | import java.util.Date; 7 | 8 | /** 9 | * Created by Staryet on 15/9/17. 10 | */ 11 | @Document 12 | public class App { 13 | 14 | private String id; 15 | private String name; 16 | private String key; 17 | private String masterKey; 18 | private CloudSetting cloudSetting; 19 | private Date createdTime; 20 | 21 | public String getId() { 22 | return id; 23 | } 24 | 25 | public void setId(String id) { 26 | this.id = id; 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | 37 | public String getKey() { 38 | return key; 39 | } 40 | 41 | public void setKey(String key) { 42 | this.key = key; 43 | } 44 | 45 | public String getMasterKey() { 46 | return masterKey; 47 | } 48 | 49 | public void setMasterKey(String masterKey) { 50 | this.masterKey = masterKey; 51 | } 52 | 53 | public CloudSetting getCloudSetting() { 54 | return cloudSetting; 55 | } 56 | 57 | public void setCloudSetting(CloudSetting cloudSetting) { 58 | this.cloudSetting = cloudSetting; 59 | } 60 | 61 | public Date getCreatedTime() { 62 | return createdTime; 63 | } 64 | 65 | public void setCreatedTime(Date createdTime) { 66 | this.createdTime = createdTime; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/entity/Clazz.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.entity; 2 | 3 | import org.springframework.data.mongodb.core.mapping.DBRef; 4 | import org.springframework.data.mongodb.core.mapping.Document; 5 | 6 | /** 7 | * Created by Staryet on 15/6/5. 8 | */ 9 | @Document 10 | public class Clazz { 11 | 12 | private String id; 13 | @DBRef 14 | private App app; 15 | private String name; 16 | private String description; 17 | private ClazzAcl acl; 18 | private boolean internal; 19 | transient private long count; 20 | 21 | public Clazz() { 22 | } 23 | 24 | public Clazz(String name) { 25 | this.name = name; 26 | } 27 | 28 | public String getId() { 29 | return id; 30 | } 31 | 32 | public void setId(String id) { 33 | this.id = id; 34 | } 35 | 36 | public App getApp() { 37 | return app; 38 | } 39 | 40 | public void setApp(App app) { 41 | this.app = app; 42 | } 43 | 44 | public String getName() { 45 | return name; 46 | } 47 | 48 | public void setName(String name) { 49 | this.name = name; 50 | } 51 | 52 | public String getDescription() { 53 | return description; 54 | } 55 | 56 | public void setDescription(String description) { 57 | this.description = description; 58 | } 59 | 60 | public ClazzAcl getAcl() { 61 | return acl; 62 | } 63 | 64 | public void setAcl(ClazzAcl acl) { 65 | this.acl = acl; 66 | } 67 | 68 | public boolean isInternal() { 69 | return internal; 70 | } 71 | 72 | public void setInternal(boolean internal) { 73 | this.internal = internal; 74 | } 75 | 76 | public long getCount() { 77 | return count; 78 | } 79 | 80 | public void setCount(long count) { 81 | this.count = count; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/entity/ClazzAcl.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.entity; 2 | 3 | import com.javabaas.server.user.entity.BaasUser; 4 | 5 | import java.util.HashMap; 6 | import java.util.LinkedHashMap; 7 | import java.util.Map; 8 | 9 | /** 10 | * Created by Staryet on 15/8/17. 11 | */ 12 | public class ClazzAcl extends LinkedHashMap { 13 | 14 | public void setPublicAccess(ClazzAclMethod method, boolean access) { 15 | Map accessMap = getAccessMap("*"); 16 | accessMap.put(method.toString(), access); 17 | put("*", accessMap); 18 | } 19 | 20 | public void setAccess(ClazzAclMethod method, String userId, boolean access) { 21 | Map accessMap = getAccessMap(userId); 22 | accessMap.put(method.toString(), access); 23 | put(userId, accessMap); 24 | } 25 | 26 | public boolean hasAccess(ClazzAclMethod method, BaasUser user) { 27 | if (user == null) { 28 | return hasPublicAccess(method); 29 | } else { 30 | return hasPublicAccess(method) || hasAccess(method, user.getId()); 31 | } 32 | } 33 | 34 | public boolean hasPublicAccess(ClazzAclMethod method) { 35 | return hasAccess(method, "*"); 36 | } 37 | 38 | public boolean hasAccess(ClazzAclMethod method, String name) { 39 | Map map = getAccessMap(name); 40 | Boolean write = map.get(method.toString()); 41 | if (write == null) { 42 | return false; 43 | } else { 44 | return write; 45 | } 46 | } 47 | 48 | private Map getAccessMap(String name) { 49 | Map accessMap = (Map) get(name); 50 | if (accessMap == null) { 51 | accessMap = new HashMap<>(); 52 | } 53 | return accessMap; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/entity/ClazzAclMethod.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.entity; 2 | 3 | /** 4 | * Created by Staryet on 15/8/17. 5 | */ 6 | public enum ClazzAclMethod { 7 | 8 | INSERT("insert"), 9 | UPDATE("update"), 10 | GET("get"), 11 | FIND("find"), 12 | DELETE("delete"); 13 | 14 | private String value; 15 | 16 | ClazzAclMethod(String value) { 17 | this.value = value; 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return value; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/entity/ClientPlatform.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.entity; 2 | 3 | /** 4 | * Created by Staryet on 15/8/17. 5 | */ 6 | public enum ClientPlatform { 7 | 8 | ANDROID("android"), 9 | IOS("ios"), 10 | JS("js"), 11 | CLOUD("cloud"), 12 | SHELL("shell"), 13 | ADMIN("admin"); 14 | 15 | private String value; 16 | 17 | ClientPlatform(String value) { 18 | this.value = value; 19 | } 20 | 21 | @Override 22 | public String toString() { 23 | return value; 24 | } 25 | 26 | public static ClientPlatform get(String value) { 27 | ClientPlatform[] platforms = ClientPlatform.class.getEnumConstants(); 28 | for (ClientPlatform plat : platforms) { 29 | if (plat.value.equals(value)) { 30 | return plat; 31 | } 32 | } 33 | return null; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/entity/Field.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.entity; 2 | 3 | import org.springframework.data.mongodb.core.mapping.DBRef; 4 | import org.springframework.data.mongodb.core.mapping.Document; 5 | 6 | /** 7 | * Created by Staryet on 15/6/15. 8 | */ 9 | @Document 10 | public class Field { 11 | 12 | private String id; 13 | @DBRef 14 | private Clazz clazz; 15 | private String name; 16 | private int type; 17 | private boolean internal;//系统内建字段 禁止修改删除 18 | private boolean security;//安全字段 必须使用管理权限操作 19 | private boolean notnull;//必填字段 20 | private boolean readonly;//只读字段 21 | private String description;//字段描述 22 | 23 | public Field() { 24 | } 25 | 26 | public Field(int type, String name) { 27 | this.type = type; 28 | this.name = name; 29 | } 30 | 31 | public String getId() { 32 | return id; 33 | } 34 | 35 | public void setId(String id) { 36 | this.id = id; 37 | } 38 | 39 | public Clazz getClazz() { 40 | return clazz; 41 | } 42 | 43 | public void setClazz(Clazz clazz) { 44 | this.clazz = clazz; 45 | } 46 | 47 | public String getName() { 48 | return name; 49 | } 50 | 51 | public void setName(String name) { 52 | this.name = name; 53 | } 54 | 55 | public int getType() { 56 | return type; 57 | } 58 | 59 | public void setType(int type) { 60 | this.type = type; 61 | } 62 | 63 | public boolean isInternal() { 64 | return internal; 65 | } 66 | 67 | public void setInternal(boolean internal) { 68 | this.internal = internal; 69 | } 70 | 71 | public boolean isSecurity() { 72 | return security; 73 | } 74 | 75 | public void setSecurity(boolean security) { 76 | this.security = security; 77 | } 78 | 79 | public boolean isNotnull() { 80 | return notnull; 81 | } 82 | 83 | public void setNotnull(boolean notnull) { 84 | this.notnull = notnull; 85 | } 86 | 87 | public boolean isReadonly() { 88 | return readonly; 89 | } 90 | 91 | public void setReadonly(boolean readonly) { 92 | this.readonly = readonly; 93 | } 94 | 95 | public String getDescription() { 96 | return description; 97 | } 98 | 99 | public void setDescription(String description) { 100 | this.description = description; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/entity/FieldType.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.entity; 2 | 3 | /** 4 | * Created by Staryet on 15/6/17. 5 | */ 6 | public class FieldType { 7 | 8 | public static final int STRING = 1; 9 | public static final int NUMBER = 2; 10 | public static final int BOOLEAN = 3; 11 | public static final int DATE = 4; 12 | public static final int FILE = 5; 13 | public static final int OBJECT = 6; 14 | public static final int ARRAY = 7; 15 | public static final int POINTER = 8; 16 | public static final int GEOPOINT = 9;//TODO 地理坐标 17 | 18 | public static boolean isValid(int type) { 19 | if (type < 1 || type > 9) { 20 | return false; 21 | } else { 22 | return true; 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/entity/InternalFields.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.entity; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by Codi on 2017/9/13. 8 | */ 9 | public class InternalFields { 10 | 11 | public static List fields() { 12 | return Arrays.asList("_id", "createdAt", "updatedAt", "createdPlat", "updatedPlat", "acl"); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/entity/dto/AppDto.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.entity.dto; 2 | 3 | import com.javabaas.server.admin.entity.App; 4 | 5 | /** 6 | * Created by Codi on 16/1/4. 7 | */ 8 | public class AppDto extends App { 9 | 10 | private long userCount; 11 | private long yesterday; 12 | private long currentMonth; 13 | 14 | public long getUserCount() { 15 | return userCount; 16 | } 17 | 18 | public void setUserCount(long userCount) { 19 | this.userCount = userCount; 20 | } 21 | 22 | public long getYesterday() { 23 | return yesterday; 24 | } 25 | 26 | public void setYesterday(long yesterday) { 27 | this.yesterday = yesterday; 28 | } 29 | 30 | public long getCurrentMonth() { 31 | return currentMonth; 32 | } 33 | 34 | public void setCurrentMonth(long currentMonth) { 35 | this.currentMonth = currentMonth; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/entity/dto/AppExport.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.entity.dto; 2 | 3 | import com.javabaas.server.cloud.entity.CloudSetting; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * Created by Codi on 15/11/9. 9 | */ 10 | public class AppExport { 11 | 12 | private String id; 13 | private String name; 14 | private String key; 15 | private String masterKey; 16 | private CloudSetting cloudSetting; 17 | private List clazzs; 18 | 19 | public String getId() { 20 | return id; 21 | } 22 | 23 | public void setId(String id) { 24 | this.id = id; 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | public String getKey() { 36 | return key; 37 | } 38 | 39 | public void setKey(String key) { 40 | this.key = key; 41 | } 42 | 43 | public String getMasterKey() { 44 | return masterKey; 45 | } 46 | 47 | public void setMasterKey(String masterKey) { 48 | this.masterKey = masterKey; 49 | } 50 | 51 | public CloudSetting getCloudSetting() { 52 | return cloudSetting; 53 | } 54 | 55 | public void setCloudSetting(CloudSetting cloudSetting) { 56 | this.cloudSetting = cloudSetting; 57 | } 58 | 59 | public List getClazzs() { 60 | return clazzs; 61 | } 62 | 63 | public void setClazzs(List clazzs) { 64 | this.clazzs = clazzs; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/entity/dto/ClazzExport.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.entity.dto; 2 | 3 | import com.javabaas.server.admin.entity.ClazzAcl; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * Created by Codi on 15/11/9. 9 | */ 10 | public class ClazzExport { 11 | 12 | private String id; 13 | private String name; 14 | private ClazzAcl acl; 15 | private boolean internal; 16 | private List fields; 17 | 18 | public String getId() { 19 | return id; 20 | } 21 | 22 | public void setId(String id) { 23 | this.id = id; 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | 34 | public ClazzAcl getAcl() { 35 | return acl; 36 | } 37 | 38 | public void setAcl(ClazzAcl acl) { 39 | this.acl = acl; 40 | } 41 | 42 | public boolean isInternal() { 43 | return internal; 44 | } 45 | 46 | public void setInternal(boolean internal) { 47 | this.internal = internal; 48 | } 49 | 50 | public List getFields() { 51 | return fields; 52 | } 53 | 54 | public void setFields(List fields) { 55 | this.fields = fields; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/entity/dto/FieldExport.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.entity.dto; 2 | 3 | /** 4 | * Created by Codi on 15/11/9. 5 | */ 6 | public class FieldExport { 7 | 8 | private String id; 9 | private String name; 10 | private int type; 11 | private boolean internal; 12 | private boolean security; 13 | private boolean notnull; 14 | private boolean readonly; 15 | 16 | public String getId() { 17 | return id; 18 | } 19 | 20 | public void setId(String id) { 21 | this.id = id; 22 | } 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public void setName(String name) { 29 | this.name = name; 30 | } 31 | 32 | public int getType() { 33 | return type; 34 | } 35 | 36 | public void setType(int type) { 37 | this.type = type; 38 | } 39 | 40 | public boolean isInternal() { 41 | return internal; 42 | } 43 | 44 | public void setInternal(boolean internal) { 45 | this.internal = internal; 46 | } 47 | 48 | public boolean isSecurity() { 49 | return security; 50 | } 51 | 52 | public void setSecurity(boolean security) { 53 | this.security = security; 54 | } 55 | 56 | public boolean isReadonly() { 57 | return readonly; 58 | } 59 | 60 | public void setReadonly(boolean readonly) { 61 | this.readonly = readonly; 62 | } 63 | 64 | public boolean isNotnull() { 65 | return notnull; 66 | } 67 | 68 | public void setNotnull(boolean notnull) { 69 | this.notnull = notnull; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/repository/AppRepository.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.repository; 2 | 3 | import com.javabaas.server.admin.entity.App; 4 | import org.springframework.data.mongodb.repository.MongoRepository; 5 | 6 | /** 7 | * Created by Staryet on 15/6/5. 8 | */ 9 | public interface AppRepository extends MongoRepository { 10 | 11 | long deleteByName(String name); 12 | 13 | App findByName(String name); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/repository/ClazzRepository.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.repository; 2 | 3 | import com.javabaas.server.admin.entity.Clazz; 4 | import org.springframework.data.domain.Pageable; 5 | import org.springframework.data.mongodb.repository.MongoRepository; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * Created by Staryet on 15/6/5. 11 | */ 12 | public interface ClazzRepository extends MongoRepository { 13 | 14 | long deleteByName(String name); 15 | 16 | Clazz findByAppIdAndName(String appId, String name); 17 | 18 | List findByAppId(String appId, Pageable pageable); 19 | 20 | long deleteByAppId(String appId); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/repository/FieldRepository.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.repository; 2 | 3 | import com.javabaas.server.admin.entity.Clazz; 4 | import com.javabaas.server.admin.entity.Field; 5 | import org.springframework.data.domain.Pageable; 6 | import org.springframework.data.mongodb.repository.MongoRepository; 7 | 8 | import java.util.List; 9 | 10 | /** 11 | * Created by Staryet on 15/6/5. 12 | */ 13 | public interface FieldRepository extends MongoRepository { 14 | 15 | List findByClazzId(String id, Pageable pageable); 16 | 17 | long deleteByClazzAndName(Clazz clazz, String name); 18 | 19 | Field findByClazzAndName(Clazz clazz, String name); 20 | 21 | long deleteByClazz(Clazz clazz); 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/admin/util/swagger/ParameterUtil.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.admin.util.swagger; 2 | 3 | import io.swagger.models.Model; 4 | import io.swagger.models.parameters.BodyParameter; 5 | import io.swagger.models.parameters.Parameter; 6 | import io.swagger.models.parameters.PathParameter; 7 | import io.swagger.models.parameters.QueryParameter; 8 | import io.swagger.models.properties.BooleanProperty; 9 | import io.swagger.models.properties.IntegerProperty; 10 | import io.swagger.models.properties.StringProperty; 11 | 12 | /** 13 | * Created by Codi on 2017/10/11. 14 | */ 15 | public class ParameterUtil { 16 | 17 | public static Parameter id() { 18 | return new PathParameter().name("id").type(StringProperty.TYPE).required(true); 19 | } 20 | 21 | public static Parameter where() { 22 | return new QueryParameter().name("where").type(StringProperty.TYPE); 23 | } 24 | 25 | public static Parameter fetch() { 26 | return new QueryParameter().name("fetch").type(BooleanProperty.TYPE); 27 | } 28 | 29 | public static Parameter include() { 30 | return new QueryParameter().name("include").type(StringProperty.TYPE); 31 | } 32 | 33 | public static Parameter keys() { 34 | return new QueryParameter().name("keys").type(StringProperty.TYPE); 35 | } 36 | 37 | public static Parameter order() { 38 | return new QueryParameter().name("order").type(StringProperty.TYPE); 39 | } 40 | 41 | public static Parameter limit() { 42 | return new QueryParameter().name("limit").type(IntegerProperty.TYPE); 43 | } 44 | 45 | public static Parameter skip() { 46 | return new QueryParameter().name("skip").type(IntegerProperty.TYPE); 47 | } 48 | 49 | public static Parameter body(Model scheme) { 50 | BodyParameter body = new BodyParameter(); 51 | body.setRequired(true); 52 | body.setName("body"); 53 | body.setSchema(scheme); 54 | return body; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/cloud/controller/CloudController.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.cloud.controller; 2 | 3 | import com.javabaas.server.cloud.service.CloudService; 4 | import com.javabaas.server.common.entity.SimpleResult; 5 | import com.javabaas.server.common.sign.AuthChecker; 6 | import com.javabaas.server.user.entity.BaasUser; 7 | import com.javabaas.server.user.service.UserService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import javax.servlet.http.HttpServletRequest; 12 | import java.util.HashMap; 13 | import java.util.Map; 14 | import java.util.Set; 15 | 16 | /** 17 | * 云方法 18 | */ 19 | @RestController 20 | @RequestMapping(value = "/api/cloud") 21 | public class CloudController { 22 | 23 | @Autowired 24 | private CloudService cloudService; 25 | @Autowired 26 | private AuthChecker authChecker; 27 | @Autowired 28 | private UserService userService; 29 | @Autowired 30 | private HttpServletRequest request; 31 | 32 | @RequestMapping(value = "/{name}") 33 | @ResponseBody 34 | public SimpleResult cloud(@RequestHeader(value = "JB-AppId") String appId, 35 | @RequestHeader(value = "JB-Plat") String plat, 36 | @PathVariable String name, 37 | @RequestBody(required = false) String body) { 38 | //获取登录用户 39 | boolean isMaster = authChecker.isMaster(request); 40 | BaasUser currentUser = userService.getCurrentUser(appId, plat, request); 41 | //整理请求参数 42 | Map requestParams = new HashMap<>(); 43 | Set keys = request.getParameterMap().keySet(); 44 | for (String key : keys) { 45 | requestParams.put(key, request.getParameter(key)); 46 | } 47 | return cloudService.cloud(appId, plat, name, currentUser, isMaster, requestParams, body); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/cloud/controller/CloudMasterController.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.cloud.controller; 2 | 3 | import com.javabaas.server.cloud.entity.CloudSetting; 4 | import com.javabaas.server.cloud.service.CloudService; 5 | import com.javabaas.server.common.entity.SimpleError; 6 | import com.javabaas.server.common.entity.SimpleResult; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.web.bind.annotation.*; 9 | 10 | /** 11 | * Created by Staryet on 15/9/22. 12 | */ 13 | @RestController 14 | @RequestMapping(value = "/api/master/cloud") 15 | public class CloudMasterController { 16 | 17 | @Autowired 18 | private CloudService cloudService; 19 | 20 | /** 21 | * 部署云代码相关配置 22 | * 23 | * @param appId 应用id 24 | * @param setting 云代码 配置信息 25 | * @return 结果 26 | * @throws SimpleError 27 | */ 28 | @RequestMapping(value = "", method = RequestMethod.POST) 29 | @ResponseBody 30 | public SimpleResult deploy(@RequestHeader(value = "JB-AppId") String appId, @RequestBody CloudSetting setting) { 31 | cloudService.deploy(appId, setting); 32 | return SimpleResult.success(); 33 | } 34 | 35 | /** 36 | * 删除云代码配置 37 | * 38 | * @param appId 应用id 39 | * @return 结果 40 | * @throws SimpleError 41 | */ 42 | @RequestMapping(value = "", method = RequestMethod.DELETE) 43 | @ResponseBody 44 | public SimpleResult delete(@RequestHeader(value = "JB-AppId") String appId) { 45 | cloudService.unDeploy(appId); 46 | return SimpleResult.success(); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/cloud/entity/CloudRequest.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.cloud.entity; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * Created by Staryet on 15/9/15. 7 | */ 8 | public class CloudRequest extends JBRequest { 9 | 10 | private Map params; 11 | private String body; 12 | 13 | public Map getParams() { 14 | return params; 15 | } 16 | 17 | public void setParams(Map params) { 18 | this.params = params; 19 | } 20 | 21 | public String getBody() { 22 | return body; 23 | } 24 | 25 | public void setBody(String body) { 26 | this.body = body; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/cloud/entity/CloudResponse.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.cloud.entity; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * Created by Staryet on 15/9/15. 7 | */ 8 | public class CloudResponse extends JBResponse { 9 | 10 | public Map data; 11 | 12 | public Map getData() { 13 | return data; 14 | } 15 | 16 | public void setData(Map data) { 17 | this.data = data; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/cloud/entity/CloudResponseCode.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.cloud.entity; 2 | 3 | /** 4 | * Created by Codi on 15/10/9. 5 | */ 6 | public class CloudResponseCode { 7 | 8 | public static final int SUCCESS = 0; 9 | public static final int NOT_IMPL = 1; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/cloud/entity/CloudSetting.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.cloud.entity; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | /** 7 | * Created by Staryet on 15/9/22. 8 | */ 9 | public class CloudSetting { 10 | 11 | private String customerHost; 12 | private List cloudFunctions; 13 | private Map hookSettings; 14 | 15 | public String getCustomerHost() { 16 | return customerHost; 17 | } 18 | 19 | public void setCustomerHost(String customerHost) { 20 | this.customerHost = customerHost; 21 | } 22 | 23 | public List getCloudFunctions() { 24 | return cloudFunctions; 25 | } 26 | 27 | public void setCloudFunctions(List cloudFunctions) { 28 | this.cloudFunctions = cloudFunctions; 29 | } 30 | 31 | public Map getHookSettings() { 32 | return hookSettings; 33 | } 34 | 35 | public void setHookSettings(Map hookSettings) { 36 | this.hookSettings = hookSettings; 37 | } 38 | 39 | public HookSetting getHookSetting(String name) { 40 | return hookSettings == null ? null : hookSettings.get(name); 41 | } 42 | 43 | public boolean hasFunction(String name) { 44 | if (cloudFunctions == null) { 45 | return false; 46 | } else { 47 | for (String function : cloudFunctions) { 48 | if (function.equals(name)) { 49 | return true; 50 | } 51 | } 52 | return false; 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/cloud/entity/HookSetting.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.cloud.entity; 2 | 3 | /** 4 | * Created by Staryet on 15/9/23. 5 | */ 6 | public class HookSetting { 7 | 8 | private boolean beforeInsert; 9 | private boolean afterInsert; 10 | private boolean beforeUpdate; 11 | private boolean afterUpdate; 12 | private boolean beforeDelete; 13 | private boolean afterDelete; 14 | 15 | public HookSetting() { 16 | } 17 | 18 | public HookSetting(boolean enable) { 19 | this.beforeInsert = enable; 20 | this.afterInsert = enable; 21 | this.beforeUpdate = enable; 22 | this.afterUpdate = enable; 23 | this.beforeDelete = enable; 24 | this.afterDelete = enable; 25 | } 26 | 27 | public HookSetting(boolean beforeInsert, boolean afterInsert, boolean beforeUpdate, boolean afterUpdate, boolean beforeDelete, 28 | boolean afterDelete) { 29 | this.beforeInsert = beforeInsert; 30 | this.afterInsert = afterInsert; 31 | this.beforeUpdate = beforeUpdate; 32 | this.afterUpdate = afterUpdate; 33 | this.beforeDelete = beforeDelete; 34 | this.afterDelete = afterDelete; 35 | } 36 | 37 | public boolean isBeforeInsert() { 38 | return beforeInsert; 39 | } 40 | 41 | public void setBeforeInsert(boolean beforeInsert) { 42 | this.beforeInsert = beforeInsert; 43 | } 44 | 45 | public boolean isAfterInsert() { 46 | return afterInsert; 47 | } 48 | 49 | public void setAfterInsert(boolean afterInsert) { 50 | this.afterInsert = afterInsert; 51 | } 52 | 53 | public boolean isBeforeUpdate() { 54 | return beforeUpdate; 55 | } 56 | 57 | public void setBeforeUpdate(boolean beforeUpdate) { 58 | this.beforeUpdate = beforeUpdate; 59 | } 60 | 61 | public boolean isAfterUpdate() { 62 | return afterUpdate; 63 | } 64 | 65 | public void setAfterUpdate(boolean afterUpdate) { 66 | this.afterUpdate = afterUpdate; 67 | } 68 | 69 | public boolean isBeforeDelete() { 70 | return beforeDelete; 71 | } 72 | 73 | public void setBeforeDelete(boolean beforeDelete) { 74 | this.beforeDelete = beforeDelete; 75 | } 76 | 77 | public boolean isAfterDelete() { 78 | return afterDelete; 79 | } 80 | 81 | public void setAfterDelete(boolean afterDelete) { 82 | this.afterDelete = afterDelete; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/cloud/entity/JBRequest.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.cloud.entity; 2 | 3 | import com.javabaas.server.user.entity.BaasUser; 4 | 5 | /** 6 | * Created by Codi on 2018/7/30. 7 | */ 8 | public class JBRequest { 9 | public static final String REQUEST_CLOUD = "1"; 10 | public static final String REQUEST_HOOK = "2"; 11 | 12 | private String name; 13 | private String appId; 14 | private String plat; 15 | private BaasUser user; 16 | private String timestamp; 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | 26 | public String getAppId() { 27 | return appId; 28 | } 29 | 30 | public void setAppId(String appId) { 31 | this.appId = appId; 32 | } 33 | 34 | public String getPlat() { 35 | return plat; 36 | } 37 | 38 | public void setPlat(String plat) { 39 | this.plat = plat; 40 | } 41 | 42 | public BaasUser getUser() { 43 | return user; 44 | } 45 | 46 | public void setUser(BaasUser user) { 47 | this.user = user; 48 | } 49 | 50 | public String getTimestamp() { 51 | return timestamp; 52 | } 53 | 54 | public void setTimestamp(String timestamp) { 55 | this.timestamp = timestamp; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/cloud/entity/JBResponse.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.cloud.entity; 2 | 3 | /** 4 | * Created by Codi on 2018/7/30. 5 | */ 6 | public class JBResponse { 7 | private int code; 8 | private String message; 9 | 10 | public int getCode() { 11 | return code; 12 | } 13 | 14 | public void setCode(int code) { 15 | this.code = code; 16 | } 17 | 18 | public String getMessage() { 19 | return message; 20 | } 21 | 22 | public void setMessage(String message) { 23 | this.message = message; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/cloud/service/CloudService.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.cloud.service; 2 | 3 | import com.javabaas.server.admin.entity.App; 4 | import com.javabaas.server.admin.service.AppService; 5 | import com.javabaas.server.cloud.entity.CloudRequest; 6 | import com.javabaas.server.cloud.entity.CloudResponse; 7 | import com.javabaas.server.cloud.entity.CloudSetting; 8 | import com.javabaas.server.cloud.entity.JBRequest; 9 | import com.javabaas.server.common.entity.SimpleCode; 10 | import com.javabaas.server.common.entity.SimpleError; 11 | import com.javabaas.server.common.entity.SimpleResult; 12 | import com.javabaas.server.user.entity.BaasUser; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.stereotype.Service; 15 | import org.springframework.util.StringUtils; 16 | import org.springframework.web.client.RestTemplate; 17 | 18 | import java.util.Date; 19 | import java.util.Map; 20 | 21 | /** 22 | * Created by Staryet on 15/9/15. 23 | */ 24 | @Service 25 | public class CloudService { 26 | 27 | @Autowired 28 | private RestTemplate rest; 29 | @Autowired 30 | private AppService appService; 31 | 32 | public SimpleResult cloud(String appId, String plat, String functionName, BaasUser user, boolean isMaster, Map 33 | requestParams, String body) { 34 | //准备请求数据 35 | CloudRequest cloudRequest = new CloudRequest(); 36 | //设置用户信息 37 | if (user != null) { 38 | user.setPassword(null); 39 | cloudRequest.setUser(user); 40 | } 41 | if (plat != null) { 42 | cloudRequest.setPlat(plat); 43 | } 44 | //准备云方法请求体 45 | cloudRequest.setName(functionName); 46 | cloudRequest.setAppId(appId); 47 | cloudRequest.setParams(requestParams); 48 | cloudRequest.setBody(body); 49 | //将请求转发至业务服务器 50 | App app = appService.get(appId); 51 | if (app.getCloudSetting() == null || StringUtils.isEmpty(app.getCloudSetting().getCustomerHost())) { 52 | //未部署云代码或云代码地址为空 53 | throw new SimpleError(SimpleCode.CLOUD_NOT_DEPLOYED); 54 | } else { 55 | if (!app.getCloudSetting().hasFunction(functionName)) { 56 | //该方法未定义 57 | throw new SimpleError(SimpleCode.CLOUD_FUNCTION_NOT_FOUND); 58 | } else { 59 | //添加鉴权信息 60 | long timestamp = new Date().getTime(); 61 | String timestampStr = String.valueOf(timestamp); 62 | cloudRequest.setTimestamp(timestampStr); 63 | //发送请求x 64 | CloudResponse response; 65 | try { 66 | response = rest.postForObject(app.getCloudSetting().getCustomerHost() + "?requestType={requestType}", 67 | cloudRequest, CloudResponse.class, JBRequest.REQUEST_CLOUD); 68 | if (response == null) { 69 | throw new SimpleError(SimpleCode.CLOUD_FUNCTION_EXECUTE_FAILED); 70 | } 71 | } catch (Exception e) { 72 | //请求执行异常 73 | throw new SimpleError(SimpleCode.CLOUD_FUNCTION_EXECUTE_FAILED); 74 | } 75 | if (response.getCode() == 0) { 76 | //执行成功 77 | SimpleResult simpleResult = SimpleResult.success(); 78 | simpleResult.setCode(response.getCode()); 79 | simpleResult.setMessage(response.getMessage()); 80 | if (response.getData() != null) { 81 | simpleResult.putDataAll(response.getData()); 82 | } 83 | return simpleResult; 84 | } else { 85 | //执行失败 86 | throw new SimpleError(response.getCode(), response.getMessage()); 87 | } 88 | } 89 | } 90 | } 91 | 92 | public void deploy(String appId, CloudSetting setting) { 93 | appService.setCloudSetting(appId, setting); 94 | } 95 | 96 | public void unDeploy(String appId) { 97 | appService.setCloudSetting(appId, null); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/cloud/util/SignUtil.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.cloud.util; 2 | 3 | import com.javabaas.server.admin.entity.App; 4 | import com.javabaas.server.admin.service.AppService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Component; 7 | import org.springframework.util.DigestUtils; 8 | 9 | /** 10 | * Created by Codi on 16/2/17. 11 | */ 12 | @Component 13 | public class SignUtil { 14 | 15 | @Autowired 16 | private AppService appService; 17 | 18 | public String getSign(String appId, String timestamp) { 19 | App app = appService.get(appId); 20 | return DigestUtils.md5DigestAsHex((app.getKey() + ":" + timestamp).getBytes()); 21 | } 22 | 23 | public String getMasterSign(String appId, String timestamp) { 24 | App app = appService.get(appId); 25 | return DigestUtils.md5DigestAsHex((app.getMasterKey() + ":" + timestamp).getBytes()); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/common/controller/EmptyController.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.common.controller; 2 | 3 | import org.springframework.web.bind.annotation.RestController; 4 | 5 | /** 6 | * 无效请求地址 7 | * Created by Codi on 2017/7/3. 8 | */ 9 | @RestController 10 | public class EmptyController{ 11 | //public class EmptyController implements ErrorController { 12 | 13 | // private final static String ERROR_PATH = "/error"; 14 | // 15 | // @Override 16 | // public String getErrorPath() { 17 | // return ERROR_PATH; 18 | // } 19 | // 20 | // @RequestMapping(value = ERROR_PATH) 21 | // @ResponseBody 22 | // public SimpleResult error() { 23 | // return SimpleResult.error(SimpleCode.NOT_FOUND); 24 | // } 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/common/controller/StatusController.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.common.controller; 2 | 3 | import com.javabaas.server.common.entity.SimpleResult; 4 | import com.javabaas.server.common.service.TimeService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.actuate.health.HealthEndpoint; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RequestMethod; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import java.util.Date; 12 | import java.util.LinkedHashMap; 13 | import java.util.Map; 14 | 15 | /** 16 | * 获取服务器时间 状态 17 | * Created by Codi on 15/10/21. 18 | */ 19 | @RestController 20 | @RequestMapping(value = "/") 21 | public class StatusController { 22 | 23 | @Autowired 24 | private HealthEndpoint healthEndpoint; 25 | @Autowired 26 | private TimeService timeService; 27 | 28 | @RequestMapping(value = "", method = RequestMethod.GET) 29 | public Map time() { 30 | Map result = new LinkedHashMap<>(); 31 | result.put("time", new Date().getTime()); 32 | result.put("started", timeService.getStartedTime()); 33 | result.put("health", healthEndpoint.health()); 34 | return result; 35 | } 36 | 37 | @RequestMapping(value = "api", method = RequestMethod.GET) 38 | public SimpleResult serverTime() { 39 | Map data = new LinkedHashMap<>(); 40 | data.put("time", new Date().getTime()); 41 | data.put("started", timeService.getStartedTime()); 42 | data.put("health", healthEndpoint.health()); 43 | SimpleResult result = SimpleResult.success(); 44 | result.putData("result", data); 45 | return result; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/common/entity/SimpleError.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.common.entity; 2 | 3 | /** 4 | * Created by Staryet on 15/6/18. 5 | */ 6 | public class SimpleError extends RuntimeException { 7 | 8 | private int code; 9 | private String message; 10 | 11 | public static void e(SimpleCode simpleCode) { 12 | throw new SimpleError(simpleCode); 13 | } 14 | 15 | public SimpleError(SimpleCode simpleCode) { 16 | code = simpleCode.getCode(); 17 | message = simpleCode.getMessage(); 18 | } 19 | 20 | public SimpleError(int code) { 21 | this.code = code; 22 | this.message = ""; 23 | } 24 | 25 | public SimpleError(int code, String message) { 26 | this.code = code; 27 | this.message = message; 28 | } 29 | 30 | public int getCode() { 31 | return code; 32 | } 33 | 34 | public void setCode(int code) { 35 | this.code = code; 36 | } 37 | 38 | public String getMessage() { 39 | return message; 40 | } 41 | 42 | public void setMessage(String message) { 43 | this.message = message; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/common/entity/SimpleResult.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.common.entity; 2 | 3 | import com.javabaas.server.object.entity.BaasObject; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | /** 9 | * Created by Staryet on 15/6/15. 10 | */ 11 | public class SimpleResult extends HashMap { 12 | 13 | public static final int SUCCESS = 0; 14 | 15 | public static SimpleResult success() { 16 | return new SimpleResult(SimpleCode.SUCCESS); 17 | } 18 | 19 | public static SimpleResult error(SimpleCode simpleCode) { 20 | return new SimpleResult(simpleCode); 21 | } 22 | 23 | public SimpleResult(SimpleCode simpleCode) { 24 | this(simpleCode.getCode(), simpleCode.getMessage()); 25 | } 26 | 27 | public SimpleResult(int code, String message) { 28 | //初始化data对象 29 | put("data", new BaasObject()); 30 | setCode(code); 31 | setMessage(message); 32 | } 33 | 34 | public static SimpleResult fromError(SimpleError e) { 35 | return new SimpleResult(e.getCode(), e.getMessage()); 36 | } 37 | 38 | public int getCode() { 39 | return (int) get("code"); 40 | } 41 | 42 | public void setCode(int code) { 43 | put("code", code); 44 | } 45 | 46 | public String getMessage() { 47 | return (String) get("message"); 48 | } 49 | 50 | public void setMessage(String message) { 51 | put("message", message); 52 | } 53 | 54 | public SimpleResult putData(String key, Object value) { 55 | getData().put(key, value); 56 | return this; 57 | } 58 | 59 | public Object getData(String key) { 60 | return getData().get(key); 61 | } 62 | 63 | public SimpleResult putDataAll(Map map) { 64 | getData().putAll(map); 65 | return this; 66 | } 67 | 68 | private BaasObject getData() { 69 | return (BaasObject) get("data"); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/common/filter/SimpleCORSFilter.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.common.filter; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import javax.servlet.*; 6 | import javax.servlet.http.HttpServletResponse; 7 | import java.io.IOException; 8 | 9 | /** 10 | * 添加跨域访问标志 11 | */ 12 | @Component 13 | public class SimpleCORSFilter implements Filter { 14 | 15 | public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 16 | HttpServletResponse response = (HttpServletResponse) res; 17 | response.setHeader("Access-Control-Allow-Origin", "*"); 18 | response.setHeader("Access-Control-Allow-Credentials", "true"); 19 | response.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS"); 20 | response.setHeader("Access-Control-Allow-Headers", "content-type,JB-Plat,JB-AppId,JB-Key,JB-AdminKey,JB-MasterKey,JB-Sign," + 21 | "JB-AdminSign,JB-MasterSign,JB-Timestamp,JB-Nonce,JB-SessionToken"); 22 | chain.doFilter(req, res); 23 | } 24 | 25 | public void init(FilterConfig filterConfig) { 26 | } 27 | 28 | public void destroy() { 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/common/interceptor/AdminInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.common.interceptor; 2 | 3 | import com.javabaas.server.common.sign.AuthChecker; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.stereotype.Component; 6 | import org.springframework.web.servlet.HandlerInterceptor; 7 | import org.springframework.web.servlet.ModelAndView; 8 | 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | 12 | /** 13 | * 管理员权限拦截器 14 | */ 15 | @Component 16 | public class AdminInterceptor implements HandlerInterceptor { 17 | 18 | @Autowired 19 | private AuthChecker authChecker; 20 | 21 | @Override 22 | public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { 23 | authChecker.adminCheck(httpServletRequest); 24 | return true; 25 | } 26 | 27 | @Override 28 | public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView 29 | modelAndView) throws Exception { 30 | 31 | } 32 | 33 | @Override 34 | public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) 35 | throws Exception { 36 | 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/common/interceptor/AuthInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.common.interceptor; 2 | 3 | import com.javabaas.server.common.sign.AuthChecker; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.stereotype.Component; 6 | import org.springframework.web.servlet.HandlerInterceptor; 7 | import org.springframework.web.servlet.ModelAndView; 8 | 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | 12 | /** 13 | * API权限拦截器 14 | */ 15 | @Component 16 | public class AuthInterceptor implements HandlerInterceptor { 17 | 18 | @Autowired 19 | private AuthChecker authChecker; 20 | 21 | /** 22 | * 验证授权信息 23 | * sign为 md5(key:timestamp) 24 | */ 25 | @Override 26 | public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) 27 | throws Exception { 28 | authChecker.authCheck(httpServletRequest); 29 | return true; 30 | } 31 | 32 | @Override 33 | public void postHandle(HttpServletRequest httpServletRequest, 34 | HttpServletResponse httpServletResponse, 35 | Object o, ModelAndView modelAndView) throws Exception { 36 | } 37 | 38 | @Override 39 | public void afterCompletion(HttpServletRequest httpServletRequest, 40 | HttpServletResponse httpServletResponse, 41 | Object o, Exception e) throws Exception { 42 | 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/common/interceptor/HeaderInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.common.interceptor; 2 | 3 | import com.javabaas.server.common.entity.SimpleCode; 4 | import com.javabaas.server.common.entity.SimpleError; 5 | import org.springframework.stereotype.Component; 6 | import org.springframework.util.StringUtils; 7 | import org.springframework.web.servlet.HandlerInterceptor; 8 | import org.springframework.web.servlet.ModelAndView; 9 | 10 | import javax.servlet.http.HttpServletRequest; 11 | import javax.servlet.http.HttpServletResponse; 12 | 13 | /** 14 | * 检查请求头是否为json格式 15 | * Created by Codi on 15/10/7. 16 | */ 17 | @Component 18 | public class HeaderInterceptor implements HandlerInterceptor { 19 | 20 | @Override 21 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 22 | if (request.getMethod().equals("OPTIONS")) { 23 | //遇到OPTIONS请求时直接返回成功 24 | response.setStatus(200); 25 | return false; 26 | } 27 | if (!checkPlatform(request.getHeader("JB-Plat"))) { 28 | //检查platform 29 | throw new SimpleError(SimpleCode.REQUEST_PLATFORM_ERROR); 30 | } 31 | return true; 32 | } 33 | 34 | @Override 35 | public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws 36 | Exception { 37 | 38 | } 39 | 40 | @Override 41 | public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { 42 | 43 | } 44 | 45 | private boolean checkPlatform(String platform) { 46 | return !(StringUtils.isEmpty(platform) || 47 | !platform.equals("android") && 48 | !platform.equals("ios") && 49 | !platform.equals("js") && 50 | !platform.equals("cloud") && 51 | !platform.equals("shell") && 52 | !platform.equals("admin")); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/common/interceptor/MasterInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.common.interceptor; 2 | 3 | import com.javabaas.server.common.sign.AuthChecker; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.stereotype.Component; 6 | import org.springframework.web.servlet.HandlerInterceptor; 7 | import org.springframework.web.servlet.ModelAndView; 8 | 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | 12 | /** 13 | * Created by Staryet on 15/9/21. 14 | */ 15 | @Component 16 | public class MasterInterceptor implements HandlerInterceptor { 17 | 18 | @Autowired 19 | private AuthChecker authChecker; 20 | 21 | @Override 22 | public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { 23 | authChecker.masterCheck(httpServletRequest); 24 | return true; 25 | } 26 | 27 | @Override 28 | public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView 29 | modelAndView) throws Exception { 30 | 31 | } 32 | 33 | @Override 34 | public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) 35 | throws Exception { 36 | 37 | } 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/common/listener/ApplicationEventListener.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.common.listener; 2 | 3 | import com.javabaas.server.common.service.TimeService; 4 | import com.javabaas.server.config.AuthConfig; 5 | import com.javabaas.server.config.BaasConfig; 6 | import org.apache.commons.logging.Log; 7 | import org.apache.commons.logging.LogFactory; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent; 10 | import org.springframework.context.ApplicationListener; 11 | import org.springframework.data.mongodb.core.MongoTemplate; 12 | import org.springframework.data.redis.connection.RedisConnection; 13 | import org.springframework.data.redis.connection.RedisConnectionFactory; 14 | import org.springframework.data.redis.core.RedisConnectionUtils; 15 | import org.springframework.stereotype.Component; 16 | 17 | import java.util.Date; 18 | 19 | /** 20 | * 监听系统启动成功 21 | * Created by Codi on 15/10/30. 22 | */ 23 | @Component 24 | public class ApplicationEventListener implements ApplicationListener { 25 | 26 | public static boolean error; 27 | private static boolean ready; 28 | private Log log = LogFactory.getLog(getClass()); 29 | @Autowired 30 | private BaasConfig baasConfig; 31 | @Autowired 32 | private AuthConfig authConfig; 33 | @Autowired 34 | private TimeService timeService; 35 | @Autowired 36 | private RedisConnectionFactory redisConnectionFactory; 37 | @Autowired 38 | private MongoTemplate mongoTemplate; 39 | 40 | @Override 41 | public void onApplicationEvent(ServletWebServerInitializedEvent applicationReadyEvent) { 42 | //启动端口 43 | int port = applicationReadyEvent.getWebServer().getPort(); 44 | //记录启动时间 45 | timeService.setStartedTime(new Date()); 46 | ready = true; 47 | //check out database health 48 | try { 49 | RedisConnection connection = RedisConnectionUtils.getConnection(this.redisConnectionFactory); 50 | try { 51 | connection.info(); 52 | } catch (Exception e) { 53 | error = true; 54 | log.error(e, e); 55 | } finally { 56 | RedisConnectionUtils.releaseConnection(connection, this.redisConnectionFactory); 57 | } 58 | } catch (Exception e) { 59 | error = true; 60 | log.error(e, e); 61 | log.error("Redis error!"); 62 | } 63 | try { 64 | this.mongoTemplate.executeCommand("{ buildInfo: 1 }"); 65 | } catch (Exception e) { 66 | error = true; 67 | log.error(e, e); 68 | log.error("MongoDB error!"); 69 | } 70 | if (!error) { 71 | //应用启动成功 72 | success(port); 73 | } else { 74 | //应用启动失败 75 | log.error("JavaBaasServer failed to start!"); 76 | } 77 | } 78 | 79 | private void success(int port) { 80 | //显示配置信息 81 | log.info("JavaBaasServer started."); 82 | log.info("Key: " + authConfig.getAdminKey()); 83 | log.info("Timeout: " + authConfig.getTimeout()); 84 | log.info("Host:" + baasConfig.getHost()); 85 | //显示浏览器 86 | log.info("JavaBaas status at " + getLocalHost() + ":" + port); 87 | log.info("Browse REST API at " + getLocalHost() + ":" + port + "/explorer.html"); 88 | } 89 | 90 | private String getLocalHost() { 91 | return "http://127.0.0.1"; 92 | } 93 | 94 | public static boolean isReady() { 95 | return ready; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/common/service/MasterService.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.common.service; 2 | 3 | import org.springframework.stereotype.Service; 4 | import org.springframework.util.StringUtils; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | 8 | /** 9 | * Created by Staryet on 15/8/14. 10 | */ 11 | @Service 12 | public class MasterService { 13 | 14 | public boolean isMaster(HttpServletRequest request) { 15 | String masterSign = request.getHeader("JB-MasterSign"); 16 | if (!StringUtils.isEmpty(masterSign)) { 17 | return true; 18 | } else { 19 | return false; 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/common/service/TimeService.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.common.service; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | import java.util.Date; 6 | 7 | /** 8 | * Created by Codi on 2017/10/11. 9 | */ 10 | @Service 11 | public class TimeService { 12 | 13 | private Date startedTime; 14 | 15 | public Date getStartedTime() { 16 | return startedTime; 17 | } 18 | 19 | public void setStartedTime(Date startedTime) { 20 | this.startedTime = startedTime; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/common/sign/ReplyChecker.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.common.sign; 2 | 3 | import com.javabaas.server.common.entity.SimpleCode; 4 | import com.javabaas.server.common.entity.SimpleError; 5 | import com.javabaas.server.config.AuthConfig; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.data.redis.core.StringRedisTemplate; 8 | import org.springframework.data.redis.core.ValueOperations; 9 | import org.springframework.stereotype.Component; 10 | 11 | import java.util.concurrent.TimeUnit; 12 | 13 | /** 14 | * 防重放攻击检查器 15 | * Created by Codi on 2017/7/25. 16 | */ 17 | @Component 18 | public class ReplyChecker { 19 | 20 | private static final String SIGN_NAME = "_Sign"; 21 | 22 | @Autowired 23 | private AuthConfig authConfig; 24 | @Autowired 25 | private StringRedisTemplate redisTemplate; 26 | 27 | /** 28 | * 验证签名是否已经存在 29 | * 30 | * @param appId 应用id 31 | * @param sign 签名 32 | */ 33 | public void checkSignReplay(String appId, String sign) { 34 | //防重放攻击 35 | if (redisTemplate.hasKey(getKey(appId, sign))) { 36 | //拒绝重放攻击 37 | throw new SimpleError(SimpleCode.AUTH_REPLAY_ATTACK); 38 | } 39 | } 40 | 41 | /** 42 | * 记录签名 43 | * 44 | * @param appId 应用id 45 | * @param sign 签名 46 | */ 47 | public void recordSign(String appId, String sign) { 48 | ValueOperations ops = redisTemplate.opsForValue(); 49 | ops.set(getKey(appId, sign), "1", authConfig.getTimeout(), TimeUnit.MICROSECONDS); 50 | } 51 | 52 | private String getKey(String appId, String sign) { 53 | return "App_" + appId + SIGN_NAME + "_" + sign; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/common/sign/SignUtil.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.common.sign; 2 | 3 | import org.springframework.util.DigestUtils; 4 | 5 | /** 6 | * 签名工具类 7 | * Created by Codi on 2017/7/25. 8 | */ 9 | public class SignUtil { 10 | 11 | public static String encrypt(String key, String timeStamp, String nonce) { 12 | return DigestUtils.md5DigestAsHex((key + ":" + timeStamp + ":" + nonce).getBytes()); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/common/util/JSONUtil.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.common.util; 2 | 3 | import com.fasterxml.jackson.core.JsonProcessingException; 4 | import com.fasterxml.jackson.core.type.TypeReference; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import com.javabaas.server.common.entity.SimpleCode; 7 | import com.javabaas.server.common.entity.SimpleError; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Component; 10 | 11 | import javax.annotation.Resource; 12 | import java.io.IOException; 13 | 14 | /** 15 | * Json处理器 16 | * Created by Staryet on 15/9/28. 17 | */ 18 | @Component 19 | public class JSONUtil { 20 | 21 | @Autowired 22 | private ObjectMapper objectMapper; 23 | @Resource(name = "baasMapper") 24 | private ObjectMapper baasMapper; 25 | 26 | public T readValue(String content, Class valueType) { 27 | if (content == null) { 28 | return null; 29 | } 30 | try { 31 | return objectMapper.readValue(content, valueType); 32 | } catch (IOException e) { 33 | throw new SimpleError(SimpleCode.INTERNAL_JSON_ERROR); 34 | } 35 | } 36 | 37 | /** 38 | * 读取JavaBaas对象 39 | * 自动将Map反序列化为BaasObject 40 | * 自动将List反序列化为BaasList 41 | * 42 | * @param content 对象内容 43 | * @return BaasObject 44 | */ 45 | public T readBaas(String content, Class valueType) { 46 | if (content == null) { 47 | return null; 48 | } 49 | try { 50 | return baasMapper.readValue(content, valueType); 51 | } catch (IOException e) { 52 | throw new SimpleError(SimpleCode.INTERNAL_JSON_ERROR); 53 | } 54 | } 55 | 56 | public T readValue(String content, TypeReference valueType) { 57 | if (content == null) { 58 | return null; 59 | } 60 | try { 61 | return objectMapper.readValue(content, valueType); 62 | } catch (IOException e) { 63 | throw new SimpleError(SimpleCode.INTERNAL_JSON_ERROR); 64 | } 65 | } 66 | 67 | public String writeValueAsString(Object value) { 68 | if (value != null) { 69 | try { 70 | return objectMapper.writeValueAsString(value); 71 | } catch (JsonProcessingException e) { 72 | throw new SimpleError(SimpleCode.INTERNAL_JSON_ERROR); 73 | } 74 | } else { 75 | return ""; 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/config/AuthConfig.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.config; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | /** 7 | * Created by Codi on 15/12/17. 8 | */ 9 | @Configuration 10 | @ConfigurationProperties(prefix = "baas.auth") 11 | public class AuthConfig { 12 | 13 | private static final long DEFAULT_TIMEOUT = 600000; 14 | private static final String DEFAULT_ADMIN_KEY = "JavaBaas"; 15 | 16 | private String key; 17 | 18 | private long timeout; 19 | 20 | public String getAdminKey() { 21 | if (key == null) { 22 | return DEFAULT_ADMIN_KEY; 23 | } else { 24 | return key; 25 | } 26 | } 27 | 28 | public void setKey(String key) { 29 | this.key = key; 30 | } 31 | 32 | public long getTimeout() { 33 | if (timeout == 0) { 34 | return DEFAULT_TIMEOUT; 35 | } else { 36 | return timeout; 37 | } 38 | } 39 | 40 | public void setTimeout(long timeout) { 41 | this.timeout = timeout; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/config/BaasConfig.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | /** 8 | * Created by Codi on 15/12/17. 9 | */ 10 | @Configuration 11 | @ConfigurationProperties(prefix = "baas") 12 | public class BaasConfig { 13 | 14 | private static final String DEFAULT_HOST = "http://127.0.0.1:8080/"; 15 | 16 | private String host; 17 | @Autowired 18 | private AuthConfig authConfig; 19 | 20 | public String getHost() { 21 | if (host == null) { 22 | return DEFAULT_HOST; 23 | } else { 24 | return host; 25 | } 26 | } 27 | 28 | public void setHost(String host) { 29 | this.host = host; 30 | } 31 | 32 | public AuthConfig getAuthConfig() { 33 | return authConfig; 34 | } 35 | 36 | public void setAuthConfig(AuthConfig authConfig) { 37 | this.authConfig = authConfig; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/config/controller/AppConfigController.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.config.controller; 2 | 3 | import com.javabaas.server.common.entity.SimpleResult; 4 | import com.javabaas.server.config.entity.AppConfig; 5 | import com.javabaas.server.config.entity.AppConfigEnum; 6 | import com.javabaas.server.config.service.AppConfigService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.web.bind.annotation.*; 9 | 10 | import javax.validation.Valid; 11 | import java.util.HashMap; 12 | import java.util.LinkedList; 13 | import java.util.List; 14 | import java.util.Map; 15 | 16 | /** 17 | * 应用配置接口 18 | * Created by Codi on 2017/7/6. 19 | */ 20 | @RestController 21 | @RequestMapping(value = "/api/master/config") 22 | public class AppConfigController { 23 | 24 | @Autowired 25 | private AppConfigService appConfigService; 26 | 27 | /** 28 | * 设置应用配置 29 | * 30 | * @param appId 应用id 31 | * @param config 配置 32 | */ 33 | @RequestMapping(value = "/app", method = RequestMethod.POST) 34 | @ResponseBody 35 | public SimpleResult setAppConfig(@RequestHeader(value = "JB-AppId") String appId, 36 | @Valid @RequestBody AppConfig config) { 37 | appConfigService.setConfig(appId, config); 38 | return SimpleResult.success(); 39 | } 40 | 41 | /** 42 | * 获取应用配置 43 | * 44 | * @param appId 应用id 45 | */ 46 | @RequestMapping(value = "/app", method = RequestMethod.GET) 47 | @ResponseBody 48 | public SimpleResult getAppConfig(@RequestHeader(value = "JB-AppId") String appId, 49 | @RequestParam String key) { 50 | String value = appConfigService.getString(appId, key); 51 | Map config = new HashMap<>(); 52 | config.put(key, value); 53 | SimpleResult result = SimpleResult.success(); 54 | result.putData("result", config); 55 | return result; 56 | } 57 | 58 | /** 59 | * 获取所有可用的应用配置 60 | * 61 | * @return 配置 62 | */ 63 | @RequestMapping(value = "app/configs", method = RequestMethod.GET) 64 | @ResponseBody 65 | public SimpleResult getAppConfigs() { 66 | List> list = new LinkedList<>(); 67 | for (AppConfigEnum appConfigEnum : AppConfigEnum.values()) { 68 | Map map = new HashMap<>(); 69 | map.put("key", appConfigEnum.getKey()); 70 | map.put("name", appConfigEnum.getName()); 71 | list.add(map); 72 | } 73 | SimpleResult result = SimpleResult.success(); 74 | result.putData("result", list); 75 | return result; 76 | } 77 | 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/config/entity/AppConfig.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.config.entity; 2 | 3 | 4 | import javax.validation.constraints.NotEmpty; 5 | 6 | /** 7 | * 应用配置 8 | * Created by Codi on 2017/7/21. 9 | */ 10 | public class AppConfig { 11 | @NotEmpty 12 | private String key; 13 | @NotEmpty 14 | private String value; 15 | 16 | public AppConfig() { 17 | } 18 | 19 | public AppConfig(String key, String value) { 20 | this.key = key; 21 | this.value = value; 22 | } 23 | 24 | public String getKey() { 25 | return key; 26 | } 27 | 28 | public String getNoDotKey() { 29 | return key.replaceAll("\\.", ""); 30 | } 31 | 32 | public void setKey(String key) { 33 | this.key = key; 34 | } 35 | 36 | public String getValue() { 37 | return value; 38 | } 39 | 40 | public void setValue(String value) { 41 | this.value = value; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/config/entity/AppConfigEnum.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.config.entity; 2 | 3 | import org.springframework.util.StringUtils; 4 | 5 | /** 6 | * Created by Codi on 2017/7/6. 7 | */ 8 | public enum AppConfigEnum { 9 | // 短信相关 10 | SMS_TRY_LIMIT("baas.sms.tryLimit", "重试次数", "5"), 11 | SMS_HANDLER("baas.sms.handler", "短信发送器", "aliyun"), 12 | SMS_HANDLER_ALIYUN_KEY("baas.sms.handler.aliyun.key", "阿里云key", ""), 13 | SMS_HANDLER_ALIYUN_SECRET("baas.sms.handler.aliyun.secret", "阿里云secret", ""), 14 | SMS_CODE_TEMPLATE_ID("baas.sms.codeTemplateId", "短信验证码模版id", ""), 15 | SMS_REGISTER_TEMPLATE_ID("baas.sms.registerTemplateId", "登录注册验证码模版id", ""), 16 | SMS_BIND_TEMPLATE_ID("baas.sms.bindTemplateId", "绑定手机号验证码模版id", ""), 17 | SMS_SIGN_NAME("baas.sms.signName", "短信签名", ""), 18 | SMS_SEND_INTERVAL("baas.sms.interval", "短信发送间隔", "60"), 19 | SMS_REGISTER_SUPER_CODE("baas.sms.register.super.code", "登录注册万能验证码",""), 20 | // 推送相关 21 | PUSH_HANDLER("baas.push.handler", "推送处理", "jpush"), 22 | PUSH_HANDLER_JPUSH_KEY("baas.push.handler.jpush.key", "极光推送key", ""), 23 | PUSH_HANDLER_JPUSH_SECRET("baas.push.handler.jpush.secret", "极光推送secret", ""), 24 | // 文件存储相关 25 | FILE_HANDLER("baas.file.handler", "推送处理", "qiniu"), 26 | FILE_HANDLER_QINIU_AK("baas.file.handler.qiniu.ak", "七牛ak", ""), 27 | FILE_HANDLER_QINIU_SK("baas.file.handler.qiniu.sk", "七牛sk", ""), 28 | FILE_HANDLER_QINIU_BUCKET("baas.file.handler.qiniu.bucket", "七牛bucket", ""), 29 | FILE_HANDLER_QINIU_PIPELINE("baas.file.handler.qiniu.pipeline", "七牛pipeline", ""), 30 | FILE_HANDLER_QINIU_URL("baas.file.handler.qiniu.url", "七牛url", ""), 31 | ///////// huadong/huabei/huanan/beimei/auto 一共四个zone,如果无值或者值没有匹配项,则认为是auto 32 | FILE_HANDLER_QINIU_ZONE("baas.file.handler.qiniu.zone", "七牛zone", ""), 33 | // 微信小程序 34 | WEBAPP_APPID("baas.webapp.appid", "微信小程序appid", ""), 35 | WEBAPP_SECRET("baas.webapp.secret", "微信小程序secret", ""); 36 | 37 | private String key; 38 | private String name; 39 | private String defaultValue; 40 | 41 | public String getKey() { 42 | return key; 43 | } 44 | 45 | public String getName() { 46 | return name; 47 | } 48 | 49 | public String getDefaultValue() { 50 | return defaultValue; 51 | } 52 | 53 | public static AppConfigEnum getConfig(String key) { 54 | if (StringUtils.isEmpty(key)) { 55 | return null; 56 | } else { 57 | for (AppConfigEnum appConfigEnum : AppConfigEnum.values()) { 58 | if (key.equals(appConfigEnum.getKey())) { 59 | return appConfigEnum; 60 | } 61 | } 62 | } 63 | return null; 64 | } 65 | 66 | public static String getDefaultValue(String key) { 67 | AppConfigEnum config = getConfig(key); 68 | if (config != null) { 69 | return config.getDefaultValue(); 70 | } else { 71 | return null; 72 | } 73 | } 74 | 75 | AppConfigEnum(String key, String name, String defaultValue) { 76 | this.key = key; 77 | this.name = name; 78 | this.defaultValue = defaultValue; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/config/entity/AppConfigs.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.config.entity; 2 | 3 | import com.javabaas.server.admin.entity.App; 4 | import org.springframework.data.mongodb.core.mapping.DBRef; 5 | import org.springframework.data.mongodb.core.mapping.Document; 6 | 7 | import java.util.HashMap; 8 | import java.util.Map; 9 | 10 | /** 11 | * 应用配置 12 | * Created by Codi on 2017/7/8. 13 | */ 14 | @Document 15 | public class AppConfigs { 16 | 17 | private String id; 18 | @DBRef 19 | private App app; 20 | private Map params; 21 | 22 | public String getId() { 23 | return id; 24 | } 25 | 26 | public void setId(String id) { 27 | this.id = id; 28 | } 29 | 30 | public App getApp() { 31 | return app; 32 | } 33 | 34 | public void setApp(App app) { 35 | this.app = app; 36 | } 37 | 38 | public Map getParams() { 39 | return params; 40 | } 41 | 42 | public void setParams(Map params) { 43 | this.params = params; 44 | } 45 | 46 | public void setParam(AppConfig config) { 47 | if (params == null) { 48 | params = new HashMap<>(); 49 | } 50 | params.put(config.getNoDotKey(), config); 51 | } 52 | 53 | public String getParam(String key) { 54 | if (params == null || params.get(key) == null) { 55 | return null; 56 | } else { 57 | return params.get(key).getValue(); 58 | } 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/config/repository/AppConfigRepository.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.config.repository; 2 | 3 | import com.javabaas.server.config.entity.AppConfigs; 4 | import org.springframework.data.mongodb.repository.MongoRepository; 5 | 6 | /** 7 | * Created by Codi on 2017/7/8. 8 | */ 9 | public interface AppConfigRepository extends MongoRepository { 10 | 11 | AppConfigs findByAppId(String appId); 12 | 13 | long deleteByAppId(String appId); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/file/entity/BaasFile.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.file.entity; 2 | 3 | import com.javabaas.server.object.entity.BaasList; 4 | import com.javabaas.server.object.entity.BaasObject; 5 | 6 | import java.util.Map; 7 | 8 | /** 9 | * Created by Staryet on 15/8/13. 10 | */ 11 | public class BaasFile extends BaasObject { 12 | 13 | public BaasFile() { 14 | } 15 | 16 | public BaasFile(Map m) { 17 | super(m); 18 | } 19 | 20 | public void setUrl(String url) { 21 | put("url", url); 22 | } 23 | 24 | public String getUrl() { 25 | return getString("url"); 26 | } 27 | 28 | public void setName(String name) { 29 | put("name", name); 30 | } 31 | 32 | public String getName() { 33 | return getString("name"); 34 | } 35 | 36 | public void setKey(String key) { 37 | put("key", key); 38 | } 39 | 40 | public String getKey() { 41 | return getString("key"); 42 | } 43 | 44 | public void setMimeType(String mimeType) { 45 | put("mimeType", mimeType); 46 | } 47 | 48 | public String getMimeType() { 49 | return getString("mimeType"); 50 | } 51 | 52 | public void setSize(long size) { 53 | put("size", size); 54 | } 55 | 56 | public long getSize() { 57 | return getLong("size"); 58 | } 59 | 60 | public void setPersistentFiles(BaasList list) { 61 | put("persistentFiles", list); 62 | } 63 | 64 | public BaasList getPersistentFiles() { 65 | return getList("persistentFiles"); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/file/entity/FileStoragePlatform.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.file.entity; 2 | 3 | import com.javabaas.server.common.entity.SimpleCode; 4 | import com.javabaas.server.common.entity.SimpleError; 5 | import org.springframework.util.StringUtils; 6 | 7 | /** 8 | * Created by Staryet on 15/8/27. 9 | */ 10 | public enum FileStoragePlatform { 11 | 12 | Test("test"), 13 | Qiniu("qiniu"), 14 | Upyun("upyun"); 15 | 16 | private String name; 17 | 18 | FileStoragePlatform(String name) { 19 | this.name = name; 20 | } 21 | 22 | @Override 23 | public String toString() { 24 | return name; 25 | } 26 | 27 | public static FileStoragePlatform get(String name) { 28 | if (StringUtils.isEmpty(name)) { 29 | //未指定文件存储平台 30 | throw new SimpleError(SimpleCode.FILE_PLATFORM_EMPTY); 31 | } 32 | FileStoragePlatform[] plats = FileStoragePlatform.class.getEnumConstants(); 33 | for (FileStoragePlatform plat : plats) { 34 | if (name.equals(plat.name)) { 35 | return plat; 36 | } 37 | } 38 | //文件存储平台错误 39 | throw new SimpleError(SimpleCode.FILE_PLATFORM_ERROR); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/file/entity/qiniu/PersistentItem.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.file.entity.qiniu; 2 | 3 | /** 4 | * Created by Codi on 15/11/12. 5 | */ 6 | public class PersistentItem { 7 | 8 | private String cmd; 9 | private String key; 10 | 11 | public String getCmd() { 12 | return cmd; 13 | } 14 | 15 | public void setCmd(String cmd) { 16 | this.cmd = cmd; 17 | } 18 | 19 | public String getKey() { 20 | return key; 21 | } 22 | 23 | public void setKey(String key) { 24 | this.key = key; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/file/entity/qiniu/PersistentResult.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.file.entity.qiniu; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Created by Codi on 15/11/12. 7 | */ 8 | public class PersistentResult { 9 | private String id; 10 | private String inputKey; 11 | private int code; 12 | private List items; 13 | 14 | public String getId() { 15 | return id; 16 | } 17 | 18 | public void setId(String id) { 19 | this.id = id; 20 | } 21 | 22 | public String getInputKey() { 23 | return inputKey; 24 | } 25 | 26 | public void setInputKey(String inputKey) { 27 | this.inputKey = inputKey; 28 | } 29 | 30 | public int getCode() { 31 | return code; 32 | } 33 | 34 | public void setCode(int code) { 35 | this.code = code; 36 | } 37 | 38 | public List getItems() { 39 | return items; 40 | } 41 | 42 | public void setItems(List items) { 43 | this.items = items; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/file/handler/IFileHandler.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.file.handler; 2 | 3 | import com.javabaas.server.file.entity.BaasFile; 4 | 5 | import javax.servlet.http.HttpServletRequest; 6 | import java.util.Map; 7 | 8 | /** 9 | * Created by Staryet on 15/8/27. 10 | */ 11 | public interface IFileHandler { 12 | 13 | Map getToken(String appId, String plat, String name, Map policy); 14 | 15 | BaasFile fetch(String appId, String plat, BaasFile file, Map policy); 16 | 17 | BaasFile callback(String body, HttpServletRequest request); 18 | 19 | BaasFile process(String appId, String plat, String fileId, Map policy); 20 | 21 | void persistentNotify(String body, HttpServletRequest request); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/file/handler/impl/TestFileHandler.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.file.handler.impl; 2 | 3 | import com.fasterxml.jackson.core.type.TypeReference; 4 | import com.javabaas.server.common.util.JSONUtil; 5 | import com.javabaas.server.file.entity.BaasFile; 6 | import com.javabaas.server.file.handler.IFileHandler; 7 | import com.javabaas.server.file.service.FileService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Component; 10 | 11 | import javax.servlet.http.HttpServletRequest; 12 | import java.util.HashMap; 13 | import java.util.Map; 14 | 15 | /** 16 | * Created by Codi on 16/1/10. 17 | */ 18 | @Component 19 | public class TestFileHandler implements IFileHandler { 20 | 21 | @Autowired 22 | private FileService fileService; 23 | @Autowired 24 | private JSONUtil jsonUtil; 25 | 26 | @Override 27 | public Map getToken(String appId, String plat, String name, Map policy) { 28 | return null; 29 | } 30 | 31 | @Override 32 | public BaasFile fetch(String appId, String plat, BaasFile file, Map policy) { 33 | file = fileService.saveFile(appId, plat, file); 34 | return file; 35 | } 36 | 37 | @Override 38 | public BaasFile callback(String body, HttpServletRequest request) { 39 | BaasFile file = new BaasFile(); 40 | Map params = jsonUtil.readValue(body, new TypeReference>() { 41 | }); 42 | file.setName(params.get("source")); 43 | String plat = params.get("plat"); 44 | String appId = params.get("app"); 45 | return fileService.saveFile(appId, plat, file); 46 | } 47 | 48 | @Override 49 | public BaasFile process(String appId, String plat, String fileId, Map policy) { 50 | return null; 51 | } 52 | 53 | @Override 54 | public void persistentNotify(String body, HttpServletRequest request) { 55 | 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/file/service/FileService.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.file.service; 2 | 3 | import com.javabaas.server.common.entity.SimpleCode; 4 | import com.javabaas.server.common.entity.SimpleError; 5 | import com.javabaas.server.file.entity.BaasFile; 6 | import com.javabaas.server.file.entity.FileStoragePlatform; 7 | import com.javabaas.server.file.handler.IFileHandler; 8 | import com.javabaas.server.file.handler.impl.QiniuFileHandler; 9 | import com.javabaas.server.file.handler.impl.TestFileHandler; 10 | import com.javabaas.server.object.entity.BaasObject; 11 | import com.javabaas.server.object.service.ObjectService; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.stereotype.Service; 14 | import org.springframework.util.StringUtils; 15 | 16 | import javax.servlet.http.HttpServletRequest; 17 | import java.util.Map; 18 | import java.util.UUID; 19 | 20 | /** 21 | * 文件存储服务 22 | * Created by Staryet on 15/8/26. 23 | */ 24 | @Service 25 | public class FileService { 26 | 27 | public static String FILE_CLASS_NAME = "_File"; 28 | @Autowired 29 | private ObjectService objectService; 30 | @Autowired 31 | private TestFileHandler testFileHandler; 32 | @Autowired 33 | private QiniuFileHandler qiniuFileHandler; 34 | 35 | public Map getToken(String appId, String plat, FileStoragePlatform filePlat, String fileName, Map 36 | policy) { 37 | return getFileHandler(filePlat).getToken(appId, plat, fileName, policy); 38 | } 39 | 40 | public BaasFile callback(FileStoragePlatform plat, String body, HttpServletRequest request) { 41 | return getFileHandler(plat).callback(body, request); 42 | } 43 | 44 | public void persistentNotify(FileStoragePlatform plat, String body, HttpServletRequest request) { 45 | getFileHandler(plat).persistentNotify(body, request); 46 | } 47 | 48 | public BaasFile getFile(String appId, String plat, String id) { 49 | BaasObject object = objectService.get(appId, plat, FILE_CLASS_NAME, id); 50 | if (object == null) { 51 | return null; 52 | } else { 53 | return new BaasFile(object); 54 | } 55 | } 56 | 57 | public BaasFile saveFile(String appId, String plat, BaasFile file) { 58 | //禁止设置ACL字段 59 | file.remove("acl"); 60 | if (StringUtils.isEmpty(file.getKey())) { 61 | file.setKey(getFileKey()); 62 | } 63 | return new BaasFile(objectService.insert(appId, plat, FILE_CLASS_NAME, file, true, null, true)); 64 | } 65 | 66 | public BaasFile saveFileWithFetch(String appId, String plat, FileStoragePlatform platform, BaasFile file, Map policy) { 67 | //上传 68 | IFileHandler fileHandler = getFileHandler(platform); 69 | file = fileHandler.fetch(appId, plat, file, policy); 70 | return file; 71 | } 72 | 73 | public BaasFile process(String appId, String plat, FileStoragePlatform filePlat, String fileId, Map policy) { 74 | return getFileHandler(filePlat).process(appId, plat, fileId, policy); 75 | } 76 | 77 | private IFileHandler getFileHandler(FileStoragePlatform plat) { 78 | IFileHandler handler = null; 79 | switch (plat) { 80 | case Test: 81 | handler = testFileHandler; 82 | break; 83 | case Qiniu: 84 | handler = qiniuFileHandler; 85 | break; 86 | case Upyun: 87 | break; 88 | default: 89 | break; 90 | } 91 | if (handler == null) { 92 | throw new SimpleError(SimpleCode.FILE_NO_HANDLER); 93 | } 94 | return handler; 95 | } 96 | 97 | public String getFileKey() { 98 | return UUID.randomUUID().toString().replace("-", ""); 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/hook/entity/HookEvent.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.hook.entity; 2 | 3 | /** 4 | * Created by Codi on 15/10/11. 5 | */ 6 | public enum HookEvent { 7 | BEFORE_INSERT(1, "beforeInsert"), 8 | AFTER_INSERT(2, "afterInsert"), 9 | BEFORE_UPDATE(3, "beforeUpdate"), 10 | AFTER_UPDATE(4, "afterUpdate"), 11 | BEFORE_DELETE(5, "beforeDelete"), 12 | AFTER_DELETE(6, "afterDelete"); 13 | 14 | private int code; 15 | private String name; 16 | 17 | HookEvent(int code, String name) { 18 | this.code = code; 19 | this.name = name; 20 | } 21 | 22 | public int getCode() { 23 | return code; 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/hook/entity/HookRequest.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.hook.entity; 2 | 3 | import com.javabaas.server.cloud.entity.JBRequest; 4 | import com.javabaas.server.object.entity.BaasObject; 5 | 6 | /** 7 | * Created by Staryet on 15/9/24. 8 | */ 9 | public class HookRequest extends JBRequest { 10 | 11 | private HookEvent event; 12 | private BaasObject object; 13 | 14 | public HookEvent getEvent() { 15 | return event; 16 | } 17 | 18 | public void setEvent(HookEvent event) { 19 | this.event = event; 20 | } 21 | 22 | public BaasObject getObject() { 23 | return object; 24 | } 25 | 26 | public void setObject(BaasObject object) { 27 | this.object = object; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/hook/entity/HookResponse.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.hook.entity; 2 | 3 | import com.javabaas.server.cloud.entity.JBResponse; 4 | import com.javabaas.server.object.entity.BaasObject; 5 | 6 | /** 7 | * Created by Staryet on 15/9/24. 8 | */ 9 | public class HookResponse extends JBResponse { 10 | 11 | public BaasObject object; 12 | 13 | public BaasObject getObject() { 14 | return object; 15 | } 16 | 17 | public void setObject(BaasObject object) { 18 | this.object = object; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/hook/entity/HookResponseCode.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.hook.entity; 2 | 3 | /** 4 | * Created by Staryet on 15/9/27. 5 | */ 6 | public class HookResponseCode { 7 | 8 | public static final int SUCCESS = 0; 9 | public static final int ERROR = 1; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/log/appender/LogbackMongoAppender.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.log.appender; 2 | 3 | import ch.qos.logback.classic.spi.ILoggingEvent; 4 | import ch.qos.logback.core.AppenderBase; 5 | import com.javabaas.server.common.listener.ApplicationEventListener; 6 | import com.javabaas.server.log.service.MongoConnection; 7 | import com.mongodb.BasicDBObject; 8 | import com.mongodb.DBCollection; 9 | 10 | /** 11 | * MongoDb日志记录器 12 | * Created by Codi on 15/10/29. 13 | */ 14 | public class LogbackMongoAppender extends AppenderBase { 15 | 16 | private String server; 17 | private String username; 18 | private String database; 19 | private String password; 20 | private String host; 21 | private int port; 22 | 23 | @Override 24 | protected void append(ILoggingEvent iLoggingEvent) { 25 | MongoConnection.connectToMongo(database, username, password, host, port); 26 | if (ApplicationEventListener.isReady()) { 27 | //系统启动成功后 开始记录日志 28 | DBCollection c = MongoConnection.getDb().getCollection("log"); 29 | BasicDBObject dbo = new BasicDBObject(); 30 | dbo.append("server", server); 31 | dbo.put("level", iLoggingEvent.getLevel().toString()); 32 | dbo.append("thread", iLoggingEvent.getThreadName()); 33 | dbo.append("logger", iLoggingEvent.getLoggerName()); 34 | dbo.append("message", iLoggingEvent.getFormattedMessage()); 35 | dbo.put("timestamp", iLoggingEvent.getTimeStamp()); 36 | c.insert(dbo); 37 | } 38 | } 39 | 40 | @Override 41 | public void stop() { 42 | MongoConnection.close(); 43 | super.stop(); 44 | } 45 | 46 | public void setServer(String server) { 47 | this.server = server; 48 | } 49 | 50 | public void setUsername(String username) { 51 | this.username = username; 52 | } 53 | 54 | public void setDatabase(String database) { 55 | this.database = database; 56 | } 57 | 58 | public void setPassword(String password) { 59 | this.password = password; 60 | } 61 | 62 | public void setHost(String host) { 63 | this.host = host; 64 | } 65 | 66 | public void setPort(int port) { 67 | this.port = port; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/log/controller/LogController.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.log.controller; 2 | 3 | import com.javabaas.server.log.entity.BaasLog; 4 | import com.javabaas.server.log.service.LogService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.*; 7 | 8 | import java.util.List; 9 | 10 | /** 11 | * 日志 12 | * Created by Codi on 15/10/31. 13 | */ 14 | @RestController 15 | @RequestMapping(value = "/api/admin/log") 16 | public class LogController { 17 | 18 | @Autowired 19 | private LogService logService; 20 | 21 | @RequestMapping(value = "/{serverName}", method = RequestMethod.GET) 22 | public List getLog(@PathVariable("serverName") String serverName, 23 | @RequestParam(required = false) String level, 24 | @RequestParam(required = false) String logger, 25 | @RequestParam(required = false) Long from, 26 | @RequestParam(required = false) Long to) { 27 | return logService.getLogs(serverName, level, logger, from, to); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/log/entity/BaasLog.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.log.entity; 2 | 3 | import com.javabaas.server.object.entity.BaasObject; 4 | 5 | import java.util.Map; 6 | 7 | /** 8 | * Created by Codi on 15/10/31. 9 | */ 10 | public class BaasLog extends BaasObject { 11 | 12 | public BaasLog() { 13 | super(); 14 | } 15 | 16 | public BaasLog(Map m) { 17 | super(m); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/log/service/LogService.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.log.service; 2 | 3 | import com.javabaas.server.log.entity.BaasLog; 4 | import com.mongodb.BasicDBObject; 5 | import com.mongodb.DBCollection; 6 | import com.mongodb.DBCursor; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.LinkedList; 10 | import java.util.List; 11 | 12 | /** 13 | * Created by Codi on 15/10/31. 14 | */ 15 | @Service 16 | public class LogService { 17 | 18 | public List getLogs(String serverName, String level, String logger, Long from, Long to) { 19 | DBCollection c = MongoConnection.getLogCollection(); 20 | BasicDBObject query = new BasicDBObject(); 21 | query.put("server", serverName); 22 | if (level != null) { 23 | query.put("level", level); 24 | } 25 | if (logger != null) { 26 | query.put("logger", logger); 27 | } 28 | if (from != null) { 29 | query.put("timestamp", new BasicDBObject("$gt", from)); 30 | } 31 | if (to != null) { 32 | query.put("timestamp", new BasicDBObject("$lt", to)); 33 | } 34 | BasicDBObject sort = new BasicDBObject(); 35 | //默认排序为时间倒序 36 | sort.put("timestamp", -1); 37 | LinkedList result = new LinkedList<>(); 38 | DBCursor cursor = c.find(query).sort(sort).limit(1000); 39 | cursor.forEach(dbo -> { 40 | dbo.removeField("_id"); 41 | result.add(new BaasLog((BasicDBObject) dbo)); 42 | }); 43 | return result; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/log/service/MongoConnection.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.log.service; 2 | 3 | import com.mongodb.*; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | /** 9 | * Created by Codi on 15/10/31. 10 | */ 11 | public class MongoConnection { 12 | 13 | private static MongoClient mongo; 14 | private static DB db; 15 | 16 | public static void connectToMongo(String database, String username, String password, String host, int port) { 17 | if (db == null) { 18 | MongoClientOptions options = MongoClientOptions.builder().build(); 19 | List credentials = Arrays.asList(MongoCredential.createScramSha1Credential( 20 | username, "admin", password.toCharArray())); 21 | mongo = new MongoClient(Arrays.asList(new ServerAddress(host, port)), 22 | credentials, options); 23 | db = mongo.getDB(database); 24 | } 25 | } 26 | 27 | public static DB getDb() { 28 | return db; 29 | } 30 | 31 | public static DBCollection getLogCollection() { 32 | return db.getCollection("log"); 33 | } 34 | 35 | public static void close() { 36 | if (mongo != null) { 37 | mongo.close(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/dao/IDao.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.dao; 2 | 3 | import com.javabaas.server.object.entity.*; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * 对象存储抽象 9 | * Created by Codi on 15/12/9. 10 | */ 11 | public interface IDao { 12 | 13 | BaasObject insert(String appId, String className, BaasObject object); 14 | 15 | BaasObject findOne(String appId, String className, BaasQuery query); 16 | 17 | List find(String appId, String className, BaasQuery query, BaasList keys, BaasSort sort, Integer limit, Integer skip); 18 | 19 | void update(String appId, String className, BaasQuery query, BaasObject object); 20 | 21 | void findAndModify(String appId, String className, BaasQuery query, BaasObject object); 22 | 23 | void remove(String appId, String className, BaasQuery query); 24 | 25 | void removeApp(String appId); 26 | 27 | void removeClass(String appId, String className); 28 | 29 | void removeField(String appId, String className, String fieldName); 30 | 31 | long count(String appId, String className, BaasQuery query); 32 | 33 | void createIndex(String appId, String className, String fieldName, IndexType indexType); 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/dao/impl/mongo/MongoDBConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.dao.impl.mongo; 2 | 3 | import com.mongodb.MongoClient; 4 | import com.mongodb.MongoClientOptions; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 7 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.core.env.Environment; 11 | 12 | import javax.annotation.PreDestroy; 13 | import java.net.UnknownHostException; 14 | 15 | /** 16 | * Created by Codi on 15/10/1. 17 | */ 18 | @Configuration 19 | @ConditionalOnClass(MongoClient.class) 20 | @ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDbFactory") 21 | public class MongoDBConfiguration { 22 | @Autowired 23 | private MongoProperties properties; 24 | 25 | @Autowired(required = false) 26 | private MongoClientOptions options; 27 | 28 | @Autowired 29 | private Environment environment; 30 | 31 | private MongoClient mongo; 32 | 33 | @PreDestroy 34 | public void close() { 35 | if (this.mongo != null) { 36 | this.mongo.close(); 37 | } 38 | } 39 | 40 | @Bean 41 | @ConditionalOnMissingBean 42 | public MongoClient mongo() throws UnknownHostException { 43 | this.mongo = this.properties.createMongoClient(this.options, this.environment); 44 | return this.mongo; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/entity/BaasAcl.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.entity; 2 | 3 | import com.javabaas.server.role.entity.BaasRole; 4 | import com.javabaas.server.user.entity.BaasUser; 5 | 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | /** 10 | * Created by Staryet on 15/8/13. 11 | */ 12 | public class BaasAcl extends BaasObject { 13 | 14 | public BaasAcl() { 15 | } 16 | 17 | public BaasAcl(Map m) { 18 | super(m); 19 | } 20 | 21 | public BaasAcl(String key, Object value) { 22 | super(key, value); 23 | } 24 | 25 | public void setPublicReadAccess(boolean access) { 26 | Map accessMap = getAccessMap("*"); 27 | accessMap.put("read", access); 28 | put("*", accessMap); 29 | } 30 | 31 | public void setPublicWriteAccess(boolean access) { 32 | Map accessMap = getAccessMap("*"); 33 | accessMap.put("write", access); 34 | put("*", accessMap); 35 | } 36 | 37 | public void setReadAccess(BaasUser user, boolean access) { 38 | Map accessMap = getAccessMap(user.getId()); 39 | accessMap.put("read", access); 40 | put(user.getId(), accessMap); 41 | } 42 | 43 | public void setReadAccess(BaasRole role, boolean access) { 44 | Map accessMap = getAccessMap("role:" + role.getName()); 45 | accessMap.put("read", access); 46 | put("role:" + role.getName(), accessMap); 47 | } 48 | 49 | public void setWriteAccess(BaasUser user, boolean access) { 50 | Map accessMap = getAccessMap(user.getId()); 51 | accessMap.put("write", access); 52 | put(user.getId(), accessMap); 53 | } 54 | 55 | public void setWriteAccess(BaasRole role, boolean access) { 56 | Map accessMap = getAccessMap("role:" + role.getName()); 57 | accessMap.put("write", access); 58 | put("role:" + role.getName(), accessMap); 59 | } 60 | 61 | public Map getAccessMap(String name) { 62 | Map accessMap = (Map) get(name); 63 | if (accessMap == null) { 64 | accessMap = new HashMap<>(); 65 | } 66 | return accessMap; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/entity/BaasInclude.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.entity; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * Created by Staryet on 15/8/3. 8 | */ 9 | public class BaasInclude { 10 | 11 | private String name; 12 | private Map sub; 13 | 14 | public BaasInclude(String name) { 15 | this.name = name; 16 | sub = new HashMap<>(); 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | 27 | public Map getSubs() { 28 | return sub; 29 | } 30 | 31 | public BaasInclude getSub(String name) { 32 | return sub.get(name); 33 | } 34 | 35 | public void addSub(BaasInclude include) { 36 | sub.put(include.getName(), include); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/entity/BaasList.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.entity; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | import java.util.Map; 6 | 7 | /** 8 | * Created by Staryet on 15/6/23. 9 | */ 10 | public class BaasList extends ArrayList { 11 | 12 | public BaasList() { 13 | } 14 | 15 | public BaasList(Collection c) { 16 | super(c); 17 | } 18 | 19 | @SuppressWarnings("unchecked") 20 | public BaasObject getBaasObject(int index) { 21 | Object value = get(index); 22 | Map object = null; 23 | try { 24 | object = (Map) value; 25 | } catch (ClassCastException ignored) { 26 | } 27 | return object == null ? null : new BaasObject(object); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/entity/BaasObject.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.entity; 2 | 3 | import com.javabaas.server.user.entity.BaasUser; 4 | 5 | import java.util.Date; 6 | import java.util.LinkedHashMap; 7 | import java.util.Map; 8 | 9 | /** 10 | * Created by Staryet on 15/6/23. 11 | */ 12 | public class BaasObject extends LinkedHashMap { 13 | 14 | public BaasObject() { 15 | super(); 16 | } 17 | 18 | public BaasObject(Map m) { 19 | super(m); 20 | } 21 | 22 | public BaasObject(String key, Object value) { 23 | this.put(key, value); 24 | } 25 | 26 | public void setId(String id) { 27 | put("_id", id); 28 | } 29 | 30 | public String getId() { 31 | return (String) get("_id"); 32 | } 33 | 34 | public BaasAcl getAcl() { 35 | return get("acl") == null ? null : new BaasAcl((BaasObject)get("acl")); 36 | } 37 | 38 | public void setAcl(BaasAcl acl) { 39 | put("acl", acl); 40 | } 41 | 42 | public String getString(String key) { 43 | return (String) get(key); 44 | } 45 | 46 | public boolean getBoolean(String key) { 47 | Boolean b = (Boolean) get(key); 48 | return b == null ? false : b; 49 | } 50 | 51 | public long getLong(String key) { 52 | Object value = get(key); 53 | return value == null ? 0 : Long.valueOf(value.toString()); 54 | } 55 | 56 | public int getInt(String key) { 57 | Object value = get(key); 58 | return value == null ? 0 : Integer.valueOf(value.toString()); 59 | } 60 | 61 | public BaasList getList(String key) { 62 | Object value = get(key); 63 | return value == null ? null : (BaasList) value; 64 | } 65 | 66 | @SuppressWarnings("unchecked") 67 | public BaasObject getBaasObject(String key) { 68 | Object value = get(key); 69 | Map object = null; 70 | try { 71 | object = (Map) value; 72 | } catch (ClassCastException ignored) { 73 | } 74 | return object == null ? null : new BaasObject(object); 75 | } 76 | 77 | @SuppressWarnings("unchecked") 78 | public BaasUser getBaasUser(String key) { 79 | Object value = get(key); 80 | Map object = null; 81 | try { 82 | object = (Map) value; 83 | } catch (ClassCastException ignored) { 84 | } 85 | return object == null ? null : new BaasUser(object); 86 | } 87 | 88 | public Date getCreatedAt() { 89 | Date date = new Date(); 90 | date.setTime(getLong("createdAt")); 91 | return date; 92 | } 93 | 94 | public Date getUpdatedAt() { 95 | Date date = new Date(); 96 | date.setTime(getLong("updatedAt")); 97 | return date; 98 | } 99 | 100 | public Object getCreatedPlatform() { 101 | return get("createdPlat"); 102 | } 103 | 104 | public Object getUpdatedPlatform() { 105 | return get("updatedPlat"); 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/entity/BaasOperator.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.entity; 2 | 3 | /** 4 | * Created by Codi on 2017/8/4. 5 | */ 6 | public class BaasOperator { 7 | 8 | private BaasOperatorEnum type; 9 | private Object value; 10 | 11 | public BaasOperator(BaasOperatorEnum type, Object value) { 12 | this.type = type; 13 | this.value = value; 14 | } 15 | 16 | public BaasOperatorEnum getType() { 17 | return type; 18 | } 19 | 20 | public void setType(BaasOperatorEnum type) { 21 | this.type = type; 22 | } 23 | 24 | public Object getValue() { 25 | return value; 26 | } 27 | 28 | public void setValue(Object value) { 29 | this.value = value; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/entity/BaasOperatorEnum.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.entity; 2 | 3 | import org.springframework.util.StringUtils; 4 | 5 | /** 6 | * Created by Codi on 2017/8/4. 7 | */ 8 | public enum BaasOperatorEnum { 9 | DELETE("Delete"), 10 | ADD("Add"), 11 | ADD_UNIQUE("AddUnique"), 12 | REMOVE("Remove"), 13 | INCREMENT("Increment"), 14 | MULTIPLY("Multiply"); 15 | 16 | private String name; 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | BaasOperatorEnum(String name) { 23 | this.name = name; 24 | } 25 | 26 | public static BaasOperatorEnum getOperator(String key) { 27 | if (StringUtils.isEmpty(key)) { 28 | return null; 29 | } else { 30 | for (BaasOperatorEnum operator : BaasOperatorEnum.values()) { 31 | if (key.equals(operator.getName())) { 32 | return operator; 33 | } 34 | } 35 | } 36 | return null; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/entity/BaasQuery.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.entity; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * Created by Staryet on 15/6/25. 7 | */ 8 | public class BaasQuery extends BaasObject { 9 | 10 | public BaasQuery() { 11 | } 12 | 13 | public BaasQuery(Map m) { 14 | super(m); 15 | } 16 | 17 | public BaasQuery(String key, Object value) { 18 | super(key, value); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/entity/BaasSort.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.entity; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * Created by Staryet on 15/6/25. 7 | */ 8 | public class BaasSort extends BaasObject { 9 | 10 | public BaasSort() { 11 | } 12 | 13 | public BaasSort(String key, Object value) { 14 | super(key, value); 15 | } 16 | 17 | public BaasSort(Map m) { 18 | super(m); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/entity/ClassIds.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.entity; 2 | 3 | import java.util.*; 4 | 5 | /** 6 | * Created by Staryet on 15/8/5. 7 | */ 8 | public class ClassIds { 9 | 10 | Map> classes = new HashMap<>(); 11 | 12 | public void addId(String className, String id) { 13 | List classIds = getIds(className); 14 | classIds.add(id); 15 | } 16 | 17 | public List getIds(String className) { 18 | List classIds = classes.get(className); 19 | if (classIds == null) { 20 | classIds = new ArrayList<>(); 21 | classes.put(className, classIds); 22 | } 23 | return classIds; 24 | } 25 | 26 | public Set getClassNames() { 27 | return classes.keySet(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/entity/IndexType.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.entity; 2 | 3 | /** 4 | * Created by Codi on 2018/12/4. 5 | */ 6 | public enum IndexType { 7 | GEO_2D_SPHERE//地理位置索引 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/entity/error/DuplicateKeyError.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.entity.error; 2 | 3 | import com.javabaas.server.common.entity.SimpleCode; 4 | import com.javabaas.server.common.entity.SimpleError; 5 | 6 | /** 7 | * 字段类型错误 8 | */ 9 | public class DuplicateKeyError extends SimpleError { 10 | 11 | public DuplicateKeyError(String key) { 12 | super(SimpleCode.OBJECT_DUPLICATE_KEY.getCode(), "唯一字段" + key + "禁止重复值"); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/entity/error/FieldRequiredError.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.entity.error; 2 | 3 | import com.javabaas.server.common.entity.SimpleCode; 4 | import com.javabaas.server.common.entity.SimpleError; 5 | 6 | /** 7 | * 字段类型错误 8 | */ 9 | public class FieldRequiredError extends SimpleError { 10 | 11 | public FieldRequiredError(String fieldName) { 12 | super(SimpleCode.OBJECT_FIELD_NOTNULL.getCode(), "字段" + fieldName + "不能为空!"); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/entity/error/FieldTypeError.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.entity.error; 2 | 3 | import com.javabaas.server.common.entity.SimpleCode; 4 | import com.javabaas.server.common.entity.SimpleError; 5 | 6 | /** 7 | * 字段类型错误 8 | */ 9 | public class FieldTypeError extends SimpleError { 10 | 11 | public FieldTypeError(String message) { 12 | super(SimpleCode.OBJECT_FIELD_TYPE_ERROR.getCode(), message); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/entity/error/ObjectInsertError.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.entity.error; 2 | 3 | import com.javabaas.server.common.entity.SimpleCode; 4 | import com.javabaas.server.common.entity.SimpleError; 5 | 6 | /** 7 | * Created by Codi on 2018/12/5. 8 | */ 9 | public class ObjectInsertError extends SimpleError { 10 | 11 | public ObjectInsertError() { 12 | super(SimpleCode.OBJECT_INSERT_ERROR); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/entity/error/OperatorWrongTypeError.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.entity.error; 2 | 3 | import com.javabaas.server.common.entity.SimpleCode; 4 | import com.javabaas.server.common.entity.SimpleError; 5 | 6 | /** 7 | * 字段类型错误 8 | */ 9 | public class OperatorWrongTypeError extends SimpleError { 10 | 11 | public OperatorWrongTypeError(String key, String name) { 12 | super(SimpleCode.OBJECT_OPERATOR_WRONG_TYPE.getCode(), 13 | "字段: " + key + " 操作符类型错误。操作符 " + name + " 不可用。"); 14 | } 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/entity/error/OperatorWrongValueError.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.entity.error; 2 | 3 | import com.javabaas.server.common.entity.SimpleCode; 4 | import com.javabaas.server.common.entity.SimpleError; 5 | 6 | /** 7 | * 字段类型错误 8 | */ 9 | public class OperatorWrongValueError extends SimpleError { 10 | 11 | public OperatorWrongValueError(String key, Object value) { 12 | super(SimpleCode.OBJECT_OPERATOR_WRONG_VALUE.getCode(), 13 | "字段: " + key + " 类型错误。类型 " + value.getClass() + " 不可用。"); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/service/ClazzAclChecker.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.service; 2 | 3 | import com.javabaas.server.admin.entity.Clazz; 4 | import com.javabaas.server.admin.entity.ClazzAclMethod; 5 | import com.javabaas.server.admin.service.ClazzService; 6 | import com.javabaas.server.common.entity.SimpleCode; 7 | import com.javabaas.server.common.entity.SimpleError; 8 | import com.javabaas.server.user.entity.BaasUser; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.stereotype.Component; 11 | 12 | /** 13 | * Created by Codi on 2017/8/3. 14 | */ 15 | @Component 16 | public class ClazzAclChecker { 17 | 18 | @Autowired 19 | private ClazzService clazzService; 20 | 21 | public void verifyClazzAccess(String appId, ClazzAclMethod method, String className, BaasUser user, boolean isMaster) { 22 | Clazz clazz = clazzService.get(appId, className); 23 | if (clazz.getAcl() != null) { 24 | //验证表级ACL权限 25 | if (!isMaster) { 26 | //非master权限验证acl 27 | if (!clazz.getAcl().hasAccess(method, user)) { 28 | //无操作权限 29 | throw new SimpleError(SimpleCode.OBJECT_CLAZZ_NO_ACCESS); 30 | } 31 | } 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/service/ObjectExtractor.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.service; 2 | 3 | import com.javabaas.server.admin.entity.Field; 4 | import com.javabaas.server.object.entity.BaasObject; 5 | import org.springframework.stereotype.Component; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * Created by Codi on 2017/9/15. 11 | */ 12 | @Component 13 | public class ObjectExtractor { 14 | 15 | /** 16 | * 提取对象 17 | * 按照权限提取对象 无管理权限时过滤掉保密字段 18 | * 19 | * @param fields 对象字段列表 20 | * @param object 原对象 21 | * @param isMaster 是否为管理权限 22 | * @return 提取后对象 23 | */ 24 | public BaasObject extractObject(List fields, BaasObject object, boolean isMaster) { 25 | BaasObject extracted = new BaasObject(); 26 | //获取id 27 | Object id = object.get("_id"); 28 | if (id == null) { 29 | return null; 30 | } 31 | extracted.put("_id", id.toString()); 32 | //获取时间 33 | Object createdAt = object.get("createdAt"); 34 | Object updatedAt = object.get("updatedAt"); 35 | if (createdAt == null || updatedAt == null) { 36 | return null; 37 | } 38 | extracted.put("createdAt", object.get("createdAt")); 39 | extracted.put("updatedAt", object.get("updatedAt")); 40 | //获取plat 41 | extracted.put("createdPlat", object.get("createdPlat")); 42 | extracted.put("updatedPlat", object.get("updatedPlat")); 43 | //获取ACL 44 | Object acl = object.get("acl"); 45 | if (acl == null) { 46 | return null; 47 | } 48 | extracted.put("acl", object.get("acl")); 49 | //自定义字段 50 | fields.forEach(field -> { 51 | if (!field.isSecurity() || isMaster) { 52 | //非管理权限 无法操作保密字段 53 | String name = field.getName(); 54 | Object value = object.get(name); 55 | if (value != null) { 56 | extracted.put(name, value); 57 | } 58 | } 59 | }); 60 | return extracted; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/object/util/BaasObjectIdUtil.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.object.util; 2 | 3 | import org.springframework.util.StringUtils; 4 | 5 | import java.util.UUID; 6 | 7 | /** 8 | * Created by Codi on 2017/9/12. 9 | */ 10 | public class BaasObjectIdUtil { 11 | 12 | public static String createId() { 13 | return UUID.randomUUID().toString().replace("-", ""); 14 | } 15 | 16 | public static boolean isValidId(String id) { 17 | return !StringUtils.isEmpty(id) && id.length() == 32; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/push/controller/PushController.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.push.controller; 2 | 3 | import com.javabaas.server.common.entity.SimpleResult; 4 | import com.javabaas.server.common.util.JSONUtil; 5 | import com.javabaas.server.object.entity.BaasQuery; 6 | import com.javabaas.server.push.entity.Push; 7 | import com.javabaas.server.push.service.PushService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.util.StringUtils; 10 | import org.springframework.web.bind.annotation.*; 11 | 12 | import javax.validation.Valid; 13 | 14 | /** 15 | * 推送 16 | * Created by Codi on 15/11/2. 17 | */ 18 | @RestController 19 | @RequestMapping(value = "/api/master/push") 20 | public class PushController { 21 | 22 | @Autowired 23 | private PushService pushService; 24 | @Autowired 25 | private JSONUtil jsonUtil; 26 | 27 | /** 28 | * 推送消息 29 | * 30 | * @return 结果 31 | */ 32 | @RequestMapping(value = "", method = RequestMethod.POST) 33 | @ResponseBody 34 | public SimpleResult push(@RequestHeader(value = "JB-AppId") String appId, 35 | @RequestHeader(value = "JB-Plat") String plat, 36 | @RequestParam(required = false) String where, 37 | @Valid @RequestBody Push push) { 38 | //处理查询字段 39 | BaasQuery query = StringUtils.isEmpty(where) ? null : jsonUtil.readValue(where, BaasQuery.class); 40 | pushService.sendPush(appId, plat, query, push); 41 | return SimpleResult.success(); 42 | } 43 | 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/push/entity/Push.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.push.entity; 2 | 3 | /** 4 | * Created by Codi on 15/11/2. 5 | */ 6 | public class Push { 7 | 8 | // 透传信息,静默推送 9 | private PushMessage message; 10 | // 推送信息,可打通知栏 11 | private PushNotification notification; 12 | 13 | public PushMessage getMessage() { 14 | return message; 15 | } 16 | 17 | public void setMessage(PushMessage message) { 18 | this.message = message; 19 | } 20 | 21 | public PushNotification getNotification() { 22 | return notification; 23 | } 24 | 25 | public void setNotification(PushNotification notification) { 26 | this.notification = notification; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/push/entity/PushLog.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.push.entity; 2 | 3 | import com.javabaas.server.object.entity.BaasObject; 4 | import com.javabaas.server.object.entity.BaasQuery; 5 | 6 | import java.util.Map; 7 | 8 | /** 9 | * 推送记录 10 | * Created by Codi on 15/11/2. 11 | */ 12 | public class PushLog extends BaasObject { 13 | 14 | public PushLog() { 15 | } 16 | 17 | public PushLog(Map m) { 18 | super(m); 19 | } 20 | 21 | public void setWhere(BaasQuery where) { 22 | put("where", where); 23 | } 24 | 25 | public BaasQuery getWhere() { 26 | return new BaasQuery((Map) get("where")); 27 | } 28 | 29 | public void setTitle(String title) { 30 | put("title", title); 31 | } 32 | 33 | public String getTitle() { 34 | return getString("title"); 35 | } 36 | 37 | public void setAlert(String alert) { 38 | put("alert", alert); 39 | } 40 | 41 | public String getAlert() { 42 | return getString("alert"); 43 | } 44 | 45 | public void setBadge(int badge) { 46 | put("badge", badge); 47 | } 48 | 49 | public Integer getBadge() { 50 | return getInt("badge"); 51 | } 52 | 53 | public void setSound(String sound) { 54 | put("sound", sound); 55 | } 56 | 57 | public String getSound() { 58 | return getString("sound"); 59 | } 60 | 61 | public void setParams(Map params) { 62 | put("params", params); 63 | } 64 | 65 | public Map getParams() { 66 | return (Map) get("params"); 67 | } 68 | 69 | public void setPushTime(long time) { 70 | put("pushTime", time); 71 | } 72 | 73 | public long getPushTime() { 74 | return getLong("pushTime"); 75 | } 76 | 77 | public void setContentType(String contentType) { 78 | put("contentType", contentType); 79 | } 80 | 81 | public String getContentType() { 82 | return getString("contentType"); 83 | } 84 | 85 | public void setPushType(int type) { 86 | put("pushType", type); 87 | } 88 | 89 | public int getPushType() { 90 | return getInt("pushType"); 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/push/entity/PushMessage.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.push.entity; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * Created by test on 2017/6/27. 7 | *

8 | * 应用内消息。或者称作:自定义消息,透传消息。 9 | * 此部分内容不会展示到通知栏上。 10 | */ 11 | public class PushMessage { 12 | 13 | // 消息标题 14 | private String title; 15 | // 消息内容本身 16 | private String alert; 17 | // 消息内容类型, 例如text 18 | private String contentType; 19 | // 扩展参数 20 | private Map extras; 21 | 22 | 23 | public String getTitle() { 24 | return title; 25 | } 26 | 27 | public void setTitle(String title) { 28 | this.title = title; 29 | } 30 | 31 | public String getAlert() { 32 | return alert; 33 | } 34 | 35 | public void setAlert(String alert) { 36 | this.alert = alert; 37 | } 38 | 39 | public String getContentType() { 40 | return contentType; 41 | } 42 | 43 | public void setContentType(String contentType) { 44 | this.contentType = contentType; 45 | } 46 | 47 | public Map getExtras() { 48 | return extras; 49 | } 50 | 51 | public void setExtras(Map extras) { 52 | this.extras = extras; 53 | } 54 | 55 | @Override 56 | public String toString() { 57 | return "PushMessage{" + 58 | "title='" + title + '\'' + 59 | ", alert='" + alert + '\'' + 60 | ", contentType='" + contentType + '\'' + 61 | ", extras=" + extras + 62 | '}'; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/push/entity/PushNotification.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.push.entity; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * Created by test on 2017/6/27. 7 | *

8 | * 通知类消息,是会作为“通知”推送到客户端的 9 | */ 10 | public class PushNotification { 11 | // 标题 12 | private String title; 13 | // 内容 不能为空 14 | private String alert; 15 | // 通知提示音 16 | private String sound; 17 | // 应用角标 18 | private int badge; 19 | // 扩展参数 20 | private Map extras; 21 | // ios静默推送选项 22 | private Boolean contentAvailable; 23 | // ios10支持的附件选项 24 | private Boolean mutableContent; 25 | 26 | 27 | public String getTitle() { 28 | return title; 29 | } 30 | 31 | public void setTitle(String title) { 32 | this.title = title; 33 | } 34 | 35 | public String getAlert() { 36 | return alert; 37 | } 38 | 39 | public void setAlert(String alert) { 40 | this.alert = alert; 41 | } 42 | 43 | public String getSound() { 44 | return sound; 45 | } 46 | 47 | public void setSound(String sound) { 48 | this.sound = sound; 49 | } 50 | 51 | public int getBadge() { 52 | return badge; 53 | } 54 | 55 | public void setBadge(int badge) { 56 | this.badge = badge; 57 | } 58 | 59 | public Map getExtras() { 60 | return extras; 61 | } 62 | 63 | public void setExtras(Map extras) { 64 | this.extras = extras; 65 | } 66 | 67 | public Boolean getContentAvailable() { 68 | return contentAvailable; 69 | } 70 | 71 | public void setContentAvailable(Boolean contentAvailable) { 72 | this.contentAvailable = contentAvailable; 73 | } 74 | 75 | public Boolean getMutableContent() { 76 | return mutableContent; 77 | } 78 | 79 | public void setMutableContent(Boolean mutableContent) { 80 | this.mutableContent = mutableContent; 81 | } 82 | 83 | @Override 84 | public String toString() { 85 | return "PushNotification{" + 86 | "title='" + title + '\'' + 87 | ", alert='" + alert + '\'' + 88 | ", sound='" + sound + '\'' + 89 | ", badge=" + badge + 90 | ", extras=" + extras + 91 | ", contentAvailable=" + contentAvailable + 92 | ", mutableContent=" + mutableContent + 93 | '}'; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/push/handler/IPushHandler.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.push.handler; 2 | 3 | import com.javabaas.server.push.entity.Push; 4 | 5 | import java.util.Collection; 6 | 7 | /** 8 | * 推送接口 9 | * Created by Codi on 15/11/2. 10 | */ 11 | public interface IPushHandler { 12 | 13 | /** 14 | * 推送给特定设备 15 | * 16 | * @param id installationId 17 | * @param push 推送消息 18 | */ 19 | void pushSingle(String appId, String id, Push push); 20 | 21 | /** 22 | * 推送给多个设备 23 | * 24 | * @param ids id列表 25 | * @param push 推送消息 26 | */ 27 | void pushMulti(String appId, Collection ids, Push push); 28 | 29 | /** 30 | * 推送到所有设备 31 | * 32 | * @param push 推送消息 33 | */ 34 | void pushAll(String appId, Push push); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/push/handler/impl/TestPushHandler.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.push.handler.impl; 2 | 3 | import com.javabaas.server.push.entity.Push; 4 | import com.javabaas.server.push.handler.IPushHandler; 5 | 6 | import java.util.Collection; 7 | 8 | /** 9 | * Created by Codi on 16/1/11. 10 | */ 11 | public class TestPushHandler implements IPushHandler { 12 | 13 | @Override 14 | public void pushSingle(String appId, String id, Push push) { 15 | 16 | } 17 | 18 | @Override 19 | public void pushMulti(String appId, Collection ids, Push push) { 20 | 21 | } 22 | 23 | @Override 24 | public void pushAll(String appId, Push push) { 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/push/service/PushService.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.push.service; 2 | 3 | import com.javabaas.server.common.entity.SimpleCode; 4 | import com.javabaas.server.common.entity.SimpleError; 5 | import com.javabaas.server.config.entity.AppConfigEnum; 6 | import com.javabaas.server.config.service.AppConfigService; 7 | import com.javabaas.server.object.entity.BaasObject; 8 | import com.javabaas.server.object.entity.BaasQuery; 9 | import com.javabaas.server.object.service.ObjectService; 10 | import com.javabaas.server.push.entity.Push; 11 | import com.javabaas.server.push.entity.PushLog; 12 | import com.javabaas.server.push.handler.IPushHandler; 13 | import com.javabaas.server.user.service.InstallationService; 14 | import org.apache.commons.logging.Log; 15 | import org.apache.commons.logging.LogFactory; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.stereotype.Service; 18 | 19 | import java.util.Collection; 20 | import java.util.LinkedList; 21 | import java.util.List; 22 | import java.util.Map; 23 | 24 | /** 25 | * 推送 26 | * Created by Codi on 15/11/2. 27 | */ 28 | @Service 29 | public class PushService { 30 | 31 | public static String PUSH_LOG_CLASS_NAME = "_PushLog"; 32 | private Log logger = LogFactory.getLog(getClass()); 33 | @Autowired 34 | private ObjectService objectService; 35 | 36 | @Autowired 37 | private Map handlers; 38 | @Autowired 39 | private AppConfigService appConfigService; 40 | 41 | public void sendPush(String appId, String plat, BaasQuery query, Push push) { 42 | if (push.getNotification() == null && push.getMessage() == null) { 43 | throw new SimpleError(SimpleCode.PUSH_EMPTY); 44 | } 45 | String handlerName = appConfigService.getString(appId, AppConfigEnum.PUSH_HANDLER); 46 | IPushHandler pushHandler = handlers.get(handlerName); 47 | if (query == null) { 48 | //全体推送 49 | pushHandler.pushAll(appId, push); 50 | } else { 51 | //按查询条件推送 52 | List devices = objectService.find(appId, plat, InstallationService.INSTALLATION_CLASS_NAME, query, null, null, 53 | null, 1000, 0, null, true); 54 | Collection ids = new LinkedList<>(); 55 | devices.forEach(device -> ids.add(device.getId())); 56 | pushHandler.pushMulti(appId, ids, push); 57 | } 58 | PushLog pushLog = new PushLog(); 59 | //记录推送日志 60 | if (push.getNotification() != null) { 61 | pushLog.setPushType(1); 62 | pushLog.setTitle(push.getNotification().getTitle()); 63 | pushLog.setAlert(push.getNotification().getAlert()); 64 | if (push.getNotification().getBadge() != 0) { 65 | pushLog.setBadge(push.getNotification().getBadge()); 66 | } 67 | pushLog.setSound(push.getNotification().getSound()); 68 | } else if (push.getMessage() != null) { 69 | pushLog.setPushType(2); 70 | pushLog.setTitle(push.getMessage().getTitle()); 71 | pushLog.setAlert(push.getMessage().getAlert()); 72 | pushLog.setContentType(push.getMessage().getContentType()); 73 | if (push.getMessage().getExtras() != null) { 74 | pushLog.setParams(push.getMessage().getExtras()); 75 | } 76 | } 77 | pushLog.setPushTime(System.currentTimeMillis()); 78 | if (query != null) { 79 | pushLog.setWhere(query); 80 | } 81 | pushLog = new PushLog(objectService.insert(appId, plat, PUSH_LOG_CLASS_NAME, pushLog, true, null, true)); 82 | logger.debug("App:" + appId 83 | + " 推送成功 id:" + pushLog.getId() 84 | + " title:" + pushLog.getTitle() 85 | + " alert:" + pushLog.getAlert() 86 | + " badge:" + pushLog.getBadge() 87 | + " sound:" + pushLog.getSound() 88 | + " where:" + pushLog.getWhere()); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/role/entity/BaasRole.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.role.entity; 2 | 3 | import com.javabaas.server.object.entity.BaasObject; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | /** 9 | * Created by zangyilin on 2018/4/16. 10 | */ 11 | public class BaasRole extends BaasObject { 12 | public BaasRole() { 13 | super(); 14 | } 15 | 16 | public BaasRole(Map m) { 17 | super(m); 18 | } 19 | 20 | public void setName(String name) { 21 | put("name", name); 22 | } 23 | 24 | public String getName() { 25 | return (String) get("name"); 26 | } 27 | 28 | public void setRoles(List roles) { 29 | put("roles", roles); 30 | } 31 | 32 | public List getRoles() { 33 | return get("roles") == null ? null : (List) get("roles"); 34 | } 35 | 36 | public void setUsers(List users) { 37 | put("users", users); 38 | } 39 | 40 | public List getUsers() { 41 | return get("users") == null ? null : (List) get("users"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/sms/controller/SmsController.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.sms.controller; 2 | 3 | import com.javabaas.server.common.entity.SimpleResult; 4 | import com.javabaas.server.common.util.JSONUtil; 5 | import com.javabaas.server.object.entity.BaasObject; 6 | import com.javabaas.server.sms.service.SmsService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.util.StringUtils; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | /** 12 | * 短信发送 13 | * Created by Codi on 15/11/2. 14 | */ 15 | @RestController 16 | @RequestMapping(value = "/api/master/sms") 17 | public class SmsController { 18 | 19 | @Autowired 20 | private SmsService smsService; 21 | @Autowired 22 | private JSONUtil jsonUtil; 23 | 24 | /** 25 | * 发送短信 26 | * 27 | * @return 结果 28 | */ 29 | @RequestMapping(value = "", method = RequestMethod.POST) 30 | @ResponseBody 31 | public SimpleResult send(@RequestHeader(value = "JB-AppId") String appId, 32 | @RequestHeader(value = "JB-Plat") String plat, 33 | @RequestParam String phone, 34 | @RequestParam String templateId, 35 | @RequestBody(required = false) String params) { 36 | BaasObject paramsObject = StringUtils.isEmpty(params) ? null : jsonUtil.readValue(params, BaasObject.class); 37 | return smsService.sendSms(appId, plat, phone, templateId, paramsObject); 38 | } 39 | 40 | /** 41 | * 发送短信验证码 42 | * 43 | * @param phone 手机号码 44 | * @param ttl 失效时间 45 | */ 46 | @RequestMapping(value = "/smsCode", method = RequestMethod.POST) 47 | @ResponseBody 48 | public SimpleResult smsCode(@RequestHeader(value = "JB-AppId") String appId, 49 | @RequestHeader(value = "JB-Plat") String plat, 50 | @RequestParam String phone, 51 | @RequestParam long ttl, 52 | @RequestBody(required = false) String params) { 53 | BaasObject paramsObject = StringUtils.isEmpty(params) ? null : jsonUtil.readValue(params, BaasObject.class); 54 | return smsService.sendSmsCode(appId, plat, phone, ttl, paramsObject); 55 | } 56 | 57 | /** 58 | * 验证短信验证码 59 | * 60 | * @param phone 手机号码 61 | * @param code 验证码 62 | */ 63 | @RequestMapping(value = "/verifyCode", method = RequestMethod.GET) 64 | @ResponseBody 65 | public SimpleResult verifyCode(@RequestHeader(value = "JB-AppId") String appId, 66 | @RequestHeader(value = "JB-Plat") String plat, 67 | @RequestParam String phone, 68 | @RequestParam String code) { 69 | boolean result = smsService.verifySmsCode(appId, phone, code); 70 | return SimpleResult.success().putData("verifyResult", result); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/sms/entity/SmsLog.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.sms.entity; 2 | 3 | import com.javabaas.server.object.entity.BaasObject; 4 | 5 | import java.util.Map; 6 | 7 | /** 8 | * Created by Codi on 2017/6/30. 9 | */ 10 | public class SmsLog extends BaasObject { 11 | 12 | public SmsLog() { 13 | } 14 | 15 | public SmsLog(Map m) { 16 | super(m); 17 | } 18 | 19 | public void setPhone(String phone) { 20 | put("phone", phone); 21 | } 22 | 23 | public String getPhone() { 24 | return getString("phone"); 25 | } 26 | 27 | public void setSignName(String signName) { 28 | put("signName", signName); 29 | } 30 | 31 | public String getSignName() { 32 | return getString("signName"); 33 | } 34 | 35 | public void setTemplateId(String templateId) { 36 | put("templateId", templateId); 37 | } 38 | 39 | public String getTemplateId() { 40 | return getString("templateId"); 41 | } 42 | 43 | public void setParams(BaasObject params) { 44 | put("params", params); 45 | } 46 | 47 | public String getParams() { 48 | return getString("params"); 49 | } 50 | 51 | public void setState(int state) { 52 | put("state", state); 53 | } 54 | 55 | public int getState() { 56 | return getInt("state"); 57 | } 58 | 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/sms/entity/SmsSendState.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.sms.entity; 2 | 3 | /** 4 | * 短信发送状态 5 | * Created by Codi on 2017/6/30. 6 | */ 7 | public enum SmsSendState { 8 | 9 | WAIT(0),//等待发送 10 | SUCCESS(1), //发送成功 11 | FAIL(2);//失败 12 | 13 | private int code; 14 | 15 | SmsSendState(int code) { 16 | this.code = code; 17 | } 18 | 19 | public int getCode() { 20 | return code; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/sms/handler/ISmsHandler.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.sms.handler; 2 | 3 | import com.javabaas.server.common.entity.SimpleResult; 4 | import com.javabaas.server.object.entity.BaasObject; 5 | 6 | /** 7 | * Created by Codi on 2017/6/26. 8 | */ 9 | public interface ISmsHandler { 10 | 11 | /** 12 | * 发送短信 13 | * 14 | * @param appId 应用 15 | * @param id 流水号 16 | * @param phone 目标电话号码 17 | * @param signName 短信签名 18 | * @param templateId 模版编号 19 | * @param params 参数 20 | * @return 发送结果 21 | */ 22 | SimpleResult sendSms(String appId, String id, String phone, String signName, String templateId, BaasObject params); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/sms/handler/impl/LocalSmsSendHandler.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.sms.handler.impl; 2 | 3 | import com.javabaas.server.common.entity.SimpleResult; 4 | import com.javabaas.server.object.entity.BaasObject; 5 | import com.javabaas.server.sms.handler.ISmsHandler; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.web.client.RestTemplate; 8 | 9 | /** 10 | * Created by Codi on 2017/6/30. 11 | */ 12 | public class LocalSmsSendHandler implements ISmsHandler { 13 | 14 | @Autowired 15 | private RestTemplate rest; 16 | 17 | @Override 18 | public SimpleResult sendSms(String appId, String id, String phone, String signName, String templateId, BaasObject params) { 19 | return null; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/sms/handler/impl/MockSmsHandler.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.sms.handler.impl; 2 | 3 | import com.javabaas.server.common.entity.SimpleResult; 4 | import com.javabaas.server.object.entity.BaasObject; 5 | import com.javabaas.server.sms.handler.ISmsHandler; 6 | import org.apache.commons.logging.Log; 7 | import org.apache.commons.logging.LogFactory; 8 | import org.springframework.stereotype.Component; 9 | 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | /** 14 | * 用于测试的短信发送器 15 | * Created by Codi on 2017/6/28. 16 | */ 17 | @Component("mock") 18 | public class MockSmsHandler implements ISmsHandler { 19 | 20 | private Log log = LogFactory.getLog(getClass()); 21 | 22 | private Map map = new HashMap<>(); 23 | 24 | public String getSms(String phone) { 25 | return map.get(phone); 26 | } 27 | 28 | @Override 29 | public SimpleResult sendSms(String appId, String id, String phone, String signName, String templateId, BaasObject params) { 30 | StringBuilder sms = new StringBuilder(); 31 | if (params != null) { 32 | params.forEach((k, v) -> sms.append(v)); 33 | } 34 | map.put(phone, sms.toString()); 35 | log.info("Mock短信 template:" + templateId + " phone:" + phone + " sms:" + sms); 36 | return SimpleResult.success(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/sms/handler/impl/aliyun/AliyunSmsHandler.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.sms.handler.impl.aliyun; 2 | 3 | import com.aliyuncs.DefaultAcsClient; 4 | import com.aliyuncs.IAcsClient; 5 | import com.aliyuncs.exceptions.ClientException; 6 | import com.aliyuncs.profile.DefaultProfile; 7 | import com.aliyuncs.profile.IClientProfile; 8 | import com.javabaas.server.common.entity.SimpleCode; 9 | import com.javabaas.server.common.entity.SimpleError; 10 | import com.javabaas.server.common.entity.SimpleResult; 11 | import com.javabaas.server.common.util.JSONUtil; 12 | import com.javabaas.server.config.entity.AppConfigEnum; 13 | import com.javabaas.server.config.service.AppConfigService; 14 | import com.javabaas.server.object.entity.BaasObject; 15 | import com.javabaas.server.sms.handler.ISmsHandler; 16 | import org.apache.commons.logging.Log; 17 | import org.apache.commons.logging.LogFactory; 18 | import org.springframework.beans.factory.annotation.Autowired; 19 | import org.springframework.stereotype.Component; 20 | import org.springframework.util.StringUtils; 21 | 22 | @Component("aliyun") 23 | public class AliyunSmsHandler implements ISmsHandler { 24 | 25 | private Log log = LogFactory.getLog(getClass()); 26 | @Autowired 27 | private AppConfigService appConfigService; 28 | @Autowired 29 | private JSONUtil jsonUtil; 30 | 31 | @Override 32 | public SimpleResult sendSms(String appId, String id, String phone, String signName, String templateId, BaasObject params) { 33 | try { 34 | //初始化acsClient 35 | IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKey(appId), secret(appId)); 36 | DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com"); 37 | IAcsClient acsClient = new DefaultAcsClient(profile); 38 | //组装请求对象 39 | SendSmsRequest request = new SendSmsRequest(); 40 | request.setPhoneNumbers(phone); 41 | request.setSignName(signName); 42 | request.setTemplateCode(templateId); 43 | request.setTemplateParam(jsonUtil.writeValueAsString(params)); 44 | //流水号 45 | request.setOutId(id); 46 | SendSmsResponse response = acsClient.getAcsResponse(request); 47 | if (response.getCode().equals("OK")) { 48 | //发送成功 49 | return SimpleResult.success(); 50 | } else { 51 | //发送失败 52 | log.warn("阿里云短信发送失败 code:" + response.getCode() + " message:" + response.getMessage()); 53 | switch (response.getCode()) { 54 | case "isv.AMOUNT_NOT_ENOUGH": 55 | return SimpleResult.error(SimpleCode.SMS_AMOUNT_NOT_ENOUGH); 56 | case "isv.MOBILE_NUMBER_ILLEGAL": 57 | return SimpleResult.error(SimpleCode.SMS_ILLEGAL_PHONE_NUMBER); 58 | case "isv.INVALID_PARAMETERS": 59 | return SimpleResult.error(SimpleCode.SMS_INVALID_PARAM); 60 | case "isv.TEMPLATE_MISSING_PARAMETERS": 61 | return SimpleResult.error(SimpleCode.SMS_TEMPLATE_MISSING_PARAMETERS); 62 | case "isv.BUSINESS_LIMIT_CONTROL": 63 | return SimpleResult.error(SimpleCode.SMS_LIMIT_CONTROL); 64 | default: 65 | return new SimpleResult(SimpleCode.SMS_OTHER_ERRORS.getCode(), response.getMessage()); 66 | } 67 | } 68 | } catch (ClientException e) { 69 | //客户端错误 70 | log.error(e, e); 71 | return SimpleResult.error(SimpleCode.SMS_SEND_ERROR); 72 | } 73 | } 74 | 75 | private String accessKey(String appId) { 76 | String ak = appConfigService.getString(appId, AppConfigEnum.SMS_HANDLER_ALIYUN_KEY); 77 | if (StringUtils.isEmpty(ak)) { 78 | throw new SimpleError(SimpleCode.SMS_NO_KEY); 79 | } 80 | return ak; 81 | } 82 | 83 | private String secret(String appId) { 84 | String sk = appConfigService.getString(appId, AppConfigEnum.SMS_HANDLER_ALIYUN_SECRET); 85 | if (StringUtils.isEmpty(sk)) { 86 | throw new SimpleError(SimpleCode.SMS_NO_SECRET); 87 | } 88 | return sk; 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/sms/handler/impl/aliyun/SendSmsRequest.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.sms.handler.impl.aliyun; 2 | 3 | import com.aliyuncs.RpcAcsRequest; 4 | 5 | public class SendSmsRequest 6 | extends RpcAcsRequest { 7 | private String outId; 8 | private String signName; 9 | private Long ownerId; 10 | private Long resourceOwnerId; 11 | private String templateCode; 12 | private String phoneNumbers; 13 | private String resourceOwnerAccount; 14 | private String templateParam; 15 | 16 | public SendSmsRequest() { 17 | super("Dysmsapi", "2017-05-25", "SendSms"); 18 | } 19 | 20 | public String getOutId() { 21 | return this.outId; 22 | } 23 | 24 | public void setOutId(String outId) { 25 | this.outId = outId; 26 | putQueryParameter("OutId", outId); 27 | } 28 | 29 | public String getSignName() { 30 | return this.signName; 31 | } 32 | 33 | public void setSignName(String signName) { 34 | this.signName = signName; 35 | putQueryParameter("SignName", signName); 36 | } 37 | 38 | public Long getOwnerId() { 39 | return this.ownerId; 40 | } 41 | 42 | public void setOwnerId(Long ownerId) { 43 | this.ownerId = ownerId; 44 | putQueryParameter("OwnerId", ownerId); 45 | } 46 | 47 | public Long getResourceOwnerId() { 48 | return this.resourceOwnerId; 49 | } 50 | 51 | public void setResourceOwnerId(Long resourceOwnerId) { 52 | this.resourceOwnerId = resourceOwnerId; 53 | putQueryParameter("ResourceOwnerId", resourceOwnerId); 54 | } 55 | 56 | public String getTemplateCode() { 57 | return this.templateCode; 58 | } 59 | 60 | public void setTemplateCode(String templateCode) { 61 | this.templateCode = templateCode; 62 | putQueryParameter("TemplateCode", templateCode); 63 | } 64 | 65 | public String getPhoneNumbers() { 66 | return this.phoneNumbers; 67 | } 68 | 69 | public void setPhoneNumbers(String phoneNumbers) { 70 | this.phoneNumbers = phoneNumbers; 71 | putQueryParameter("PhoneNumbers", phoneNumbers); 72 | } 73 | 74 | public String getResourceOwnerAccount() { 75 | return this.resourceOwnerAccount; 76 | } 77 | 78 | public void setResourceOwnerAccount(String resourceOwnerAccount) { 79 | this.resourceOwnerAccount = resourceOwnerAccount; 80 | putQueryParameter("ResourceOwnerAccount", resourceOwnerAccount); 81 | } 82 | 83 | public String getTemplateParam() { 84 | return this.templateParam; 85 | } 86 | 87 | public void setTemplateParam(String templateParam) { 88 | this.templateParam = templateParam; 89 | putQueryParameter("TemplateParam", templateParam); 90 | } 91 | 92 | public Class getResponseClass() { 93 | return SendSmsResponse.class; 94 | } 95 | } 96 | 97 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/sms/handler/impl/aliyun/SendSmsResponse.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.sms.handler.impl.aliyun; 2 | 3 | import com.aliyuncs.AcsResponse; 4 | import com.aliyuncs.transform.UnmarshallerContext; 5 | 6 | public class SendSmsResponse 7 | extends AcsResponse { 8 | private String requestId; 9 | private String bizId; 10 | private String code; 11 | private String message; 12 | 13 | public String getRequestId() { 14 | return this.requestId; 15 | } 16 | 17 | public void setRequestId(String requestId) { 18 | this.requestId = requestId; 19 | } 20 | 21 | public String getBizId() { 22 | return this.bizId; 23 | } 24 | 25 | public void setBizId(String bizId) { 26 | this.bizId = bizId; 27 | } 28 | 29 | public String getCode() { 30 | return this.code; 31 | } 32 | 33 | public void setCode(String code) { 34 | this.code = code; 35 | } 36 | 37 | public String getMessage() { 38 | return this.message; 39 | } 40 | 41 | public void setMessage(String message) { 42 | this.message = message; 43 | } 44 | 45 | public SendSmsResponse getInstance(UnmarshallerContext context) { 46 | return SendSmsResponseUnmarshaller.unmarshall(this, context); 47 | } 48 | } 49 | 50 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/sms/handler/impl/aliyun/SendSmsResponseUnmarshaller.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.sms.handler.impl.aliyun; 2 | 3 | import com.aliyuncs.transform.UnmarshallerContext; 4 | 5 | public class SendSmsResponseUnmarshaller { 6 | public static SendSmsResponse unmarshall(SendSmsResponse sendSmsResponse, UnmarshallerContext context) { 7 | sendSmsResponse.setRequestId(context.stringValue("SendSmsResponse.RequestId")); 8 | sendSmsResponse.setBizId(context.stringValue("SendSmsResponse.BizId")); 9 | sendSmsResponse.setCode(context.stringValue("SendSmsResponse.Code")); 10 | sendSmsResponse.setMessage(context.stringValue("SendSmsResponse.Message")); 11 | 12 | return sendSmsResponse; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/sms/service/SmsRateLimiter.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.sms.service; 2 | 3 | import com.javabaas.server.common.entity.SimpleCode; 4 | import com.javabaas.server.common.entity.SimpleError; 5 | import com.javabaas.server.config.entity.AppConfigEnum; 6 | import com.javabaas.server.config.service.AppConfigService; 7 | import com.javabaas.server.object.entity.BaasObject; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.data.redis.core.StringRedisTemplate; 10 | import org.springframework.data.redis.core.ValueOperations; 11 | import org.springframework.stereotype.Component; 12 | import org.springframework.util.StringUtils; 13 | 14 | import java.util.concurrent.TimeUnit; 15 | 16 | /** 17 | * 短信请求频度限制 18 | * Created by Codi on 2017/6/29. 19 | */ 20 | @Component 21 | public class SmsRateLimiter { 22 | 23 | @Autowired 24 | private AppConfigService appConfigService; 25 | @Autowired 26 | private StringRedisTemplate redisTemplate; 27 | 28 | public void rate(String appId, String phone, String templateId, BaasObject params) { 29 | sendIntervalLimit(appId, phone); 30 | } 31 | 32 | /** 33 | * 两次短信请求间隔限制 34 | */ 35 | private void sendIntervalLimit(String appId, String phone) { 36 | ValueOperations ops = redisTemplate.opsForValue(); 37 | Long sendInterval = appConfigService.getLong(appId, AppConfigEnum.SMS_SEND_INTERVAL); 38 | String key = "App_" + appId + "_SMS_SEND_INTERVAL_LIMIT_" + phone; 39 | String exist = ops.get(key); 40 | if (StringUtils.isEmpty(exist)) { 41 | //创建记录 42 | ops.set(key, "1", sendInterval, TimeUnit.SECONDS); 43 | } else { 44 | //时间间隔内 禁止连续发送 45 | SimpleError.e(SimpleCode.SMS_SEND_INTERVAL_LIMIT); 46 | } 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/user/controller/InstallationController.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.user.controller; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.javabaas.server.common.entity.SimpleError; 5 | import com.javabaas.server.common.entity.SimpleResult; 6 | import com.javabaas.server.user.entity.BaasInstallation; 7 | import com.javabaas.server.user.service.InstallationService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import java.io.IOException; 12 | 13 | /** 14 | * 注册设备 15 | * Created by Staryet on 15/8/13. 16 | */ 17 | @RestController 18 | @RequestMapping(value = "/api/installation") 19 | public class InstallationController { 20 | 21 | @Autowired 22 | private InstallationService installationService; 23 | @Autowired 24 | private ObjectMapper objectMapper; 25 | 26 | /** 27 | * 注册设备 28 | * 29 | * @param body 30 | * @return id 31 | * @throws SimpleError 32 | */ 33 | @RequestMapping(value = "", method = RequestMethod.POST) 34 | @ResponseBody 35 | public SimpleResult insert(@RequestHeader(value = "JB-AppId") String appId, 36 | @RequestHeader(value = "JB-Plat") String plat, 37 | @RequestBody String body) throws IOException { 38 | BaasInstallation installation = objectMapper.readValue(body, BaasInstallation.class); 39 | String id = installationService.register(appId, plat, installation); 40 | SimpleResult result = SimpleResult.success(); 41 | result.putData("_id", id); 42 | return result; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/user/entity/BaasAuth.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.user.entity; 2 | 3 | import com.javabaas.server.object.entity.BaasObject; 4 | 5 | import java.util.Map; 6 | 7 | /** 8 | * Created by Codi on 15/10/14. 9 | */ 10 | public class BaasAuth extends BaasObject { 11 | public BaasAuth() { 12 | 13 | } 14 | 15 | public BaasAuth(Map m) { 16 | super(m); 17 | } 18 | 19 | public void setAccessToken(String accessToken) { 20 | put("accessToken", accessToken); 21 | } 22 | 23 | public String getAccessToken() { 24 | return (String) get("accessToken"); 25 | } 26 | 27 | public String getUid() { 28 | return (String) get("uid"); 29 | } 30 | 31 | public void setUid(String uid) { 32 | put("uid", uid); 33 | } 34 | 35 | public String getOpenId() { 36 | return (String) get("openId"); 37 | } 38 | 39 | public void setOpenId(String openId) { 40 | put("openId", openId); 41 | } 42 | 43 | public String getUnionId() { 44 | return (String) get("unionId"); 45 | } 46 | 47 | public void setUnionId(String unionId) { 48 | put("unionId", unionId); 49 | } 50 | 51 | public String getEncryptedData() { 52 | return (String) get("encryptedData"); 53 | } 54 | 55 | public String getCode() { 56 | return (String) get("code"); 57 | } 58 | 59 | public String getIV() { 60 | return (String) get("iv"); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/user/entity/BaasInstallation.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.user.entity; 2 | 3 | import com.javabaas.server.object.entity.BaasObject; 4 | 5 | import java.util.Map; 6 | 7 | /** 8 | * Created by Staryet on 15/8/13. 9 | */ 10 | public class BaasInstallation extends BaasObject { 11 | 12 | public BaasInstallation() { 13 | } 14 | 15 | public BaasInstallation(Map m) { 16 | super(m); 17 | } 18 | 19 | public void setInstallationId(String installationId) { 20 | put("installationId", installationId); 21 | } 22 | 23 | public String getInstallationId() { 24 | return getString("installationId"); 25 | } 26 | 27 | public void setDeviceType(String deviceType) { 28 | put("deviceType", deviceType); 29 | } 30 | 31 | public String getDeviceType() { 32 | return getString("deviceType"); 33 | } 34 | 35 | public void setDeviceToken(String deviceToken) { 36 | put("deviceToken", deviceToken); 37 | } 38 | 39 | public String getDeviceToken() { 40 | return getString("deviceToken"); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/user/entity/BaasPhoneRegister.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.user.entity; 2 | 3 | import com.javabaas.server.object.entity.BaasObject; 4 | 5 | import javax.validation.constraints.NotEmpty; 6 | 7 | /** 8 | * 手机号注册对象 9 | * Created by Codi on 2017/6/26. 10 | */ 11 | public class BaasPhoneRegister extends BaasObject { 12 | 13 | public BaasPhoneRegister() { 14 | } 15 | 16 | @NotEmpty(message = "手机号不能为空") 17 | public String getPhone() { 18 | return getString("phone"); 19 | } 20 | 21 | public void setPhone(String phone) { 22 | put("phone", phone); 23 | } 24 | 25 | @NotEmpty(message = "验证码不能为空") 26 | public String getCode() { 27 | return getString("code"); 28 | } 29 | 30 | public void setCode(String code) { 31 | put("code", code); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/user/entity/BaasSnsRegister.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.user.entity; 2 | 3 | import com.javabaas.server.object.entity.BaasObject; 4 | 5 | import javax.validation.constraints.NotEmpty; 6 | 7 | /** 8 | * 社交平台注册登录 9 | * Created by Codi on 2017/7/11. 10 | */ 11 | public class BaasSnsRegister extends BaasObject { 12 | 13 | @NotEmpty(message = "授权信息不能为空") 14 | public BaasAuth getAuth() { 15 | BaasObject auth = getBaasObject("auth"); 16 | return auth == null ? null : new BaasAuth(auth); 17 | } 18 | 19 | public BaasUser getUser() { 20 | return getBaasUser("user"); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/user/entity/BaasSnsType.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.user.entity; 2 | 3 | /** 4 | * Created by test on 2017/6/12. 5 | */ 6 | public enum BaasSnsType { 7 | WEIBO(1, "weibo"), 8 | QQ(2, "qq"), 9 | WEIXIN(3, "wx"), 10 | WEBAPP(4, "wx"); 11 | 12 | private int code; 13 | private String value; 14 | 15 | BaasSnsType(int code, String value) { 16 | this.code = code; 17 | this.value = value; 18 | } 19 | 20 | public int getCode() { 21 | return this.code; 22 | } 23 | 24 | public String getValue() { 25 | return this.value; 26 | } 27 | 28 | public static BaasSnsType getType(int code) { 29 | for (BaasSnsType baasSnsType : BaasSnsType.values()) { 30 | if (baasSnsType.code == code) { 31 | return baasSnsType; 32 | } 33 | } 34 | return null; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return super.toString() + " code:" + this.code + " value:" + this.value; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/user/entity/BaasUser.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.user.entity; 2 | 3 | import com.javabaas.server.object.entity.BaasObject; 4 | 5 | import java.util.Map; 6 | 7 | /** 8 | * Created by Staryet on 15/8/13. 9 | */ 10 | public class BaasUser extends BaasObject { 11 | 12 | public BaasUser() { 13 | } 14 | 15 | public BaasUser(Map m) { 16 | super(m); 17 | } 18 | 19 | public void setUsername(String username) { 20 | put("username", username); 21 | } 22 | 23 | public String getUsername() { 24 | return (String) get("username"); 25 | } 26 | 27 | public void setPassword(String password) { 28 | put("password", password); 29 | } 30 | 31 | public String getPassword() { 32 | return (String) get("password"); 33 | } 34 | 35 | public void setSessionToken(String sessionToken) { 36 | put("sessionToken", sessionToken); 37 | } 38 | 39 | public void setAuth(BaasObject auth) { 40 | put("auth", auth); 41 | } 42 | 43 | public BaasObject getAuth() { 44 | return (BaasObject) get("auth"); 45 | } 46 | 47 | public String getSessionToken() { 48 | return (String) get("sessionToken"); 49 | } 50 | 51 | public void setPhone(String phone) { 52 | put("phone", phone); 53 | } 54 | 55 | public String getPhone() { 56 | return (String) get("phone"); 57 | } 58 | 59 | public String getEmail() { 60 | return (String) get("email"); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/user/service/InstallationService.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.user.service; 2 | 3 | import com.javabaas.server.common.entity.SimpleError; 4 | import com.javabaas.server.object.service.ObjectService; 5 | import com.javabaas.server.user.entity.BaasInstallation; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | /** 10 | * Created by Staryet on 15/8/13. 11 | */ 12 | @Service 13 | public class InstallationService { 14 | 15 | public static String INSTALLATION_CLASS_NAME = "_Installation"; 16 | @Autowired 17 | private ObjectService objectService; 18 | 19 | /** 20 | * 注册设备 21 | * 22 | * @param installation 设备信息 23 | * @return id 24 | * @throws SimpleError 25 | */ 26 | public String register(String appId, String plat, BaasInstallation installation) { 27 | //禁止设置ACL字段 28 | installation.remove("acl"); 29 | return objectService.insert(appId, plat, INSTALLATION_CLASS_NAME, installation, null, true).getId(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/user/util/AesCbcUtil.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.user.util; 2 | 3 | 4 | import org.apache.tomcat.util.codec.binary.Base64; 5 | import org.bouncycastle.jce.provider.BouncyCastleProvider; 6 | 7 | import javax.crypto.BadPaddingException; 8 | import javax.crypto.Cipher; 9 | import javax.crypto.IllegalBlockSizeException; 10 | import javax.crypto.NoSuchPaddingException; 11 | import javax.crypto.spec.IvParameterSpec; 12 | import javax.crypto.spec.SecretKeySpec; 13 | import java.io.UnsupportedEncodingException; 14 | import java.security.*; 15 | import java.security.spec.InvalidParameterSpecException; 16 | 17 | /** 18 | * Created by test on 2017/6/2. 19 | *

20 | * AES-128-CBC 加密方式 21 | * 注: 22 | * AES-128-CBC可以自己定义“密钥”和“偏移量“。 23 | * AES-128是jdk自动生成的“密钥”。 24 | */ 25 | public class AesCbcUtil { 26 | 27 | 28 | static { 29 | //BouncyCastle是一个开源的加解密解决方案,主页在http://www.bouncycastle.org/ 30 | Security.addProvider(new BouncyCastleProvider()); 31 | } 32 | 33 | /** 34 | * AES解密 35 | * 36 | * @param data //密文,被加密的数据 37 | * @param key //秘钥 38 | * @param iv //偏移量 39 | * @param encodingFormat //解密后的结果需要进行的编码 40 | * @return 41 | * @throws Exception 42 | */ 43 | public static String decrypt(String data, String key, String iv, String encodingFormat) throws Exception { 44 | // initialize(); 45 | 46 | //被加密的数据 47 | byte[] dataByte = Base64.decodeBase64(data); 48 | //加密秘钥 49 | byte[] keyByte = Base64.decodeBase64(key); 50 | //偏移量 51 | byte[] ivByte = Base64.decodeBase64(iv); 52 | 53 | 54 | try { 55 | Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); 56 | 57 | SecretKeySpec spec = new SecretKeySpec(keyByte, "AES"); 58 | 59 | AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES"); 60 | parameters.init(new IvParameterSpec(ivByte)); 61 | 62 | cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化 63 | 64 | byte[] resultByte = cipher.doFinal(dataByte); 65 | if (null != resultByte && resultByte.length > 0) { 66 | String result = new String(resultByte, encodingFormat); 67 | return result; 68 | } 69 | return null; 70 | } catch (NoSuchAlgorithmException e) { 71 | e.printStackTrace(); 72 | } catch (NoSuchPaddingException e) { 73 | e.printStackTrace(); 74 | } catch (InvalidParameterSpecException e) { 75 | e.printStackTrace(); 76 | } catch (InvalidKeyException e) { 77 | e.printStackTrace(); 78 | } catch (InvalidAlgorithmParameterException e) { 79 | e.printStackTrace(); 80 | } catch (IllegalBlockSizeException e) { 81 | e.printStackTrace(); 82 | } catch (BadPaddingException e) { 83 | e.printStackTrace(); 84 | } catch (UnsupportedEncodingException e) { 85 | e.printStackTrace(); 86 | } 87 | 88 | return null; 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/com/javabaas/server/user/util/UUID.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server.user.util; 2 | 3 | /** 4 | * Created by Codi on 2017/7/4. 5 | */ 6 | public class UUID { 7 | 8 | public static String uuid() { 9 | return java.util.UUID.randomUUID().toString().replace("-", ""); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Spring相关配置 2 | spring.application.name=JavaBaas 3 | #数据库环境配置 4 | spring.data.mongodb.database=baas 5 | #控制器配置 6 | spring.mvc.view.prefix=/ 7 | spring.mvc.view.suffix=.html 8 | #日志级别 9 | logging.level.org.springframework.web.servlet.mvc.method.annotation=INFO 10 | logging.level.com.javabaas.server=debug 11 | #EndPoint 12 | management.server.servlet.context-path=/manage 13 | spring.jmx.enabled=false 14 | management.endpoint.health.cache.time-to-live=1000 15 | #JavaBaas配置 16 | #回调地址 17 | baas.host=${host:127.0.0.1} 18 | #用户鉴权 19 | baas.auth.key=${key:JavaBaas} 20 | baas.auth.timeout=${timeout:60000000} 21 | #七牛云存储 22 | baas.file.handler.qiniu.ak= 23 | baas.file.handler.qiniu.sk= 24 | baas.file.handler.qiniu.bucket= 25 | baas.file.handler.qiniu.url= 26 | #极光推送 27 | baas.push.handler.jpush.key= 28 | baas.push.handler.jpush.secret= -------------------------------------------------------------------------------- /src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | ${AnsiColor.BRIGHT_GREEN} 2 | ___ ______ 3 | |_ | | ___ \ 4 | | | __ _ __ __ __ _ | |_/ / __ _ __ _ ___ 5 | | | / _` |\ \ / // _` || ___ \ / _` | / _` |/ __| 6 | /\__/ /| (_| | \ V /| (_| || |_/ /| (_| || (_| |\__ \ 7 | \____/ \__,_| \_/ \__,_|\____/ \__,_| \__,_||___/ 8 | ${AnsiColor.BRIGHT_RED} :: ${spring.application.name} :: ${AnsiColor.BRIGHT_YELLOW} ${application.formatted-version} -------------------------------------------------------------------------------- /src/main/resources/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaBaas/JavaBaasServer/460c5831be825fa8485977fa62dffc6bbc4b1d81/src/main/resources/favicon.ico -------------------------------------------------------------------------------- /src/main/resources/static/css/reset.css: -------------------------------------------------------------------------------- 1 | a,abbr,acronym,address,applet,article,aside,audio,b,big,blockquote,body,canvas,caption,center,cite,code,dd,del,details,dfn,div,dl,dt,em,embed,fieldset,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,header,hgroup,html,i,iframe,img,ins,kbd,label,legend,li,mark,menu,nav,object,ol,output,p,pre,q,ruby,s,samp,section,small,span,strike,strong,sub,summary,sup,table,tbody,td,tfoot,th,thead,time,tr,tt,u,ul,var,video{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline}article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:after,blockquote:before,q:after,q:before{content:'';content:none}table{border-collapse:collapse;border-spacing:0} -------------------------------------------------------------------------------- /src/main/resources/static/css/style.css: -------------------------------------------------------------------------------- 1 | .swagger-section #header a#logo{font-size:1.5em;font-weight:700;text-decoration:none;padding:20px 0 20px 40px}#text-head{font-size:80px;font-family:Roboto,sans-serif;color:#fff;float:right;margin-right:20%}.navbar-fixed-top .navbar-brand,.navbar-fixed-top .navbar-nav,.navbar-header{height:auto}.navbar-inverse{background-color:#000;border-color:#000}#navbar-brand{margin-left:20%}.navtext{font-size:10px}.h1,h1{font-size:60px}.navbar-default .navbar-header .navbar-brand{color:#a2dfee}.swagger-section .swagger-ui-wrap ul#resources li.resource div.heading h2 a{color:#393939;font-family:Arvo,serif;font-size:1.5em}.swagger-section .swagger-ui-wrap ul#resources li.resource div.heading h2 a:hover{color:#000}.swagger-section .swagger-ui-wrap ul#resources li.resource div.heading h2{color:#525252;padding-left:0;display:block;clear:none;float:left;font-family:Arvo,serif;font-weight:700}.navbar-default .navbar-collapse,.navbar-default .navbar-form{border-color:#0a0a0a}.container1{width:1500px;margin:auto;margin-top:0;background-repeat:no-repeat;background-position:-40px -20px;margin-bottom:210px}.container-inner{width:1200px;margin:auto;background-color:hsla(192,8%,88%,.75);padding-bottom:40px;padding-top:40px;border-radius:15px}.header-content{padding:0;width:1000px}.title1{font-size:80px;font-family:Vollkorn,serif;color:#404040;text-align:center;padding-top:40px;padding-bottom:100px}#icon{margin-top:-18px}.subtext{font-size:25px;font-style:italic;color:#08b;text-align:right;padding-right:250px}.bg-primary{background-color:#00468b}.navbar-default .nav>li>a,.navbar-default .nav>li>a:focus,.navbar-default .nav>li>a:focus:hover,.navbar-default .nav>li>a:hover{color:#08b}.text-faded{font-size:25px;font-family:Vollkorn,serif}.section-heading{font-family:Vollkorn,serif;font-size:45px;padding-bottom:10px}hr{border-color:#00468b;padding-bottom:10px}.description{margin-top:20px;padding-bottom:200px}.description li{font-family:Vollkorn,serif;font-size:25px;color:#525252;margin-left:28%;padding-top:5px}.gap{margin-top:200px}.troubleshootingtext{color:hsla(0,0%,100%,.7);padding-left:30%}.troubleshootingtext li{list-style-type:circle;font-size:25px;padding-bottom:5px}.overlay{position:absolute;top:0;left:0;width:100%;height:100%;z-index:1}.block.response_body.json:hover{cursor:pointer}.backdrop{color:blue}#myModal{height:100%}.modal-backdrop{bottom:0;position:fixed}.curl{padding:10px;font-family:Anonymous Pro,Menlo,Consolas,Bitstream Vera Sans Mono,Courier New,monospace;font-size:.9em;max-height:400px;margin-top:5px;overflow-y:auto;background-color:#fcf6db;border:1px solid #e5e0c6;border-radius:4px}.curl_title{font-size:1.1em;margin:0;padding:15px 0 5px;font-family:Open Sans,Helvetica Neue,Arial,sans-serif;font-weight:500;line-height:1.1}.footer{display:none}.swagger-section .swagger-ui-wrap h2{padding:0}h2{margin:0;margin-bottom:5px}.markdown p,.swagger-section .swagger-ui-wrap .code{font-size:15px;font-family:Arvo,serif}.swagger-section .swagger-ui-wrap b{font-family:Arvo,serif}#signin:hover{cursor:pointer}.dropdown-menu{padding:15px}.navbar-right .dropdown-menu{left:0;right:auto}#signinbutton{width:100%;height:32px;font-size:13px;font-weight:700;color:#08b}.navbar-default .nav>li .details{color:#000;text-transform:none;font-size:15px;font-weight:400;font-family:Open Sans,sans-serif;font-style:italic;line-height:20px;top:-2px}.navbar-default .nav>li .details:hover{color:#000}#signout{width:100%;height:32px;font-size:13px;font-weight:700;color:#08b} -------------------------------------------------------------------------------- /src/main/resources/static/css/typography.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaBaas/JavaBaasServer/460c5831be825fa8485977fa62dffc6bbc4b1d81/src/main/resources/static/css/typography.css -------------------------------------------------------------------------------- /src/main/resources/static/fonts/DroidSans-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaBaas/JavaBaasServer/460c5831be825fa8485977fa62dffc6bbc4b1d81/src/main/resources/static/fonts/DroidSans-Bold.ttf -------------------------------------------------------------------------------- /src/main/resources/static/fonts/DroidSans.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaBaas/JavaBaasServer/460c5831be825fa8485977fa62dffc6bbc4b1d81/src/main/resources/static/fonts/DroidSans.ttf -------------------------------------------------------------------------------- /src/main/resources/static/fonts/element-icons.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaBaas/JavaBaasServer/460c5831be825fa8485977fa62dffc6bbc4b1d81/src/main/resources/static/fonts/element-icons.woff -------------------------------------------------------------------------------- /src/main/resources/static/images/collapse.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaBaas/JavaBaasServer/460c5831be825fa8485977fa62dffc6bbc4b1d81/src/main/resources/static/images/collapse.gif -------------------------------------------------------------------------------- /src/main/resources/static/images/expand.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaBaas/JavaBaasServer/460c5831be825fa8485977fa62dffc6bbc4b1d81/src/main/resources/static/images/expand.gif -------------------------------------------------------------------------------- /src/main/resources/static/images/explorer_icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaBaas/JavaBaasServer/460c5831be825fa8485977fa62dffc6bbc4b1d81/src/main/resources/static/images/explorer_icons.png -------------------------------------------------------------------------------- /src/main/resources/static/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaBaas/JavaBaasServer/460c5831be825fa8485977fa62dffc6bbc4b1d81/src/main/resources/static/images/logo.png -------------------------------------------------------------------------------- /src/main/resources/static/images/pet_store_api.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaBaas/JavaBaasServer/460c5831be825fa8485977fa62dffc6bbc4b1d81/src/main/resources/static/images/pet_store_api.png -------------------------------------------------------------------------------- /src/main/resources/static/images/throbber.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaBaas/JavaBaasServer/460c5831be825fa8485977fa62dffc6bbc4b1d81/src/main/resources/static/images/throbber.gif -------------------------------------------------------------------------------- /src/main/resources/static/images/wordnik_api.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaBaas/JavaBaasServer/460c5831be825fa8485977fa62dffc6bbc4b1d81/src/main/resources/static/images/wordnik_api.png -------------------------------------------------------------------------------- /src/main/resources/static/lang/translator.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * Translator for documentation pages. 5 | * 6 | * To enable translation you should include one of language-files in your explorer.html 7 | * after . 8 | * For example - 9 | * 10 | * If you wish to translate some new texts you should do two things: 11 | * 1. Add a new phrase pair ("New Phrase": "New Translation") into your language file (for example lang/ru.js). It will be great if you add it in other language files too. 12 | * 2. Mark that text it templates this way New Phrase or . 13 | * The main thing here is attribute data-sw-translate. Only inner html, title-attribute and value-attribute are going to translate. 14 | * 15 | */ 16 | window.SwaggerTranslator = { 17 | 18 | _words:[], 19 | 20 | translate: function(sel) { 21 | var $this = this; 22 | sel = sel || '[data-sw-translate]'; 23 | 24 | $(sel).each(function() { 25 | $(this).html($this._tryTranslate($(this).html())); 26 | 27 | $(this).val($this._tryTranslate($(this).val())); 28 | $(this).attr('title', $this._tryTranslate($(this).attr('title'))); 29 | }); 30 | }, 31 | 32 | _tryTranslate: function(word) { 33 | return this._words[$.trim(word)] !== undefined ? this._words[$.trim(word)] : word; 34 | }, 35 | 36 | learn: function(wordsMap) { 37 | this._words = wordsMap; 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /src/main/resources/static/lang/zh-cn.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /* jshint quotmark: double */ 4 | window.SwaggerTranslator.learn({ 5 | "Warning: Deprecated": "警告:已过时", 6 | "Implementation Notes": "实现备注", 7 | "Response Class": "响应类", 8 | "Status": "状态", 9 | "Parameters": "参数", 10 | "Parameter": "参数", 11 | "Value": "值", 12 | "Description": "描述", 13 | "Parameter Type": "参数类型", 14 | "Data Type": "数据类型", 15 | "Response Messages": "响应消息", 16 | "HTTP Status Code": "HTTP状态码", 17 | "Reason": "原因", 18 | "Response Model": "响应模型", 19 | "Request URL": "请求URL", 20 | "Response Body": "响应体", 21 | "Response Code": "响应码", 22 | "Response Headers": "响应头", 23 | "Hide Response": "隐藏响应", 24 | "Headers": "头", 25 | "Try it out!": "测试", 26 | "Show/Hide": "显示/隐藏", 27 | "List Operations": "显示操作", 28 | "Expand Operations": "展开操作", 29 | "Raw": "原始", 30 | "can't parse JSON. Raw result": "无法解析JSON. 原始结果", 31 | "Example Value": "示例", 32 | "Click to set as parameter value": "点击设置参数", 33 | "Model Schema": "模型架构", 34 | "Model": "模型", 35 | "apply": "应用", 36 | "Username": "用户名", 37 | "Password": "密码", 38 | "Terms of service": "服务条款", 39 | "Created by": "创建者", 40 | "See more at": "查看更多:", 41 | "Contact the developer": "联系开发者", 42 | "api version": "api版本", 43 | "Response Content Type": "响应Content Type", 44 | "Parameter content type:": "参数类型:", 45 | "fetching resource": "正在获取资源", 46 | "fetching resource list": "正在获取资源列表", 47 | "Explore": "浏览", 48 | "Show Swagger Petstore Example Apis": "显示 Swagger Petstore 示例 Apis", 49 | "Can't read from server. It may not have the appropriate access-control-origin settings.": "无法从服务器读取。可能没有正确设置access-control-origin。", 50 | "Please specify the protocol for": "请指定协议:", 51 | "Can't read swagger JSON from": "无法读取swagger JSON于", 52 | "Finished Loading Resource Information. Rendering Swagger UI": "已加载资源信息。正在渲染Swagger UI", 53 | "Unable to read api": "无法读取api", 54 | "from path": "从路径", 55 | "server returned": "服务器返回" 56 | }); 57 | -------------------------------------------------------------------------------- /src/main/resources/static/lib/highlight.9.1.0.pack_extended.js: -------------------------------------------------------------------------------- 1 | "use strict";!function(){var h,l;h=hljs.configure,hljs.configure=function(l){var i=l.highlightSizeThreshold;hljs.highlightSizeThreshold=i===+i?i:null,h.call(this,l)},l=hljs.highlightBlock,hljs.highlightBlock=function(h){var i=h.innerHTML,g=hljs.highlightSizeThreshold;(null==g||g>i.length)&&l.call(hljs,h)}}(); -------------------------------------------------------------------------------- /src/main/resources/static/lib/jquery.ba-bbq.min.js: -------------------------------------------------------------------------------- 1 | !function(e,t){function n(e){return"string"==typeof e}function r(e){var t=g.call(arguments,1);return function(){return e.apply(this,t.concat(g.call(arguments)))}}function o(e){return e.replace(/^[^#]*#?(.*)$/,"$1")}function a(e){return e.replace(/(?:^[^?#]*\?([^#]*).*$)?.*/,"$1")}function i(r,o,a,i,c){var u,s,p,h,d;return i!==f?(p=a.match(r?/^([^#]*)\#?(.*)$/:/^([^#?]*)\??([^#]*)(#?.*)/),d=p[3]||"",2===c&&n(i)?s=i.replace(r?R:E,""):(h=l(p[2]),i=n(i)?l[r?A:w](i):i,s=2===c?i:1===c?e.extend({},i,h):e.extend({},h,i),s=b(s),r&&(s=s.replace(m,y))),u=p[1]+(r?"#":s||!p[1]?"?":"")+s+d):u=o(a!==f?a:t[S][q]),u}function c(e,t,r){return t===f||"boolean"==typeof t?(r=t,t=b[e?A:w]()):t=n(t)?t.replace(e?R:E,""):t,l(t,r)}function u(t,r,o,a){return n(o)||"object"==typeof o||(a=o,o=r,r=f),this.each(function(){var n=e(this),i=r||v()[(this.nodeName||"").toLowerCase()]||"",c=i&&n.attr(i)||"";n.attr(i,b[t](c,o,a))})}var f,s,l,p,h,d,v,m,g=Array.prototype.slice,y=decodeURIComponent,b=e.param,$=e.bbq=e.bbq||{},x=e.event.special,j="hashchange",w="querystring",A="fragment",N="elemUrlAttr",S="location",q="href",C="src",E=/^.*\?|#.*$/g,R=/^.*\#/,U={};b[w]=r(i,0,a),b[A]=s=r(i,1,o),s.noEscape=function(t){t=t||"";var n=e.map(t.split(""),encodeURIComponent);m=new RegExp(n.join("|"),"g")},s.noEscape(",/"),e.deparam=l=function(t,n){var r={},o={"true":!0,"false":!1,"null":null};return e.each(t.replace(/\+/g," ").split("&"),function(t,a){var i,c=a.split("="),u=y(c[0]),s=r,l=0,p=u.split("]["),h=p.length-1;if(/\[/.test(p[0])&&/\]$/.test(p[h])?(p[h]=p[h].replace(/\]$/,""),p=p.shift().split("[").concat(p),h=p.length-1):h=0,2===c.length)if(i=y(c[1]),n&&(i=i&&!isNaN(i)?+i:"undefined"===i?f:o[i]!==f?o[i]:i),h)for(;l<=h;l++)u=""===p[l]?s.length:p[l],s=s[u]=l').hide().insertAfter("body")[0].contentWindow,s=function(){return r(a.document[i][u])},(f=function(e,t){if(e!==t){var n=a.document;n.open().close(),n[i].hash="#"+e}})(r()))}var o,a,f,s,p={};return p.start=function(){if(!o){var a=r();f||n(),function l(){var n=r(),p=s(a);n!==a?(f(a=n,p),e(t).trigger(c)):p!==a&&(t[i][u]=t[i][u].replace(/#.*/,"")+"#"+p),o=setTimeout(l,e[c+"Delay"])}()}},p.stop=function(){a||(o&&clearTimeout(o),o=0)},p}()}(jQuery,this); -------------------------------------------------------------------------------- /src/main/resources/static/lib/jquery.slideto.min.js: -------------------------------------------------------------------------------- 1 | !function(i){i.fn.slideto=function(o){return o=i.extend({slide_duration:"slow",highlight_duration:3e3,highlight:!0,highlight_color:"#FFFF99"},o),this.each(function(){obj=i(this),i("body").animate({scrollTop:obj.offset().top},o.slide_duration,function(){o.highlight&&i.ui.version&&obj.effect("highlight",{color:o.highlight_color},o.highlight_duration)})})}}(jQuery); -------------------------------------------------------------------------------- /src/main/resources/static/lib/jquery.wiggle.min.js: -------------------------------------------------------------------------------- 1 | jQuery.fn.wiggle=function(e){var a={speed:50,wiggles:3,travel:5,callback:null},e=jQuery.extend(a,e);return this.each(function(){var a=this,l=(jQuery(this).wrap('

').css("position","relative"),0);for(i=1;i<=e.wiggles;i++)jQuery(this).animate({left:"-="+e.travel},e.speed).animate({left:"+="+2*e.travel},2*e.speed).animate({left:"-="+e.travel},e.speed,function(){l++,jQuery(a).parent().hasClass("wiggle-wrap")&&jQuery(a).parent().replaceWith(a),l==e.wiggles&&jQuery.isFunction(e.callback)&&e.callback()})})}; -------------------------------------------------------------------------------- /src/main/resources/static/lib/object-assign-pollyfill.js: -------------------------------------------------------------------------------- 1 | "function"!=typeof Object.assign&&!function(){Object.assign=function(n){"use strict";if(void 0===n||null===n)throw new TypeError("Cannot convert undefined or null to object");for(var t=Object(n),o=1;o 2 | var qp = null; 3 | if(/code|token|error/.test(window.location.hash)) { 4 | qp = location.hash.substring(1); 5 | } 6 | else { 7 | qp = location.search.substring(1); 8 | } 9 | qp = qp ? JSON.parse('{"' + qp.replace(/&/g, '","').replace(/=/g,'":"') + '"}', 10 | function(key, value) { 11 | return key===""?value:decodeURIComponent(value) } 12 | ):{} 13 | 14 | if (window.opener.swaggerUiAuth.tokenUrl) 15 | window.opener.processOAuthCode(qp); 16 | else 17 | window.opener.onOAuthComplete(qp); 18 | 19 | window.close(); 20 | 21 | -------------------------------------------------------------------------------- /src/test/java/com/javabaas/server/AppConfigTests.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server; 2 | 3 | import com.javabaas.server.admin.entity.App; 4 | import com.javabaas.server.admin.service.AppService; 5 | import com.javabaas.server.config.entity.AppConfigEnum; 6 | import com.javabaas.server.config.service.AppConfigService; 7 | import org.junit.After; 8 | import org.junit.Assert; 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.boot.test.context.SpringBootTest; 14 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 15 | 16 | import static org.hamcrest.Matchers.equalTo; 17 | 18 | /** 19 | * 应用配置测试 20 | * Created by Codi on 2017/7/10. 21 | */ 22 | @RunWith(SpringJUnit4ClassRunner.class) 23 | @SpringBootTest(classes = Main.class) 24 | public class AppConfigTests { 25 | 26 | @Autowired 27 | private AppService appService; 28 | @Autowired 29 | private AppConfigService appConfigService; 30 | private String appId; 31 | 32 | @Before 33 | public void before() { 34 | appService.deleteByAppName("AppConfigTestApp"); 35 | App app = new App(); 36 | app.setName("AppConfigTestApp"); 37 | appService.insert(app); 38 | appId = app.getId(); 39 | } 40 | 41 | @After 42 | public void after() { 43 | appService.delete(appId); 44 | } 45 | 46 | @Test 47 | public void testSetConfig() { 48 | appConfigService.setConfig(appId, AppConfigEnum.SMS_HANDLER, "aliyun"); 49 | String smsHandler = appConfigService.getString(appId, AppConfigEnum.SMS_HANDLER); 50 | Assert.assertThat(smsHandler, equalTo("aliyun")); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/com/javabaas/server/ClazzTests.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server; 2 | 3 | import com.javabaas.server.admin.entity.App; 4 | import com.javabaas.server.admin.entity.Clazz; 5 | import com.javabaas.server.admin.entity.Field; 6 | import com.javabaas.server.admin.entity.FieldType; 7 | import com.javabaas.server.admin.service.AppService; 8 | import com.javabaas.server.admin.service.ClazzService; 9 | import com.javabaas.server.admin.service.FieldService; 10 | import com.javabaas.server.common.entity.SimpleCode; 11 | import com.javabaas.server.common.entity.SimpleError; 12 | import org.junit.After; 13 | import org.junit.Assert; 14 | import org.junit.Before; 15 | import org.junit.Test; 16 | import org.junit.runner.RunWith; 17 | import org.springframework.beans.factory.annotation.Autowired; 18 | import org.springframework.boot.test.context.SpringBootTest; 19 | import org.springframework.test.context.junit4.SpringRunner; 20 | 21 | import java.util.List; 22 | 23 | import static org.hamcrest.Matchers.equalTo; 24 | 25 | /** 26 | * Created by Staryet on 15/8/11. 27 | */ 28 | @RunWith(SpringRunner.class) 29 | @SpringBootTest(classes = Main.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK) 30 | public class ClazzTests { 31 | 32 | @Autowired 33 | private AppService appService; 34 | @Autowired 35 | private ClazzService clazzService; 36 | @Autowired 37 | private FieldService fieldService; 38 | 39 | private App app; 40 | 41 | @Before 42 | public void before() { 43 | appService.deleteByAppName("ClazzTest"); 44 | app = new App(); 45 | app.setName("ClazzTest"); 46 | appService.insert(app); 47 | app = appService.get(app.getId()); 48 | } 49 | 50 | @After 51 | public void after() { 52 | appService.delete(app.getId()); 53 | } 54 | 55 | @Test 56 | public void testInsert() { 57 | Clazz clazz1 = new Clazz(); 58 | clazz1.setName("Clazz1"); 59 | clazz1.setApp(app); 60 | clazzService.insert(app.getId(), clazz1); 61 | clazz1 = clazzService.get(app.getId(), "Clazz1"); 62 | Assert.assertThat(clazz1.getName(), equalTo("Clazz1")); 63 | 64 | //测试类名称错误 65 | Clazz clazz2 = new Clazz(); 66 | clazz2.setName("_Clazz2"); 67 | clazz2.setApp(app); 68 | try { 69 | clazzService.insert(app.getId(), clazz2); 70 | Assert.fail(); 71 | } catch (SimpleError error) { 72 | Assert.assertThat(error.getCode(), equalTo(SimpleCode.CLAZZ_NAME_ERROR.getCode())); 73 | } 74 | } 75 | 76 | @Test 77 | public void testDelete() { 78 | //创建类 79 | Clazz clazzForDelete = new Clazz(); 80 | clazzForDelete.setName("ClazzForDelete"); 81 | clazzForDelete.setApp(app); 82 | clazzService.insert(app.getId(), clazzForDelete); 83 | clazzForDelete = clazzService.get(app.getId(), "ClazzForDelete"); 84 | //添加字段 85 | fieldService.insert(app.getId(), "ClazzForDelete", new Field(FieldType.STRING, "field1")); 86 | fieldService.insert(app.getId(), "ClazzForDelete", new Field(FieldType.STRING, "field2")); 87 | fieldService.insert(app.getId(), "ClazzForDelete", new Field(FieldType.STRING, "field3")); 88 | //验证创建成功 89 | Assert.assertThat(clazzForDelete.getName(), equalTo("ClazzForDelete")); 90 | List fields = fieldService.list(app.getId(), "ClazzForDelete"); 91 | Assert.assertThat(fields.size(), equalTo(3)); 92 | 93 | //删除类 94 | clazzService.delete(app.getId(), "ClazzForDelete"); 95 | 96 | //验证删除成功 97 | try { 98 | clazzService.get(app.getId(), "ClazzForDelete"); 99 | Assert.fail(); 100 | } catch (SimpleError e) { 101 | Assert.assertThat(e.getCode(), equalTo(SimpleCode.CLAZZ_NOT_FOUND.getCode())); 102 | } 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /src/test/java/com/javabaas/server/FieldTests.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server; 2 | 3 | import com.javabaas.server.admin.entity.App; 4 | import com.javabaas.server.admin.entity.Clazz; 5 | import com.javabaas.server.admin.entity.Field; 6 | import com.javabaas.server.admin.entity.FieldType; 7 | import com.javabaas.server.admin.service.AppService; 8 | import com.javabaas.server.admin.service.ClazzService; 9 | import com.javabaas.server.admin.service.FieldService; 10 | import com.javabaas.server.common.entity.SimpleCode; 11 | import com.javabaas.server.common.entity.SimpleError; 12 | import org.junit.After; 13 | import org.junit.Assert; 14 | import org.junit.Before; 15 | import org.junit.Test; 16 | import org.junit.runner.RunWith; 17 | import org.springframework.beans.factory.annotation.Autowired; 18 | import org.springframework.boot.test.context.SpringBootTest; 19 | import org.springframework.test.context.junit4.SpringRunner; 20 | 21 | import static org.hamcrest.Matchers.equalTo; 22 | import static org.hamcrest.Matchers.notNullValue; 23 | 24 | /** 25 | * Created by Staryet on 15/8/11. 26 | */ 27 | @RunWith(SpringRunner.class) 28 | @SpringBootTest(classes = Main.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK) 29 | public class FieldTests { 30 | 31 | @Autowired 32 | private AppService appService; 33 | @Autowired 34 | private ClazzService clazzService; 35 | @Autowired 36 | private FieldService fieldService; 37 | 38 | private App app; 39 | 40 | @Before 41 | public void before() { 42 | appService.deleteByAppName("FieldTestApp"); 43 | app = new App(); 44 | app.setName("FieldTestApp"); 45 | appService.insert(app); 46 | 47 | Clazz clazz = new Clazz("Test"); 48 | clazzService.insert(app.getId(), clazz); 49 | } 50 | 51 | @After 52 | public void after() { 53 | appService.delete(app.getId()); 54 | } 55 | 56 | /** 57 | * 测试字段的创建 58 | * 59 | * @throws SimpleError 60 | */ 61 | @Test 62 | public void testInsert() { 63 | Field fieldString = new Field(FieldType.STRING, "string"); 64 | fieldService.insert(app.getId(), "Test", fieldString); 65 | 66 | Field field = fieldService.get(app.getId(), "Test", "string"); 67 | Assert.assertThat(field, notNullValue()); 68 | 69 | //测试禁止使用保留字创建Field 70 | Field fieldError = new Field(FieldType.STRING, "_aaa"); 71 | try { 72 | fieldService.insert(app.getId(), "Test", fieldError); 73 | Assert.fail("禁止使用保留子创建字段"); 74 | } catch (SimpleError error) { 75 | Assert.assertThat(error.getCode(), equalTo(SimpleCode.FIELD_NAME_ERROR.getCode())); 76 | } 77 | } 78 | 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/test/java/com/javabaas/server/FileTests.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server; 2 | 3 | import com.javabaas.server.admin.entity.App; 4 | import com.javabaas.server.admin.service.AppService; 5 | import com.javabaas.server.common.util.JSONUtil; 6 | import com.javabaas.server.file.entity.BaasFile; 7 | import com.javabaas.server.file.entity.FileStoragePlatform; 8 | import com.javabaas.server.file.service.FileService; 9 | import org.junit.After; 10 | import org.junit.Assert; 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | import org.junit.runner.RunWith; 14 | import org.springframework.beans.factory.annotation.Autowired; 15 | import org.springframework.boot.test.context.SpringBootTest; 16 | import org.springframework.test.context.junit4.SpringRunner; 17 | 18 | import java.util.HashMap; 19 | import java.util.Map; 20 | 21 | import static org.hamcrest.Matchers.*; 22 | 23 | /** 24 | * Created by Staryet on 15/8/11. 25 | */ 26 | @RunWith(SpringRunner.class) 27 | @SpringBootTest(classes = Main.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK) 28 | public class FileTests { 29 | 30 | @Autowired 31 | private FileService fileService; 32 | @Autowired 33 | private AppService appService; 34 | private FileStoragePlatform platform = FileStoragePlatform.Test; 35 | @Autowired 36 | private JSONUtil jsonUtil; 37 | private App app; 38 | 39 | @Before 40 | public void before() { 41 | appService.deleteByAppName("ObjectTestApp"); 42 | app = new App(); 43 | app.setName("ObjectTestApp"); 44 | appService.insert(app); 45 | } 46 | 47 | @After 48 | public void after() { 49 | appService.delete(app.getId()); 50 | } 51 | 52 | @Test 53 | public void testInsert() { 54 | BaasFile file = new BaasFile(); 55 | file.setName("baidu"); 56 | String url = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superplus/img/logo_white_ee663702.png"; 57 | file.setUrl(url); 58 | BaasFile baasFile = fileService.saveFile(app.getId(), "admin", file); 59 | 60 | file = fileService.getFile(app.getId(), "admin", baasFile.getId()); 61 | Assert.assertThat(file.getName(), equalTo("baidu")); 62 | Assert.assertThat(file.getUrl(), equalTo(url)); 63 | Assert.assertThat(file.getId(), not(nullValue())); 64 | } 65 | 66 | @Test 67 | public void testUpload() { 68 | //模拟上传回调操作 69 | Map callbackParams = new HashMap<>(); 70 | callbackParams.put("name", "fileTest"); 71 | callbackParams.put("app", app.getId()); 72 | callbackParams.put("plat", "admin"); 73 | String fileId = fileService.callback(FileStoragePlatform.Test, jsonUtil.writeValueAsString(callbackParams), null).getId(); 74 | 75 | //检查文件是否保存成功 76 | BaasFile file = fileService.getFile(app.getId(), "admin", fileId); 77 | Assert.assertThat(file.getId(), equalTo(fileId)); 78 | } 79 | 80 | @Test 81 | public void testFetch() { 82 | BaasFile file = new BaasFile(); 83 | file.setName("file"); 84 | file.setUrl("http://7xnus0.com2.z0.glb.qiniucdn.com/56308742a290aa5006f2b0d6/83debfaee6b040e6bbdd1fdb3e11a84d"); 85 | String fileId = fileService.saveFileWithFetch(app.getId(), "admin", platform, file, null).getId(); 86 | //检查文件是否保存成功 87 | file = fileService.getFile(app.getId(), "admin", fileId); 88 | Assert.assertThat(file.getId(), equalTo(fileId)); 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /src/test/java/com/javabaas/server/PushTests.java: -------------------------------------------------------------------------------- 1 | package com.javabaas.server; 2 | 3 | import com.javabaas.server.admin.entity.App; 4 | import com.javabaas.server.admin.service.AppService; 5 | import com.javabaas.server.push.service.PushService; 6 | import org.junit.After; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.test.context.junit4.SpringRunner; 13 | 14 | /** 15 | * 测试推送 16 | */ 17 | @RunWith(SpringRunner.class) 18 | @SpringBootTest(classes = Main.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK) 19 | public class PushTests { 20 | 21 | @Autowired 22 | private AppService appService; 23 | @Autowired 24 | private PushService pushService; 25 | 26 | private App app; 27 | 28 | @Before 29 | public void before() { 30 | appService.deleteByAppName("PushTestApp"); 31 | app = new App(); 32 | app.setName("PushTestApp"); 33 | appService.insert(app); 34 | } 35 | 36 | @After 37 | public void after() { 38 | appService.delete(app.getId()); 39 | } 40 | 41 | @Test 42 | public void testPush() { 43 | 44 | } 45 | 46 | } 47 | --------------------------------------------------------------------------------