main
xingzi 2 years ago
parent 3641bba15a
commit 3cefdee896

@ -0,0 +1,9 @@
FROM openjdk:8u272-jdk
RUN apt-get update && apt-get install -y redis-server
WORKDIR /application
ARG JAR_FILE=./eladmin-system/target/eladmin-system-2.7.jar
COPY ${JAR_FILE} application.jar
ENTRYPOINT ["java", "-jar", "/application/application.jar"]

@ -15,21 +15,39 @@
*/
package me.zhengjie;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
import com.alipay.api.domain.Goods;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.rest.AnonymousGetMapping;
import me.zhengjie.mapper.db1.IStoreMapper;
import me.zhengjie.mapper.db2.IStoreMapper2;
import me.zhengjie.modules.goods.repository.GoodsRepository;
import me.zhengjie.utils.SpringContextHolder;
/**
@ -42,9 +60,10 @@ import me.zhengjie.utils.SpringContextHolder;
@RestController
@Api(hidden = true)
@SpringBootApplication
@EnableTransactionManagement
@EnableJpaAuditing(auditorAwareRef = "auditorAware")
// @EnableTransactionManagement
// @EnableJpaAuditing(auditorAwareRef = "auditorAware")
@RequiredArgsConstructor
@Component
public class AppRun {
public static void main(String[] args) {
@ -73,6 +92,265 @@ public class AppRun {
public String index() {
System.out.println(iStoreMapper.findByName("店1"));
System.out.println(iStoreMapper2.findByName("店1"));
// test();
return "Backend service started successfully";
}
// public void test() {
// // 设置使用主数据源
// DynamicDataSource.setCurrentLookupKey("primaryDataSource");
// // 执行数据库操作
// System.out.println("query start:");
// GoodsRepository goodsRepository = new GoodsRepository() {
// @Override
// public List<me.zhengjie.modules.goods.domain.Goods> findAll() {
// // TODO Auto-generated method stub
// Goods goods = new Goods();
// }
// @Override
// public List<me.zhengjie.modules.goods.domain.Goods> findAll(Sort sort) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'findAll'");
// }
// @Override
// public List<me.zhengjie.modules.goods.domain.Goods>
// findAllById(Iterable<Integer> ids) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method
// 'findAllById'");
// }
// @Override
// public <S extends me.zhengjie.modules.goods.domain.Goods> List<S>
// saveAll(Iterable<S> entities) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'saveAll'");
// }
// @Override
// public void flush() {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'flush'");
// }
// @Override
// public <S extends me.zhengjie.modules.goods.domain.Goods> S saveAndFlush(S
// entity) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method
// 'saveAndFlush'");
// }
// @Override
// public <S extends me.zhengjie.modules.goods.domain.Goods> List<S>
// saveAllAndFlush(Iterable<S> entities) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method
// 'saveAllAndFlush'");
// }
// @Override
// public void deleteAllInBatch(Iterable<me.zhengjie.modules.goods.domain.Goods>
// entities) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method
// 'deleteAllInBatch'");
// }
// @Override
// public void deleteAllByIdInBatch(Iterable<Integer> ids) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method
// 'deleteAllByIdInBatch'");
// }
// @Override
// public void deleteAllInBatch() {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method
// 'deleteAllInBatch'");
// }
// @Override
// public me.zhengjie.modules.goods.domain.Goods getOne(Integer id) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'getOne'");
// }
// @Override
// public me.zhengjie.modules.goods.domain.Goods getById(Integer id) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'getById'");
// }
// @Override
// public <S extends me.zhengjie.modules.goods.domain.Goods> List<S>
// findAll(Example<S> example) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'findAll'");
// }
// @Override
// public <S extends me.zhengjie.modules.goods.domain.Goods> List<S>
// findAll(Example<S> example, Sort sort) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'findAll'");
// }
// @Override
// public Page<me.zhengjie.modules.goods.domain.Goods> findAll(Pageable
// pageable) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'findAll'");
// }
// @Override
// public <S extends me.zhengjie.modules.goods.domain.Goods> S save(S entity) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'save'");
// }
// @Override
// public Optional<me.zhengjie.modules.goods.domain.Goods> findById(Integer id)
// {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'findById'");
// }
// @Override
// public boolean existsById(Integer id) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'existsById'");
// }
// @Override
// public long count() {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'count'");
// }
// @Override
// public void deleteById(Integer id) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'deleteById'");
// }
// @Override
// public void delete(me.zhengjie.modules.goods.domain.Goods entity) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'delete'");
// }
// @Override
// public void deleteAllById(Iterable<? extends Integer> ids) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method
// 'deleteAllById'");
// }
// @Override
// public void deleteAll(Iterable<? extends
// me.zhengjie.modules.goods.domain.Goods> entities) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'deleteAll'");
// }
// @Override
// public void deleteAll() {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'deleteAll'");
// }
// @Override
// public <S extends me.zhengjie.modules.goods.domain.Goods> Optional<S>
// findOne(Example<S> example) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'findOne'");
// }
// @Override
// public <S extends me.zhengjie.modules.goods.domain.Goods> Page<S>
// findAll(Example<S> example,
// Pageable pageable) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'findAll'");
// }
// @Override
// public <S extends me.zhengjie.modules.goods.domain.Goods> long
// count(Example<S> example) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'count'");
// }
// @Override
// public <S extends me.zhengjie.modules.goods.domain.Goods> boolean
// exists(Example<S> example) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'exists'");
// }
// @Override
// public <S extends me.zhengjie.modules.goods.domain.Goods, R> R
// findBy(Example<S> example,
// Function<FetchableFluentQuery<S>, R> queryFunction) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'findBy'");
// }
// @Override
// public Optional<me.zhengjie.modules.goods.domain.Goods> findOne(
// Specification<me.zhengjie.modules.goods.domain.Goods> spec) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'findOne'");
// }
// @Override
// public List<me.zhengjie.modules.goods.domain.Goods> findAll(
// Specification<me.zhengjie.modules.goods.domain.Goods> spec) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'findAll'");
// }
// @Override
// public Page<me.zhengjie.modules.goods.domain.Goods> findAll(
// Specification<me.zhengjie.modules.goods.domain.Goods> spec, Pageable
// pageable) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'findAll'");
// }
// @Override
// public List<me.zhengjie.modules.goods.domain.Goods> findAll(
// Specification<me.zhengjie.modules.goods.domain.Goods> spec, Sort sort) {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'findAll'");
// }
// @Override
// public long count(Specification<me.zhengjie.modules.goods.domain.Goods> spec)
// {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method 'count'");
// }
// };
// System.out.println("query end:");
// // ...
// // 清除数据源上下文
// DynamicDataSource.clearCurrentLookupKey();
// // 设置使用辅数据源
// DynamicDataSource.setCurrentLookupKey("secondDataSource");
// // 执行数据库操作
// System.out.println("query2 start:");
// System.out.println("query2 end:");
// // ...
// // 清除数据源上下文
// DynamicDataSource.clearCurrentLookupKey();
// }
}

@ -0,0 +1,23 @@
package me.zhengjie;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
@Override
protected Object determineCurrentLookupKey() {
System.out.println("get:" + contextHolder.get());
return contextHolder.get();
}
public static void setCurrentLookupKey(String key) {
System.out.println("set:" + key);
contextHolder.set(key);
}
public static void clearCurrentLookupKey() {
contextHolder.remove();
}
}

@ -15,16 +15,18 @@
*/
package me.zhengjie.modules.quartz.config;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.quartz.domain.QuartzJob;
import me.zhengjie.modules.quartz.repository.QuartzJobRepository;
import me.zhengjie.modules.quartz.utils.QuartzManage;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.List;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.quartz.domain.QuartzJob;
import me.zhengjie.modules.quartz.repository.QuartzJobRepository;
import me.zhengjie.modules.quartz.utils.QuartzManage;
/**
* @author Zheng Jie
@ -48,4 +50,5 @@ public class JobRunner implements ApplicationRunner {
quartzJobs.forEach(quartzManage::addJob);
log.info("Timing task injection complete");
}
}

@ -15,6 +15,25 @@
*/
package me.zhengjie.modules.quartz.rest;
import java.io.IOException;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
@ -27,15 +46,6 @@ import me.zhengjie.modules.quartz.service.QuartzJobService;
import me.zhengjie.modules.quartz.service.dto.JobQueryCriteria;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.SpringContextHolder;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;
/**
* @author Zheng Jie
@ -54,8 +64,8 @@ public class QuartzJobController {
@ApiOperation("查询定时任务")
@GetMapping
@PreAuthorize("@el.check('timing:list')")
public ResponseEntity<PageResult<QuartzJob>> queryQuartzJob(JobQueryCriteria criteria, Pageable pageable){
return new ResponseEntity<>(quartzJobService.queryAll(criteria,pageable), HttpStatus.OK);
public ResponseEntity<PageResult<QuartzJob>> queryQuartzJob(JobQueryCriteria criteria, Pageable pageable) {
return new ResponseEntity<>(quartzJobService.queryAll(criteria, pageable), HttpStatus.OK);
}
@ApiOperation("导出任务数据")
@ -75,17 +85,17 @@ public class QuartzJobController {
@ApiOperation("查询任务执行日志")
@GetMapping(value = "/logs")
@PreAuthorize("@el.check('timing:list')")
public ResponseEntity<PageResult<QuartzLog>> queryQuartzJobLog(JobQueryCriteria criteria, Pageable pageable){
return new ResponseEntity<>(quartzJobService.queryAllLog(criteria,pageable), HttpStatus.OK);
public ResponseEntity<PageResult<QuartzLog>> queryQuartzJobLog(JobQueryCriteria criteria, Pageable pageable) {
return new ResponseEntity<>(quartzJobService.queryAllLog(criteria, pageable), HttpStatus.OK);
}
@Log("新增定时任务")
@ApiOperation("新增定时任务")
@PostMapping
@PreAuthorize("@el.check('timing:add')")
public ResponseEntity<Object> createQuartzJob(@Validated @RequestBody QuartzJob resources){
public ResponseEntity<Object> createQuartzJob(@Validated @RequestBody QuartzJob resources) {
if (resources.getId() != null) {
throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
}
// 验证Bean是不是合法的合法的定时任务 Bean 需要用 @Service 定义
checkBean(resources.getBeanName());
@ -97,7 +107,7 @@ public class QuartzJobController {
@ApiOperation("修改定时任务")
@PutMapping
@PreAuthorize("@el.check('timing:edit')")
public ResponseEntity<Object> updateQuartzJob(@Validated(QuartzJob.Update.class) @RequestBody QuartzJob resources){
public ResponseEntity<Object> updateQuartzJob(@Validated(QuartzJob.Update.class) @RequestBody QuartzJob resources) {
// 验证Bean是不是合法的合法的定时任务 Bean 需要用 @Service 定义
checkBean(resources.getBeanName());
quartzJobService.update(resources);
@ -108,7 +118,7 @@ public class QuartzJobController {
@ApiOperation("更改定时任务状态")
@PutMapping(value = "/{id}")
@PreAuthorize("@el.check('timing:edit')")
public ResponseEntity<Object> updateQuartzJobStatus(@PathVariable Long id){
public ResponseEntity<Object> updateQuartzJobStatus(@PathVariable Long id) {
quartzJobService.updateIsPause(quartzJobService.findById(id));
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@ -117,7 +127,7 @@ public class QuartzJobController {
@ApiOperation("执行定时任务")
@PutMapping(value = "/exec/{id}")
@PreAuthorize("@el.check('timing:edit')")
public ResponseEntity<Object> executionQuartzJob(@PathVariable Long id){
public ResponseEntity<Object> executionQuartzJob(@PathVariable Long id) {
quartzJobService.execution(quartzJobService.findById(id));
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@ -126,15 +136,15 @@ public class QuartzJobController {
@ApiOperation("删除定时任务")
@DeleteMapping
@PreAuthorize("@el.check('timing:del')")
public ResponseEntity<Object> deleteQuartzJob(@RequestBody Set<Long> ids){
public ResponseEntity<Object> deleteQuartzJob(@RequestBody Set<Long> ids) {
quartzJobService.delete(ids);
return new ResponseEntity<>(HttpStatus.OK);
}
private void checkBean(String beanName){
private void checkBean(String beanName) {
// 避免调用攻击者可以从SpringContextHolder获得控制jdbcTemplate类
// 并使用getDeclaredMethod调用jdbcTemplate的queryForMap函数执行任意sql命令。
if(!SpringContextHolder.getAllServiceBeanName().contains(beanName)){
if (!SpringContextHolder.getAllServiceBeanName().contains(beanName)) {
throw new BadRequestException("非法的 Bean请重新输入");
}
}

Loading…
Cancel
Save