Commit 7c35a79c authored by ZhengJie's avatar ZhengJie
Browse files

[新增功能](el-admin v2.5): v2.5 beta

详情 https://www.ydyno.com/archives/1225.html
parent d35ffc9d
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.quartz.repository; package me.zhengjie.modules.quartz.repository;
import me.zhengjie.modules.quartz.domain.QuartzJob; import me.zhengjie.modules.quartz.domain.QuartzJob;
......
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.quartz.repository; package me.zhengjie.modules.quartz.repository;
import me.zhengjie.modules.quartz.domain.QuartzLog; import me.zhengjie.modules.quartz.domain.QuartzLog;
......
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.quartz.rest; package me.zhengjie.modules.quartz.rest;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.aop.log.Log; import me.zhengjie.aop.log.Log;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
...@@ -14,7 +30,6 @@ import org.springframework.http.ResponseEntity; ...@@ -14,7 +30,6 @@ import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.Set; import java.util.Set;
...@@ -25,18 +40,14 @@ import java.util.Set; ...@@ -25,18 +40,14 @@ import java.util.Set;
*/ */
@Slf4j @Slf4j
@RestController @RestController
@Api(tags = "系统:定时任务管理") @RequiredArgsConstructor
@RequestMapping("/api/jobs") @RequestMapping("/api/jobs")
@Api(tags = "系统:定时任务管理")
public class QuartzJobController { public class QuartzJobController {
private static final String ENTITY_NAME = "quartzJob"; private static final String ENTITY_NAME = "quartzJob";
private final QuartzJobService quartzJobService; private final QuartzJobService quartzJobService;
public QuartzJobController(QuartzJobService quartzJobService) {
this.quartzJobService = quartzJobService;
}
@Log("查询定时任务") @Log("查询定时任务")
@ApiOperation("查询定时任务") @ApiOperation("查询定时任务")
@GetMapping @GetMapping
......
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.quartz.service; package me.zhengjie.modules.quartz.service;
import me.zhengjie.modules.quartz.domain.QuartzJob; import me.zhengjie.modules.quartz.domain.QuartzJob;
......
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.quartz.service.dto; package me.zhengjie.modules.quartz.service.dto;
import lombok.Data; import lombok.Data;
import me.zhengjie.annotation.Query; import me.zhengjie.annotation.Query;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.List; import java.util.List;
......
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.quartz.service.impl; package me.zhengjie.modules.quartz.service.impl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.quartz.domain.QuartzJob; import me.zhengjie.modules.quartz.domain.QuartzJob;
import me.zhengjie.modules.quartz.domain.QuartzLog; import me.zhengjie.modules.quartz.domain.QuartzLog;
...@@ -20,7 +36,6 @@ import org.springframework.data.domain.Pageable; ...@@ -20,7 +36,6 @@ import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
...@@ -29,23 +44,16 @@ import java.util.*; ...@@ -29,23 +44,16 @@ import java.util.*;
* @author Zheng Jie * @author Zheng Jie
* @date 2019-01-07 * @date 2019-01-07
*/ */
@RequiredArgsConstructor
@Service(value = "quartzJobService") @Service(value = "quartzJobService")
@CacheConfig(cacheNames = "quartzJob") @CacheConfig(cacheNames = "quartzJob")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class) @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class QuartzJobServiceImpl implements QuartzJobService { public class QuartzJobServiceImpl implements QuartzJobService {
private final QuartzJobRepository quartzJobRepository; private final QuartzJobRepository quartzJobRepository;
private final QuartzLogRepository quartzLogRepository; private final QuartzLogRepository quartzLogRepository;
private final QuartzManage quartzManage; private final QuartzManage quartzManage;
public QuartzJobServiceImpl(QuartzJobRepository quartzJobRepository, QuartzLogRepository quartzLogRepository, QuartzManage quartzManage) {
this.quartzJobRepository = quartzJobRepository;
this.quartzLogRepository = quartzLogRepository;
this.quartzManage = quartzManage;
}
@Override @Override
@Cacheable @Cacheable
public Object queryAll(JobQueryCriteria criteria, Pageable pageable){ public Object queryAll(JobQueryCriteria criteria, Pageable pageable){
...@@ -91,9 +99,6 @@ public class QuartzJobServiceImpl implements QuartzJobService { ...@@ -91,9 +99,6 @@ public class QuartzJobServiceImpl implements QuartzJobService {
@CacheEvict(allEntries = true) @CacheEvict(allEntries = true)
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void update(QuartzJob resources) { public void update(QuartzJob resources) {
if(resources.getId().equals(1L)){
throw new BadRequestException("该任务不可操作");
}
if (!CronExpression.isValidExpression(resources.getCronExpression())){ if (!CronExpression.isValidExpression(resources.getCronExpression())){
throw new BadRequestException("cron表达式格式错误"); throw new BadRequestException("cron表达式格式错误");
} }
...@@ -104,9 +109,6 @@ public class QuartzJobServiceImpl implements QuartzJobService { ...@@ -104,9 +109,6 @@ public class QuartzJobServiceImpl implements QuartzJobService {
@Override @Override
@CacheEvict(allEntries = true) @CacheEvict(allEntries = true)
public void updateIsPause(QuartzJob quartzJob) { public void updateIsPause(QuartzJob quartzJob) {
if(quartzJob.getId().equals(1L)){
throw new BadRequestException("该任务不可操作");
}
if (quartzJob.getIsPause()) { if (quartzJob.getIsPause()) {
quartzManage.resumeJob(quartzJob); quartzManage.resumeJob(quartzJob);
quartzJob.setIsPause(false); quartzJob.setIsPause(false);
...@@ -119,9 +121,6 @@ public class QuartzJobServiceImpl implements QuartzJobService { ...@@ -119,9 +121,6 @@ public class QuartzJobServiceImpl implements QuartzJobService {
@Override @Override
public void execution(QuartzJob quartzJob) { public void execution(QuartzJob quartzJob) {
if(quartzJob.getId().equals(1L)){
throw new BadRequestException("该任务不可操作");
}
quartzManage.runJobNow(quartzJob); quartzManage.runJobNow(quartzJob);
} }
...@@ -130,9 +129,6 @@ public class QuartzJobServiceImpl implements QuartzJobService { ...@@ -130,9 +129,6 @@ public class QuartzJobServiceImpl implements QuartzJobService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void delete(Set<Long> ids) { public void delete(Set<Long> ids) {
for (Long id : ids) { for (Long id : ids) {
if(id.equals(1L)){
throw new BadRequestException("更新访客记录不可删除,你可以在后台代码中取消该限制");
}
QuartzJob quartzJob = findById(id); QuartzJob quartzJob = findById(id);
quartzManage.deleteJob(quartzJob); quartzManage.deleteJob(quartzJob);
quartzJobRepository.delete(quartzJob); quartzJobRepository.delete(quartzJob);
...@@ -150,7 +146,7 @@ public class QuartzJobServiceImpl implements QuartzJobService { ...@@ -150,7 +146,7 @@ public class QuartzJobServiceImpl implements QuartzJobService {
map.put("参数", quartzJob.getParams()); map.put("参数", quartzJob.getParams());
map.put("表达式", quartzJob.getCronExpression()); map.put("表达式", quartzJob.getCronExpression());
map.put("状态", quartzJob.getIsPause() ? "暂停中" : "运行中"); map.put("状态", quartzJob.getIsPause() ? "暂停中" : "运行中");
map.put("描述", quartzJob.getRemark()); map.put("描述", quartzJob.getDescription());
map.put("创建日期", quartzJob.getCreateTime()); map.put("创建日期", quartzJob.getCreateTime());
list.add(map); list.add(map);
} }
......
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.quartz.task; package me.zhengjie.modules.quartz.task;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -13,10 +28,14 @@ import org.springframework.stereotype.Component; ...@@ -13,10 +28,14 @@ import org.springframework.stereotype.Component;
public class TestTask { public class TestTask {
public void run(){ public void run(){
log.info("执行成功"); log.info("run 执行成功");
} }
public void run1(String str){ public void run1(String str){
log.info("执行成功,参数为: {}" + str); log.info("run1 执行成功,参数为: {}" + str);
}
public void run2(){
log.info("run2 执行成功");
} }
} }
package me.zhengjie.modules.quartz.task;
import me.zhengjie.modules.monitor.service.VisitsService;
import org.springframework.stereotype.Component;
/**
* @author Zheng Jie
* @date 2018-12-25
*/
@Component
public class VisitsTask {
private final VisitsService visitsService;
public VisitsTask(VisitsService visitsService) {
this.visitsService = visitsService;
}
public void run(){
visitsService.save();
}
}
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.quartz.utils; package me.zhengjie.modules.quartz.utils;
import me.zhengjie.config.thread.TheadFactoryName; import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
import me.zhengjie.config.thread.ThreadPoolExecutorUtil; import me.zhengjie.config.thread.ThreadPoolExecutorUtil;
import me.zhengjie.domain.vo.EmailVo;
import me.zhengjie.modules.quartz.domain.QuartzJob; import me.zhengjie.modules.quartz.domain.QuartzJob;
import me.zhengjie.modules.quartz.domain.QuartzLog; import me.zhengjie.modules.quartz.domain.QuartzLog;
import me.zhengjie.modules.quartz.repository.QuartzLogRepository; import me.zhengjie.modules.quartz.repository.QuartzLogRepository;
import me.zhengjie.modules.quartz.service.QuartzJobService; import me.zhengjie.modules.quartz.service.QuartzJobService;
import me.zhengjie.service.EmailService;
import me.zhengjie.utils.SpringContextHolder; import me.zhengjie.utils.SpringContextHolder;
import me.zhengjie.utils.ThrowableUtil; import me.zhengjie.utils.ThrowableUtil;
import org.quartz.JobExecutionContext; import org.quartz.JobExecutionContext;
...@@ -13,6 +33,8 @@ import org.slf4j.Logger; ...@@ -13,6 +33,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
/** /**
...@@ -23,14 +45,14 @@ import java.util.concurrent.*; ...@@ -23,14 +45,14 @@ import java.util.concurrent.*;
@Async @Async
public class ExecutionJob extends QuartzJobBean { public class ExecutionJob extends QuartzJobBean {
private Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger logger = LoggerFactory.getLogger(this.getClass());
/** 该处仅供参考 */ /** 该处仅供参考 */
private final static ThreadPoolExecutor EXECUTOR = ThreadPoolExecutorUtil.getPoll(); private final static ThreadPoolExecutor EXECUTOR = ThreadPoolExecutorUtil.getPoll();
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected void executeInternal(JobExecutionContext context) { public void executeInternal(JobExecutionContext context) {
QuartzJob quartzJob = (QuartzJob) context.getMergedJobDataMap().get(QuartzJob.JOB_KEY); QuartzJob quartzJob = (QuartzJob) context.getMergedJobDataMap().get(QuartzJob.JOB_KEY);
// 获取spring bean // 获取spring bean
QuartzLogRepository quartzLogRepository = SpringContextHolder.getBean(QuartzLogRepository.class); QuartzLogRepository quartzLogRepository = SpringContextHolder.getBean(QuartzLogRepository.class);
...@@ -55,6 +77,13 @@ public class ExecutionJob extends QuartzJobBean { ...@@ -55,6 +77,13 @@ public class ExecutionJob extends QuartzJobBean {
// 任务状态 // 任务状态
log.setIsSuccess(true); log.setIsSuccess(true);
logger.info("任务执行完毕,任务名称:{} 总共耗时:{} 毫秒", quartzJob.getJobName(), times); logger.info("任务执行完毕,任务名称:{} 总共耗时:{} 毫秒", quartzJob.getJobName(), times);
// 判断是否存在子任务
if(quartzJob.getSubTask() != null){
String[] tasks = quartzJob.getSubTask().split("[,,]");
for (String id : tasks) {
quartzJobService.execution(quartzJobService.findById(Long.parseLong(id)));
}
}
} catch (Exception e) { } catch (Exception e) {
logger.error("任务执行失败,任务名称:{}" + quartzJob.getJobName(), e); logger.error("任务执行失败,任务名称:{}" + quartzJob.getJobName(), e);
long times = System.currentTimeMillis() - startTime; long times = System.currentTimeMillis() - startTime;
...@@ -62,11 +91,34 @@ public class ExecutionJob extends QuartzJobBean { ...@@ -62,11 +91,34 @@ public class ExecutionJob extends QuartzJobBean {
// 任务状态 0:成功 1:失败 // 任务状态 0:成功 1:失败
log.setIsSuccess(false); log.setIsSuccess(false);
log.setExceptionDetail(ThrowableUtil.getStackTrace(e)); log.setExceptionDetail(ThrowableUtil.getStackTrace(e));
quartzJob.setIsPause(false); // 任务如果失败了则暂停
//更新状态 if(quartzJob.getPauseAfterFailure() != null && quartzJob.getPauseAfterFailure()){
quartzJobService.updateIsPause(quartzJob); quartzJob.setIsPause(false);
//更新状态
quartzJobService.updateIsPause(quartzJob);
}
if(quartzJob.getEmail() != null){
EmailService emailService = SpringContextHolder.getBean(EmailService.class);
// 邮箱报警
EmailVo emailVo = taskAlarm(quartzJob, ThrowableUtil.getStackTrace(e));
emailService.send(emailVo, emailService.find());
}
} finally { } finally {
quartzLogRepository.save(log); quartzLogRepository.save(log);
} }
} }
private EmailVo taskAlarm(QuartzJob quartzJob, String msg) {
EmailVo emailVo = new EmailVo();
emailVo.setSubject("定时任务【"+ quartzJob.getJobName() +"】执行失败,请尽快处理!");
Map<String, Object> data = new HashMap<>();
data.put("task", quartzJob);
data.put("msg", msg);
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));
Template template = engine.getTemplate("email/taskAlarm.ftl");
emailVo.setContent(template.render(data));
List<String> emails = Arrays.asList(quartzJob.getEmail().split("[,,]"));
emailVo.setTos(emails);
return emailVo;
}
} }
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.quartz.utils; package me.zhengjie.modules.quartz.utils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
......
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.quartz.utils; package me.zhengjie.modules.quartz.utils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -14,9 +29,9 @@ import java.util.concurrent.Callable; ...@@ -14,9 +29,9 @@ import java.util.concurrent.Callable;
@Slf4j @Slf4j
public class QuartzRunnable implements Callable { public class QuartzRunnable implements Callable {
private Object target; private final Object target;
private Method method; private final Method method;
private String params; private final String params;
QuartzRunnable(String beanName, String methodName, String params) QuartzRunnable(String beanName, String methodName, String params)
throws NoSuchMethodException, SecurityException { throws NoSuchMethodException, SecurityException {
......
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.security.config; package me.zhengjie.modules.security.config;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess; import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.modules.security.security.*; import me.zhengjie.modules.security.security.*;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
...@@ -28,6 +44,7 @@ import java.util.Set; ...@@ -28,6 +44,7 @@ import java.util.Set;
*/ */
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
@RequiredArgsConstructor
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter { public class SecurityConfig extends WebSecurityConfigurerAdapter {
...@@ -37,15 +54,6 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -37,15 +54,6 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final JwtAccessDeniedHandler jwtAccessDeniedHandler; private final JwtAccessDeniedHandler jwtAccessDeniedHandler;
private final ApplicationContext applicationContext; private final ApplicationContext applicationContext;
public SecurityConfig(TokenProvider tokenProvider, CorsFilter corsFilter, JwtAuthenticationEntryPoint authenticationErrorHandler, JwtAccessDeniedHandler jwtAccessDeniedHandler, ApplicationContext applicationContext) {
this.tokenProvider = tokenProvider;
this.corsFilter = corsFilter;
this.authenticationErrorHandler = authenticationErrorHandler;
this.jwtAccessDeniedHandler = jwtAccessDeniedHandler;
this.applicationContext = applicationContext;
}
@Bean @Bean
GrantedAuthorityDefaults grantedAuthorityDefaults() { GrantedAuthorityDefaults grantedAuthorityDefaults() {
// 去除 ROLE_ 前缀 // 去除 ROLE_ 前缀
......
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.security.config; package me.zhengjie.modules.security.config;
import lombok.Data; import lombok.Data;
......
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.security.rest; package me.zhengjie.modules.security.rest;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
...@@ -6,6 +21,7 @@ import cn.hutool.crypto.asymmetric.RSA; ...@@ -6,6 +21,7 @@ import cn.hutool.crypto.asymmetric.RSA;
import com.wf.captcha.ArithmeticCaptcha; import com.wf.captcha.ArithmeticCaptcha;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.AnonymousAccess; import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.aop.log.Log; import me.zhengjie.aop.log.Log;
...@@ -25,7 +41,6 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio ...@@ -25,7 +41,6 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
...@@ -41,6 +56,7 @@ import java.util.concurrent.TimeUnit; ...@@ -41,6 +56,7 @@ import java.util.concurrent.TimeUnit;
@Slf4j @Slf4j
@RestController @RestController
@RequestMapping("/auth") @RequestMapping("/auth")
@RequiredArgsConstructor
@Api(tags = "系统:系统授权接口") @Api(tags = "系统:系统授权接口")
public class AuthorizationController { public class AuthorizationController {
...@@ -52,20 +68,10 @@ public class AuthorizationController { ...@@ -52,20 +68,10 @@ public class AuthorizationController {
private Boolean singleLogin; private Boolean singleLogin;
private final SecurityProperties properties; private final SecurityProperties properties;
private final RedisUtils redisUtils; private final RedisUtils redisUtils;
private final UserDetailsService userDetailsService;
private final OnlineUserService onlineUserService; private final OnlineUserService onlineUserService;
private final TokenProvider tokenProvider; private final TokenProvider tokenProvider;
private final AuthenticationManagerBuilder authenticationManagerBuilder; private final AuthenticationManagerBuilder authenticationManagerBuilder;
public AuthorizationController(SecurityProperties properties, RedisUtils redisUtils, UserDetailsService userDetailsService, OnlineUserService onlineUserService, TokenProvider tokenProvider, AuthenticationManagerBuilder authenticationManagerBuilder) {
this.properties = properties;
this.redisUtils = redisUtils;
this.userDetailsService = userDetailsService;
this.onlineUserService = onlineUserService;
this.tokenProvider = tokenProvider;
this.authenticationManagerBuilder = authenticationManagerBuilder;
}
@Log("用户登录") @Log("用户登录")
@ApiOperation("登录授权") @ApiOperation("登录授权")
@AnonymousAccess @AnonymousAccess
......
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.security.rest; package me.zhengjie.modules.security.rest;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.aop.log.Log; import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.security.service.OnlineUserService; import me.zhengjie.modules.security.service.OnlineUserService;
import me.zhengjie.utils.EncryptUtils; import me.zhengjie.utils.EncryptUtils;
...@@ -19,16 +35,13 @@ import java.util.Set; ...@@ -19,16 +35,13 @@ import java.util.Set;
* @author Zheng Jie * @author Zheng Jie
*/ */
@RestController @RestController
@RequiredArgsConstructor
@RequestMapping("/auth/online") @RequestMapping("/auth/online")
@Api(tags = "系统:在线用户管理") @Api(tags = "系统:在线用户管理")
public class OnlineController { public class OnlineController {
private final OnlineUserService onlineUserService; private final OnlineUserService onlineUserService;
public OnlineController(OnlineUserService onlineUserService) {
this.onlineUserService = onlineUserService;
}
@ApiOperation("查询在线用户") @ApiOperation("查询在线用户")
@GetMapping @GetMapping
@PreAuthorize("@el.check()") @PreAuthorize("@el.check()")
......
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.security.security; package me.zhengjie.modules.security.security;
import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.AccessDeniedException;
......
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.security.security; package me.zhengjie.modules.security.security;
import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.AuthenticationException;
......
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.security.security; package me.zhengjie.modules.security.security;
import lombok.RequiredArgsConstructor;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter; import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain; import org.springframework.security.web.DefaultSecurityFilterChain;
...@@ -8,14 +24,11 @@ import org.springframework.security.web.authentication.UsernamePasswordAuthentic ...@@ -8,14 +24,11 @@ import org.springframework.security.web.authentication.UsernamePasswordAuthentic
/** /**
* @author / * @author /
*/ */
@RequiredArgsConstructor
public class TokenConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> { public class TokenConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
private final TokenProvider tokenProvider; private final TokenProvider tokenProvider;
public TokenConfigurer(TokenProvider tokenProvider){
this.tokenProvider = tokenProvider;
}
@Override @Override
public void configure(HttpSecurity http) { public void configure(HttpSecurity http) {
TokenFilter customFilter = new TokenFilter(tokenProvider); TokenFilter customFilter = new TokenFilter(tokenProvider);
......
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.security.security; package me.zhengjie.modules.security.security;
import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.ExpiredJwtException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.security.config.SecurityProperties; import me.zhengjie.modules.security.config.SecurityProperties;
import me.zhengjie.modules.security.service.dto.OnlineUserDto; import me.zhengjie.modules.security.service.dto.OnlineUserDto;
...@@ -21,14 +37,11 @@ import java.io.IOException; ...@@ -21,14 +37,11 @@ import java.io.IOException;
* @author / * @author /
*/ */
@Slf4j @Slf4j
@RequiredArgsConstructor
public class TokenFilter extends GenericFilterBean { public class TokenFilter extends GenericFilterBean {
private final TokenProvider tokenProvider; private final TokenProvider tokenProvider;
TokenFilter(TokenProvider tokenProvider) {
this.tokenProvider = tokenProvider;
}
@Override @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException { throws IOException, ServletException {
......
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.security.security; package me.zhengjie.modules.security.security;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import io.jsonwebtoken.*; import io.jsonwebtoken.*;
import io.jsonwebtoken.io.Decoders; import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys; import io.jsonwebtoken.security.Keys;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.security.config.SecurityProperties; import me.zhengjie.modules.security.config.SecurityProperties;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
...@@ -26,17 +42,13 @@ import java.util.stream.Collectors; ...@@ -26,17 +42,13 @@ import java.util.stream.Collectors;
*/ */
@Slf4j @Slf4j
@Component @Component
@RequiredArgsConstructor
public class TokenProvider implements InitializingBean { public class TokenProvider implements InitializingBean {
private final SecurityProperties properties; private final SecurityProperties properties;
private static final String AUTHORITIES_KEY = "auth"; private static final String AUTHORITIES_KEY = "auth";
private Key key; private Key key;
public TokenProvider(SecurityProperties properties) {
this.properties = properties;
}
@Override @Override
public void afterPropertiesSet() { public void afterPropertiesSet() {
byte[] keyBytes = Decoders.BASE64.decode(properties.getBase64Secret()); byte[] keyBytes = Decoders.BASE64.decode(properties.getBase64Secret());
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment