Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
bf7c1eeb
Commit
bf7c1eeb
authored
Oct 24, 2019
by
dqjdda
Browse files
代码优化完成,去除大量idea警告,代码生成器优化等
parent
e3c3ebb1
Changes
146
Hide whitespace changes
Inline
Side-by-side
eladmin-system/src/main/java/me/zhengjie/modules/monitor/service/VisitsService.java
View file @
bf7c1eeb
package
me.zhengjie.modules.monitor.service
;
package
me.zhengjie.modules.monitor.service
;
import
org.springframework.scheduling.annotation.Async
;
import
org.springframework.scheduling.annotation.Async
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
/**
/**
...
@@ -17,20 +16,20 @@ public interface VisitsService {
...
@@ -17,20 +16,20 @@ public interface VisitsService {
/**
/**
* 新增记录
* 新增记录
* @param request
* @param request
/
*/
*/
@Async
@Async
void
count
(
HttpServletRequest
request
);
void
count
(
HttpServletRequest
request
);
/**
/**
* 获取数据
* 获取数据
* @return
* @return
/
*/
*/
Object
get
();
Object
get
();
/**
/**
* getChartData
* getChartData
* @return
* @return
/
*/
*/
Object
getChartData
();
Object
getChartData
();
}
}
eladmin-system/src/main/java/me/zhengjie/modules/monitor/service/impl/RedisServiceImpl.java
View file @
bf7c1eeb
...
@@ -3,7 +3,6 @@ package me.zhengjie.modules.monitor.service.impl;
...
@@ -3,7 +3,6 @@ package me.zhengjie.modules.monitor.service.impl;
import
me.zhengjie.modules.monitor.domain.vo.RedisVo
;
import
me.zhengjie.modules.monitor.domain.vo.RedisVo
;
import
me.zhengjie.modules.monitor.service.RedisService
;
import
me.zhengjie.modules.monitor.service.RedisService
;
import
me.zhengjie.utils.PageUtil
;
import
me.zhengjie.utils.PageUtil
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.PageImpl
;
import
org.springframework.data.domain.PageImpl
;
...
@@ -12,6 +11,7 @@ import org.springframework.data.redis.core.RedisTemplate;
...
@@ -12,6 +11,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Objects
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
/**
/**
...
@@ -21,54 +21,58 @@ import java.util.concurrent.TimeUnit;
...
@@ -21,54 +21,58 @@ import java.util.concurrent.TimeUnit;
@Service
@Service
public
class
RedisServiceImpl
implements
RedisService
{
public
class
RedisServiceImpl
implements
RedisService
{
@Autowired
private
final
RedisTemplate
redisTemplate
;
RedisTemplate
redisTemplate
;
@Value
(
"${loginCode.expiration}"
)
@Value
(
"${loginCode.expiration}"
)
private
Long
expiration
;
private
Long
expiration
;
public
RedisServiceImpl
(
RedisTemplate
redisTemplate
)
{
this
.
redisTemplate
=
redisTemplate
;
}
@Override
@Override
@SuppressWarnings
(
"unchecked"
)
public
Page
<
RedisVo
>
findByKey
(
String
key
,
Pageable
pageable
){
public
Page
<
RedisVo
>
findByKey
(
String
key
,
Pageable
pageable
){
List
<
RedisVo
>
redisVos
=
new
ArrayList
<>();
List
<
RedisVo
>
redisVos
=
new
ArrayList
<>();
if
(!
"*"
.
equals
(
key
)){
if
(!
"*"
.
equals
(
key
)){
key
=
"*"
+
key
+
"*"
;
key
=
"*"
+
key
+
"*"
;
}
}
for
(
Object
s
:
redisTemplate
.
keys
(
key
))
{
for
(
Object
s
:
Objects
.
requireNonNull
(
redisTemplate
.
keys
(
key
))
)
{
// 过滤掉权限的缓存
// 过滤掉权限的缓存
if
(
s
.
toString
().
indexOf
(
"role::loadPermissionByUser"
)
!=
-
1
||
s
.
toString
().
indexOf
(
"user::loadUserByUsername"
)
!=
-
1
)
{
if
(
s
.
toString
().
contains
(
"role::loadPermissionByUser"
)
||
s
.
toString
().
contains
(
"user::loadUserByUsername"
))
{
continue
;
continue
;
}
}
RedisVo
redisVo
=
new
RedisVo
(
s
.
toString
(),
redisTemplate
.
opsForValue
().
get
(
s
.
toString
()).
toString
());
RedisVo
redisVo
=
new
RedisVo
(
s
.
toString
(),
Objects
.
requireNonNull
(
redisTemplate
.
opsForValue
().
get
(
s
.
toString
())
)
.
toString
());
redisVos
.
add
(
redisVo
);
redisVos
.
add
(
redisVo
);
}
}
Page
<
RedisVo
>
page
=
new
PageImpl
<
RedisVo
>(
return
new
PageImpl
<
RedisVo
>(
PageUtil
.
toPage
(
pageable
.
getPageNumber
(),
pageable
.
getPageSize
(),
redisVos
),
PageUtil
.
toPage
(
pageable
.
getPageNumber
(),
pageable
.
getPageSize
(),
redisVos
),
pageable
,
pageable
,
redisVos
.
size
());
redisVos
.
size
());
return
page
;
}
}
@Override
@Override
@SuppressWarnings
(
"unchecked"
)
public
void
delete
(
String
key
)
{
public
void
delete
(
String
key
)
{
redisTemplate
.
delete
(
key
);
redisTemplate
.
delete
(
key
);
}
}
@Override
@Override
public
void
flushdb
()
{
public
void
flushdb
()
{
redisTemplate
.
getConnectionFactory
().
getConnection
().
flushDb
();
Objects
.
requireNonNull
(
redisTemplate
.
getConnectionFactory
()
)
.
getConnection
().
flushDb
();
}
}
@Override
@Override
public
String
getCodeVal
(
String
key
)
{
public
String
getCodeVal
(
String
key
)
{
try
{
try
{
String
value
=
redisTemplate
.
opsForValue
().
get
(
key
).
toString
();
return
Objects
.
requireNonNull
(
redisTemplate
.
opsForValue
().
get
(
key
)).
toString
();
return
value
;
}
catch
(
Exception
e
){
}
catch
(
Exception
e
){
return
""
;
return
""
;
}
}
}
}
@Override
@Override
@SuppressWarnings
(
"unchecked"
)
public
void
saveCode
(
String
key
,
Object
val
)
{
public
void
saveCode
(
String
key
,
Object
val
)
{
redisTemplate
.
opsForValue
().
set
(
key
,
val
);
redisTemplate
.
opsForValue
().
set
(
key
,
val
);
redisTemplate
.
expire
(
key
,
expiration
,
TimeUnit
.
MINUTES
);
redisTemplate
.
expire
(
key
,
expiration
,
TimeUnit
.
MINUTES
);
...
...
eladmin-system/src/main/java/me/zhengjie/modules/monitor/service/impl/VisitsServiceImpl.java
View file @
bf7c1eeb
...
@@ -26,11 +26,14 @@ import java.util.stream.Collectors;
...
@@ -26,11 +26,14 @@ import java.util.stream.Collectors;
@Transactional
(
propagation
=
Propagation
.
SUPPORTS
,
readOnly
=
true
,
rollbackFor
=
Exception
.
class
)
@Transactional
(
propagation
=
Propagation
.
SUPPORTS
,
readOnly
=
true
,
rollbackFor
=
Exception
.
class
)
public
class
VisitsServiceImpl
implements
VisitsService
{
public
class
VisitsServiceImpl
implements
VisitsService
{
@Autowired
private
final
VisitsRepository
visitsRepository
;
private
VisitsRepository
visitsRepository
;
@Autowired
private
final
LogRepository
logRepository
;
private
LogRepository
logRepository
;
public
VisitsServiceImpl
(
VisitsRepository
visitsRepository
,
LogRepository
logRepository
)
{
this
.
visitsRepository
=
visitsRepository
;
this
.
logRepository
=
logRepository
;
}
@Override
@Override
public
void
save
()
{
public
void
save
()
{
...
@@ -58,7 +61,7 @@ public class VisitsServiceImpl implements VisitsService {
...
@@ -58,7 +61,7 @@ public class VisitsServiceImpl implements VisitsService {
@Override
@Override
public
Object
get
()
{
public
Object
get
()
{
Map
map
=
new
HashMap
();
Map
<
String
,
Object
>
map
=
new
HashMap
<>
();
LocalDate
localDate
=
LocalDate
.
now
();
LocalDate
localDate
=
LocalDate
.
now
();
Visits
visits
=
visitsRepository
.
findByDate
(
localDate
.
toString
());
Visits
visits
=
visitsRepository
.
findByDate
(
localDate
.
toString
());
List
<
Visits
>
list
=
visitsRepository
.
findAllVisits
(
localDate
.
minusDays
(
6
).
toString
(),
localDate
.
plusDays
(
1
).
toString
());
List
<
Visits
>
list
=
visitsRepository
.
findAllVisits
(
localDate
.
minusDays
(
6
).
toString
(),
localDate
.
plusDays
(
1
).
toString
());
...
@@ -77,7 +80,7 @@ public class VisitsServiceImpl implements VisitsService {
...
@@ -77,7 +80,7 @@ public class VisitsServiceImpl implements VisitsService {
@Override
@Override
public
Object
getChartData
()
{
public
Object
getChartData
()
{
Map
map
=
new
HashMap
();
Map
<
String
,
Object
>
map
=
new
HashMap
<>
();
LocalDate
localDate
=
LocalDate
.
now
();
LocalDate
localDate
=
LocalDate
.
now
();
List
<
Visits
>
list
=
visitsRepository
.
findAllVisits
(
localDate
.
minusDays
(
6
).
toString
(),
localDate
.
plusDays
(
1
).
toString
());
List
<
Visits
>
list
=
visitsRepository
.
findAllVisits
(
localDate
.
minusDays
(
6
).
toString
(),
localDate
.
plusDays
(
1
).
toString
());
map
.
put
(
"weekDays"
,
list
.
stream
().
map
(
Visits:
:
getWeekDay
).
collect
(
Collectors
.
toList
()));
map
.
put
(
"weekDays"
,
list
.
stream
().
map
(
Visits:
:
getWeekDay
).
collect
(
Collectors
.
toList
()));
...
...
eladmin-system/src/main/java/me/zhengjie/modules/quartz/config/JobRunner.java
View file @
bf7c1eeb
...
@@ -3,7 +3,6 @@ package me.zhengjie.modules.quartz.config;
...
@@ -3,7 +3,6 @@ package me.zhengjie.modules.quartz.config;
import
me.zhengjie.modules.quartz.domain.QuartzJob
;
import
me.zhengjie.modules.quartz.domain.QuartzJob
;
import
me.zhengjie.modules.quartz.repository.QuartzJobRepository
;
import
me.zhengjie.modules.quartz.repository.QuartzJobRepository
;
import
me.zhengjie.modules.quartz.utils.QuartzManage
;
import
me.zhengjie.modules.quartz.utils.QuartzManage
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.ApplicationArguments
;
import
org.springframework.boot.ApplicationArguments
;
import
org.springframework.boot.ApplicationRunner
;
import
org.springframework.boot.ApplicationRunner
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
...
@@ -17,24 +16,24 @@ import java.util.List;
...
@@ -17,24 +16,24 @@ import java.util.List;
@Component
@Component
public
class
JobRunner
implements
ApplicationRunner
{
public
class
JobRunner
implements
ApplicationRunner
{
@Autowired
private
final
QuartzJobRepository
quartzJobRepository
;
private
QuartzJobRepository
quartzJobRepository
;
@Autowired
private
final
QuartzManage
quartzManage
;
private
QuartzManage
quartzManage
;
public
JobRunner
(
QuartzJobRepository
quartzJobRepository
,
QuartzManage
quartzManage
)
{
this
.
quartzJobRepository
=
quartzJobRepository
;
this
.
quartzManage
=
quartzManage
;
}
/**
/**
* 项目启动时重新激活启用的定时任务
* 项目启动时重新激活启用的定时任务
* @param applicationArguments
* @param applicationArguments /
* @throws Exception
*/
*/
@Override
@Override
public
void
run
(
ApplicationArguments
applicationArguments
){
public
void
run
(
ApplicationArguments
applicationArguments
){
System
.
out
.
println
(
"--------------------注入定时任务---------------------"
);
System
.
out
.
println
(
"--------------------注入定时任务---------------------"
);
List
<
QuartzJob
>
quartzJobs
=
quartzJobRepository
.
findByIsPauseIsFalse
();
List
<
QuartzJob
>
quartzJobs
=
quartzJobRepository
.
findByIsPauseIsFalse
();
quartzJobs
.
forEach
(
quartzJob
->
{
quartzJobs
.
forEach
(
quartzManage:
:
addJob
);
quartzManage
.
addJob
(
quartzJob
);
});
System
.
out
.
println
(
"--------------------定时任务注入完成---------------------"
);
System
.
out
.
println
(
"--------------------定时任务注入完成---------------------"
);
}
}
}
}
eladmin-system/src/main/java/me/zhengjie/modules/quartz/config/QuartzConfig.java
View file @
bf7c1eeb
...
@@ -2,7 +2,6 @@ package me.zhengjie.modules.quartz.config;
...
@@ -2,7 +2,6 @@ package me.zhengjie.modules.quartz.config;
import
org.quartz.Scheduler
;
import
org.quartz.Scheduler
;
import
org.quartz.spi.TriggerFiredBundle
;
import
org.quartz.spi.TriggerFiredBundle
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.config.AutowireCapableBeanFactory
;
import
org.springframework.beans.factory.config.AutowireCapableBeanFactory
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
...
@@ -12,7 +11,7 @@ import org.springframework.stereotype.Component;
...
@@ -12,7 +11,7 @@ import org.springframework.stereotype.Component;
/**
/**
* 定时任务配置
* 定时任务配置
* @author
* @author
/
* @date 2019-01-07
* @date 2019-01-07
*/
*/
@Configuration
@Configuration
...
@@ -22,10 +21,13 @@ public class QuartzConfig {
...
@@ -22,10 +21,13 @@ public class QuartzConfig {
* 解决Job中注入Spring Bean为null的问题
* 解决Job中注入Spring Bean为null的问题
*/
*/
@Component
(
"quartzJobFactory"
)
@Component
(
"quartzJobFactory"
)
public
class
QuartzJobFactory
extends
AdaptableJobFactory
{
public
static
class
QuartzJobFactory
extends
AdaptableJobFactory
{
@Autowired
private
final
AutowireCapableBeanFactory
capableBeanFactory
;
private
AutowireCapableBeanFactory
capableBeanFactory
;
public
QuartzJobFactory
(
AutowireCapableBeanFactory
capableBeanFactory
)
{
this
.
capableBeanFactory
=
capableBeanFactory
;
}
@Override
@Override
protected
Object
createJobInstance
(
TriggerFiredBundle
bundle
)
throws
Exception
{
protected
Object
createJobInstance
(
TriggerFiredBundle
bundle
)
throws
Exception
{
...
@@ -39,9 +41,9 @@ public class QuartzConfig {
...
@@ -39,9 +41,9 @@ public class QuartzConfig {
/**
/**
* 注入scheduler到spring
* 注入scheduler到spring
* @param quartzJobFactory
* @param quartzJobFactory
/
* @return
* @return
Scheduler
* @throws Exception
* @throws Exception
/
*/
*/
@Bean
(
name
=
"scheduler"
)
@Bean
(
name
=
"scheduler"
)
public
Scheduler
scheduler
(
QuartzJobFactory
quartzJobFactory
)
throws
Exception
{
public
Scheduler
scheduler
(
QuartzJobFactory
quartzJobFactory
)
throws
Exception
{
...
...
eladmin-system/src/main/java/me/zhengjie/modules/quartz/domain/QuartzJob.java
View file @
bf7c1eeb
...
@@ -25,55 +25,39 @@ public class QuartzJob implements Serializable {
...
@@ -25,55 +25,39 @@ public class QuartzJob implements Serializable {
@NotNull
(
groups
=
{
Update
.
class
})
@NotNull
(
groups
=
{
Update
.
class
})
private
Long
id
;
private
Long
id
;
/**
// 定时器名称
* 定时器名称
*/
@Column
(
name
=
"job_name"
)
@Column
(
name
=
"job_name"
)
private
String
jobName
;
private
String
jobName
;
/**
// Bean名称
* Bean名称
*/
@Column
(
name
=
"bean_name"
)
@Column
(
name
=
"bean_name"
)
@NotBlank
@NotBlank
private
String
beanName
;
private
String
beanName
;
/**
// 方法名称
* 方法名称
*/
@Column
(
name
=
"method_name"
)
@Column
(
name
=
"method_name"
)
@NotBlank
@NotBlank
private
String
methodName
;
private
String
methodName
;
/**
// 参数
* 参数
*/
@Column
(
name
=
"params"
)
@Column
(
name
=
"params"
)
private
String
params
;
private
String
params
;
/**
// cron表达式
* cron表达式
*/
@Column
(
name
=
"cron_expression"
)
@Column
(
name
=
"cron_expression"
)
@NotBlank
@NotBlank
private
String
cronExpression
;
private
String
cronExpression
;
/**
// 状态
* 状态
*/
@Column
(
name
=
"is_pause"
)
@Column
(
name
=
"is_pause"
)
private
Boolean
isPause
=
false
;
private
Boolean
isPause
=
false
;
/**
// 备注
* 备注
*/
@Column
(
name
=
"remark"
)
@Column
(
name
=
"remark"
)
@NotBlank
@NotBlank
private
String
remark
;
private
String
remark
;
/**
// 创建日期
* 创建日期
*/
@UpdateTimestamp
@UpdateTimestamp
@Column
(
name
=
"update_time"
)
@Column
(
name
=
"update_time"
)
private
Timestamp
updateTime
;
private
Timestamp
updateTime
;
...
...
eladmin-system/src/main/java/me/zhengjie/modules/quartz/domain/QuartzLog.java
View file @
bf7c1eeb
...
@@ -20,56 +20,38 @@ public class QuartzLog implements Serializable {
...
@@ -20,56 +20,38 @@ public class QuartzLog implements Serializable {
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
private
Long
id
;
private
Long
id
;
/**
// 任务名称
* 任务名称
*/
@Column
(
name
=
"job_name"
)
@Column
(
name
=
"job_name"
)
private
String
jobName
;
private
String
jobName
;
/**
// Bean名称
* Bean名称
*/
@Column
(
name
=
"baen_name"
)
@Column
(
name
=
"baen_name"
)
private
String
beanName
;
private
String
beanName
;
/**
// 方法名称
* 方法名称
*/
@Column
(
name
=
"method_name"
)
@Column
(
name
=
"method_name"
)
private
String
methodName
;
private
String
methodName
;
/**
// 参数
* 参数
*/
@Column
(
name
=
"params"
)
@Column
(
name
=
"params"
)
private
String
params
;
private
String
params
;
/**
// cron表达式
* cron表达式
*/
@Column
(
name
=
"cron_expression"
)
@Column
(
name
=
"cron_expression"
)
private
String
cronExpression
;
private
String
cronExpression
;
/**
// 状态
* 状态
*/
@Column
(
name
=
"is_success"
)
@Column
(
name
=
"is_success"
)
private
Boolean
isSuccess
;
private
Boolean
isSuccess
;
/**
// 异常详细
* 异常详细
*/
@Column
(
name
=
"exception_detail"
,
columnDefinition
=
"text"
)
@Column
(
name
=
"exception_detail"
,
columnDefinition
=
"text"
)
private
String
exceptionDetail
;
private
String
exceptionDetail
;
/**
// 耗时(毫秒)
* 耗时(毫秒)
*/
private
Long
time
;
private
Long
time
;
/**
// 创建日期
* 创建日期
*/
@CreationTimestamp
@CreationTimestamp
@Column
(
name
=
"create_time"
)
@Column
(
name
=
"create_time"
)
private
Timestamp
createTime
;
private
Timestamp
createTime
;
...
...
eladmin-system/src/main/java/me/zhengjie/modules/quartz/repository/QuartzJobRepository.java
View file @
bf7c1eeb
...
@@ -9,11 +9,11 @@ import java.util.List;
...
@@ -9,11 +9,11 @@ import java.util.List;
* @author Zheng Jie
* @author Zheng Jie
* @date 2019-01-07
* @date 2019-01-07
*/
*/
public
interface
QuartzJobRepository
extends
JpaRepository
<
QuartzJob
,
Long
>,
JpaSpecificationExecutor
{
public
interface
QuartzJobRepository
extends
JpaRepository
<
QuartzJob
,
Long
>,
JpaSpecificationExecutor
<
QuartzJob
>
{
/**
/**
* 查询启用的任务
* 查询启用的任务
* @return
* @return
List
*/
*/
List
<
QuartzJob
>
findByIsPauseIsFalse
();
List
<
QuartzJob
>
findByIsPauseIsFalse
();
}
}
eladmin-system/src/main/java/me/zhengjie/modules/quartz/repository/QuartzLogRepository.java
View file @
bf7c1eeb
...
@@ -8,6 +8,6 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
...
@@ -8,6 +8,6 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
* @author Zheng Jie
* @author Zheng Jie
* @date 2019-01-07
* @date 2019-01-07
*/
*/
public
interface
QuartzLogRepository
extends
JpaRepository
<
QuartzLog
,
Long
>,
JpaSpecificationExecutor
{
public
interface
QuartzLogRepository
extends
JpaRepository
<
QuartzLog
,
Long
>,
JpaSpecificationExecutor
<
QuartzLog
>
{
}
}
eladmin-system/src/main/java/me/zhengjie/modules/quartz/rest/QuartzJobController.java
View file @
bf7c1eeb
package
me.zhengjie.modules.quartz.rest
;
package
me.zhengjie.modules.quartz.rest
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
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
;
import
me.zhengjie.modules.quartz.domain.QuartzJob
;
import
me.zhengjie.modules.quartz.domain.QuartzJob
;
import
me.zhengjie.modules.quartz.service.QuartzJobService
;
import
me.zhengjie.modules.quartz.service.QuartzJobService
;
import
me.zhengjie.modules.quartz.service.dto.JobQueryCriteria
;
import
me.zhengjie.modules.quartz.service.dto.JobQueryCriteria
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
...
@@ -20,39 +21,47 @@ import org.springframework.web.bind.annotation.*;
...
@@ -20,39 +21,47 @@ import org.springframework.web.bind.annotation.*;
*/
*/
@Slf4j
@Slf4j
@RestController
@RestController
@RequestMapping
(
"/api"
)
@Api
(
tags
=
"系统:定时任务管理"
)
@RequestMapping
(
"/api/jobs"
)
public
class
QuartzJobController
{
public
class
QuartzJobController
{
private
static
final
String
ENTITY_NAME
=
"quartzJob"
;
private
static
final
String
ENTITY_NAME
=
"quartzJob"
;
@Autowired
private
final
QuartzJobService
quartzJobService
;
private
QuartzJobService
quartzJobService
;
public
QuartzJobController
(
QuartzJobService
quartzJobService
)
{
this
.
quartzJobService
=
quartzJobService
;
}
@Log
(
"查询定时任务"
)
@Log
(
"查询定时任务"
)
@GetMapping
(
value
=
"/jobs"
)
@ApiOperation
(
"查询定时任务"
)
@GetMapping
@PreAuthorize
(
"hasAnyRole('ADMIN','JOB_ALL','JOB_SELECT')"
)
@PreAuthorize
(
"hasAnyRole('ADMIN','JOB_ALL','JOB_SELECT')"
)
public
ResponseEntity
getJobs
(
JobQueryCriteria
criteria
,
Pageable
pageable
){
public
ResponseEntity
getJobs
(
JobQueryCriteria
criteria
,
Pageable
pageable
){
return
new
ResponseEntity
(
quartzJobService
.
queryAll
(
criteria
,
pageable
),
HttpStatus
.
OK
);
return
new
ResponseEntity
<>
(
quartzJobService
.
queryAll
(
criteria
,
pageable
),
HttpStatus
.
OK
);
}
}
@GetMapping
(
value
=
"/jobLogs"
)
@ApiOperation
(
"查询任务执行日志"
)
@GetMapping
(
value
=
"/logs"
)
@PreAuthorize
(
"hasAnyRole('ADMIN','JOB_ALL','JOB_SELECT')"
)
@PreAuthorize
(
"hasAnyRole('ADMIN','JOB_ALL','JOB_SELECT')"
)
public
ResponseEntity
getJobLogs
(
JobQueryCriteria
criteria
,
Pageable
pageable
){
public
ResponseEntity
getJobLogs
(
JobQueryCriteria
criteria
,
Pageable
pageable
){
return
new
ResponseEntity
(
quartzJobService
.
queryAllLog
(
criteria
,
pageable
),
HttpStatus
.
OK
);
return
new
ResponseEntity
<>
(
quartzJobService
.
queryAllLog
(
criteria
,
pageable
),
HttpStatus
.
OK
);
}
}
@Log
(
"新增定时任务"
)
@Log
(
"新增定时任务"
)
@PostMapping
(
value
=
"/jobs"
)
@ApiOperation
(
"新增定时任务"
)
@PostMapping
@PreAuthorize
(
"hasAnyRole('ADMIN','JOB_ALL','JOB_CREATE')"
)
@PreAuthorize
(
"hasAnyRole('ADMIN','JOB_ALL','JOB_CREATE')"
)
public
ResponseEntity
create
(
@Validated
@RequestBody
QuartzJob
resources
){
public
ResponseEntity
create
(
@Validated
@RequestBody
QuartzJob
resources
){
if
(
resources
.
getId
()
!=
null
)
{
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"
);
}
}
return
new
ResponseEntity
(
quartzJobService
.
create
(
resources
),
HttpStatus
.
CREATED
);
return
new
ResponseEntity
<>
(
quartzJobService
.
create
(
resources
),
HttpStatus
.
CREATED
);
}
}
@Log
(
"修改定时任务"
)
@Log
(
"修改定时任务"
)
@PutMapping
(
value
=
"/jobs"
)
@ApiOperation
(
"修改定时任务"
)
@PutMapping
@PreAuthorize
(
"hasAnyRole('ADMIN','JOB_ALL','JOB_EDIT')"
)
@PreAuthorize
(
"hasAnyRole('ADMIN','JOB_ALL','JOB_EDIT')"
)
public
ResponseEntity
update
(
@Validated
(
QuartzJob
.
Update
.
class
)
@RequestBody
QuartzJob
resources
){
public
ResponseEntity
update
(
@Validated
(
QuartzJob
.
Update
.
class
)
@RequestBody
QuartzJob
resources
){
quartzJobService
.
update
(
resources
);
quartzJobService
.
update
(
resources
);
...
@@ -60,7 +69,8 @@ public class QuartzJobController {
...
@@ -60,7 +69,8 @@ public class QuartzJobController {
}
}
@Log
(
"更改定时任务状态"
)
@Log
(
"更改定时任务状态"
)
@PutMapping
(
value
=
"/jobs/{id}"
)
@ApiOperation
(
"更改定时任务状态"
)
@PutMapping
(
value
=
"/{id}"
)
@PreAuthorize
(
"hasAnyRole('ADMIN','JOB_ALL','JOB_EDIT')"
)
@PreAuthorize
(
"hasAnyRole('ADMIN','JOB_ALL','JOB_EDIT')"
)
public
ResponseEntity
updateIsPause
(
@PathVariable
Long
id
){
public
ResponseEntity
updateIsPause
(
@PathVariable
Long
id
){
quartzJobService
.
updateIsPause
(
quartzJobService
.
findById
(
id
));
quartzJobService
.
updateIsPause
(
quartzJobService
.
findById
(
id
));
...
@@ -68,7 +78,8 @@ public class QuartzJobController {
...
@@ -68,7 +78,8 @@ public class QuartzJobController {
}
}
@Log
(
"执行定时任务"
)
@Log
(
"执行定时任务"
)
@PutMapping
(
value
=
"/jobs/exec/{id}"
)
@ApiOperation
(
"执行定时任务"
)
@PutMapping
(
value
=
"/exec/{id}"
)
@PreAuthorize
(
"hasAnyRole('ADMIN','JOB_ALL','JOB_EDIT')"
)
@PreAuthorize
(
"hasAnyRole('ADMIN','JOB_ALL','JOB_EDIT')"
)
public
ResponseEntity
execution
(
@PathVariable
Long
id
){
public
ResponseEntity
execution
(
@PathVariable
Long
id
){
quartzJobService
.
execution
(
quartzJobService
.
findById
(
id
));
quartzJobService
.
execution
(
quartzJobService
.
findById
(
id
));
...
@@ -76,7 +87,8 @@ public class QuartzJobController {
...
@@ -76,7 +87,8 @@ public class QuartzJobController {
}
}
@Log
(
"删除定时任务"
)
@Log
(
"删除定时任务"
)
@DeleteMapping
(
value
=
"/jobs/{id}"
)
@ApiOperation
(
"删除定时任务"
)
@DeleteMapping
(
value
=
"/{id}"
)
@PreAuthorize
(
"hasAnyRole('ADMIN','JOB_ALL','JOB_DELETE')"
)
@PreAuthorize
(
"hasAnyRole('ADMIN','JOB_ALL','JOB_DELETE')"
)
public
ResponseEntity
delete
(
@PathVariable
Long
id
){
public
ResponseEntity
delete
(
@PathVariable
Long
id
){
quartzJobService
.
delete
(
quartzJobService
.
findById
(
id
));
quartzJobService
.
delete
(
quartzJobService
.
findById
(
id
));
...
...
eladmin-system/src/main/java/me/zhengjie/modules/quartz/service/QuartzJobService.java
View file @
bf7c1eeb
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
;
import
me.zhengjie.modules.quartz.domain.QuartzLog
;
import
me.zhengjie.modules.quartz.service.dto.JobQueryCriteria
;
import
me.zhengjie.modules.quartz.service.dto.JobQueryCriteria
;
import
org.springframework.cache.annotation.CacheConfig
;
import
org.springframework.cache.annotation.CacheEvict
;
import
org.springframework.cache.annotation.Cacheable
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.domain.Pageable
;
/**
/**
* @author Zheng Jie
* @author Zheng Jie
* @date 2019-01-07
* @date 2019-01-07
*/
*/
@CacheConfig
(
cacheNames
=
"quartzJob"
)
public
interface
QuartzJobService
{
public
interface
QuartzJobService
{
/**
* queryAll quartzJob
* @param criteria
* @param pageable
* @return
*/
@Cacheable
Object
queryAll
(
JobQueryCriteria
criteria
,
Pageable
pageable
);
Object
queryAll
(
JobQueryCriteria
criteria
,
Pageable
pageable
);
/**
* queryAll quartzLog
* @param criteria
* @param pageable
* @return
*/
Object
queryAllLog
(
JobQueryCriteria
criteria
,
Pageable
pageable
);
Object
queryAllLog
(
JobQueryCriteria
criteria
,
Pageable
pageable
);
/**
* create
* @param resources
* @return
*/
@CacheEvict
(
allEntries
=
true
)
QuartzJob
create
(
QuartzJob
resources
);
QuartzJob
create
(
QuartzJob
resources
);
/**
* update
* @param resources
* @return
*/
@CacheEvict
(
allEntries
=
true
)
void
update
(
QuartzJob
resources
);
void
update
(
QuartzJob
resources
);
/**
* del
* @param quartzJob
*/
@CacheEvict
(
allEntries
=
true
)
void
delete
(
QuartzJob
quartzJob
);
void
delete
(
QuartzJob
quartzJob
);
/**
* findById
* @param id
* @return
*/
@Cacheable
(
key
=
"#p0"
)
QuartzJob
findById
(
Long
id
);
QuartzJob
findById
(
Long
id
);
/**
/**
* 更改定时任务状态
* 更改定时任务状态
* @param quartzJob
* @param quartzJob
/
*/
*/
@CacheEvict
(
allEntries
=
true
)
void
updateIsPause
(
QuartzJob
quartzJob
);
void
updateIsPause
(
QuartzJob
quartzJob
);
/**
/**
* 立即执行定时任务
* 立即执行定时任务
* @param quartzJob
* @param quartzJob
/
*/
*/
void
execution
(
QuartzJob
quartzJob
);
void
execution
(
QuartzJob
quartzJob
);
}
}
eladmin-system/src/main/java/me/zhengjie/modules/quartz/service/impl/QuartzJobServiceImpl.java
View file @
bf7c1eeb
...
@@ -11,31 +11,37 @@ import me.zhengjie.utils.PageUtil;
...
@@ -11,31 +11,37 @@ import me.zhengjie.utils.PageUtil;
import
me.zhengjie.utils.QueryHelp
;
import
me.zhengjie.utils.QueryHelp
;
import
me.zhengjie.utils.ValidationUtil
;
import
me.zhengjie.utils.ValidationUtil
;
import
org.quartz.CronExpression
;
import
org.quartz.CronExpression
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cache.annotation.CacheConfig
;
import
org.springframework.cache.annotation.CacheEvict
;
import
org.springframework.cache.annotation.Cacheable
;
import
org.springframework.data.domain.Pageable
;
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
java.util.Optional
;
/**
/**
* @author Zheng Jie
* @author Zheng Jie
* @date 2019-01-07
* @date 2019-01-07
*/
*/
@Service
(
value
=
"quartzJobService"
)
@Service
(
value
=
"quartzJobService"
)
@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
{
@Autowired
private
final
QuartzJobRepository
quartzJobRepository
;
private
QuartzJobRepository
quartzJobRepository
;
@Autowired
private
final
QuartzLogRepository
quartzLogRepository
;
private
QuartzLogRepository
quartzLogRepository
;
@Autowired
private
final
QuartzManage
quartzManage
;
private
QuartzManage
quartzManage
;
public
QuartzJobServiceImpl
(
QuartzJobRepository
quartzJobRepository
,
QuartzLogRepository
quartzLogRepository
,
QuartzManage
quartzManage
)
{
this
.
quartzJobRepository
=
quartzJobRepository
;
this
.
quartzLogRepository
=
quartzLogRepository
;
this
.
quartzManage
=
quartzManage
;
}
@Override
@Override
@Cacheable
public
Object
queryAll
(
JobQueryCriteria
criteria
,
Pageable
pageable
){
public
Object
queryAll
(
JobQueryCriteria
criteria
,
Pageable
pageable
){
return
PageUtil
.
toPage
(
quartzJobRepository
.
findAll
((
root
,
criteriaQuery
,
criteriaBuilder
)
->
QueryHelp
.
getPredicate
(
root
,
criteria
,
criteriaBuilder
),
pageable
));
return
PageUtil
.
toPage
(
quartzJobRepository
.
findAll
((
root
,
criteriaQuery
,
criteriaBuilder
)
->
QueryHelp
.
getPredicate
(
root
,
criteria
,
criteriaBuilder
),
pageable
));
}
}
...
@@ -46,13 +52,15 @@ public class QuartzJobServiceImpl implements QuartzJobService {
...
@@ -46,13 +52,15 @@ public class QuartzJobServiceImpl implements QuartzJobService {
}
}
@Override
@Override
@Cacheable
(
key
=
"#p0"
)
public
QuartzJob
findById
(
Long
id
)
{
public
QuartzJob
findById
(
Long
id
)
{
Optional
<
QuartzJob
>
quartzJob
=
quartzJobRepository
.
findById
(
id
);
QuartzJob
quartzJob
=
quartzJobRepository
.
findById
(
id
)
.
orElseGet
(
QuartzJob:
:
new
)
;
ValidationUtil
.
isNull
(
quartzJob
,
"QuartzJob"
,
"id"
,
id
);
ValidationUtil
.
isNull
(
quartzJob
.
getId
()
,
"QuartzJob"
,
"id"
,
id
);
return
quartzJob
.
get
()
;
return
quartzJob
;
}
}
@Override
@Override
@CacheEvict
(
allEntries
=
true
)
@Transactional
(
rollbackFor
=
Exception
.
class
)
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
QuartzJob
create
(
QuartzJob
resources
)
{
public
QuartzJob
create
(
QuartzJob
resources
)
{
if
(!
CronExpression
.
isValidExpression
(
resources
.
getCronExpression
())){
if
(!
CronExpression
.
isValidExpression
(
resources
.
getCronExpression
())){
...
@@ -64,6 +72,7 @@ public class QuartzJobServiceImpl implements QuartzJobService {
...
@@ -64,6 +72,7 @@ public class QuartzJobServiceImpl implements QuartzJobService {
}
}
@Override
@Override
@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
)){
if
(
resources
.
getId
().
equals
(
1L
)){
...
@@ -77,6 +86,7 @@ public class QuartzJobServiceImpl implements QuartzJobService {
...
@@ -77,6 +86,7 @@ public class QuartzJobServiceImpl implements QuartzJobService {
}
}
@Override
@Override
@CacheEvict
(
allEntries
=
true
)
public
void
updateIsPause
(
QuartzJob
quartzJob
)
{
public
void
updateIsPause
(
QuartzJob
quartzJob
)
{
if
(
quartzJob
.
getId
().
equals
(
1L
)){
if
(
quartzJob
.
getId
().
equals
(
1L
)){
throw
new
BadRequestException
(
"该任务不可操作"
);
throw
new
BadRequestException
(
"该任务不可操作"
);
...
@@ -100,6 +110,7 @@ public class QuartzJobServiceImpl implements QuartzJobService {
...
@@ -100,6 +110,7 @@ public class QuartzJobServiceImpl implements QuartzJobService {
}
}
@Override
@Override
@CacheEvict
(
allEntries
=
true
)
@Transactional
(
rollbackFor
=
Exception
.
class
)
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
delete
(
QuartzJob
quartzJob
)
{
public
void
delete
(
QuartzJob
quartzJob
)
{
if
(
quartzJob
.
getId
().
equals
(
1L
)){
if
(
quartzJob
.
getId
().
equals
(
1L
)){
...
...
eladmin-system/src/main/java/me/zhengjie/modules/quartz/task/VisitsTask.java
View file @
bf7c1eeb
package
me.zhengjie.modules.quartz.task
;
package
me.zhengjie.modules.quartz.task
;
import
me.zhengjie.modules.monitor.service.VisitsService
;
import
me.zhengjie.modules.monitor.service.VisitsService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
/**
/**
...
@@ -11,8 +10,11 @@ import org.springframework.stereotype.Component;
...
@@ -11,8 +10,11 @@ import org.springframework.stereotype.Component;
@Component
@Component
public
class
VisitsTask
{
public
class
VisitsTask
{
@Autowired
private
final
VisitsService
visitsService
;
private
VisitsService
visitsService
;
public
VisitsTask
(
VisitsService
visitsService
)
{
this
.
visitsService
=
visitsService
;
}
public
void
run
(){
public
void
run
(){
visitsService
.
save
();
visitsService
.
save
();
...
...
eladmin-system/src/main/java/me/zhengjie/modules/quartz/utils/ExecutionJob.java
View file @
bf7c1eeb
...
@@ -17,7 +17,7 @@ import java.util.concurrent.Future;
...
@@ -17,7 +17,7 @@ import java.util.concurrent.Future;
/**
/**
* 参考人人开源,https://gitee.com/renrenio/renren-security
* 参考人人开源,https://gitee.com/renrenio/renren-security
* @author
* @author
/
* @date 2019-01-07
* @date 2019-01-07
*/
*/
@Async
@Async
...
@@ -32,9 +32,8 @@ public class ExecutionJob extends QuartzJobBean {
...
@@ -32,9 +32,8 @@ public class ExecutionJob extends QuartzJobBean {
protected
void
executeInternal
(
JobExecutionContext
context
)
{
protected
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"
);
QuartzLogRepository
quartzLogRepository
=
SpringContextHolder
.
getBean
(
QuartzLogRepository
.
class
);
QuartzJobService
quartzJobService
=
SpringContextHolder
.
getBean
(
"quartzJobService"
);
QuartzJobService
quartzJobService
=
SpringContextHolder
.
getBean
(
QuartzJobService
.
class
);
QuartzManage
quartzManage
=
SpringContextHolder
.
getBean
(
"quartzManage"
);
QuartzLog
log
=
new
QuartzLog
();
QuartzLog
log
=
new
QuartzLog
();
log
.
setJobName
(
quartzJob
.
getJobName
());
log
.
setJobName
(
quartzJob
.
getJobName
());
...
...
eladmin-system/src/main/java/me/zhengjie/modules/quartz/utils/QuartzManage.java
View file @
bf7c1eeb
...
@@ -56,8 +56,7 @@ public class QuartzManage {
...
@@ -56,8 +56,7 @@ public class QuartzManage {
/**
/**
* 更新job cron表达式
* 更新job cron表达式
* @param quartzJob
* @param quartzJob /
* @throws SchedulerException
*/
*/
public
void
updateJobCron
(
QuartzJob
quartzJob
){
public
void
updateJobCron
(
QuartzJob
quartzJob
){
try
{
try
{
...
@@ -88,8 +87,7 @@ public class QuartzManage {
...
@@ -88,8 +87,7 @@ public class QuartzManage {
/**
/**
* 删除一个job
* 删除一个job
* @param quartzJob
* @param quartzJob /
* @throws SchedulerException
*/
*/
public
void
deleteJob
(
QuartzJob
quartzJob
){
public
void
deleteJob
(
QuartzJob
quartzJob
){
try
{
try
{
...
@@ -104,8 +102,7 @@ public class QuartzManage {
...
@@ -104,8 +102,7 @@ public class QuartzManage {
/**
/**
* 恢复一个job
* 恢复一个job
* @param quartzJob
* @param quartzJob /
* @throws SchedulerException
*/
*/
public
void
resumeJob
(
QuartzJob
quartzJob
){
public
void
resumeJob
(
QuartzJob
quartzJob
){
try
{
try
{
...
@@ -124,8 +121,7 @@ public class QuartzManage {
...
@@ -124,8 +121,7 @@ public class QuartzManage {
/**
/**
* 立即执行job
* 立即执行job
* @param quartzJob
* @param quartzJob /
* @throws SchedulerException
*/
*/
public
void
runAJobNow
(
QuartzJob
quartzJob
){
public
void
runAJobNow
(
QuartzJob
quartzJob
){
try
{
try
{
...
@@ -146,8 +142,7 @@ public class QuartzManage {
...
@@ -146,8 +142,7 @@ public class QuartzManage {
/**
/**
* 暂停一个job
* 暂停一个job
* @param quartzJob
* @param quartzJob /
* @throws SchedulerException
*/
*/
public
void
pauseJob
(
QuartzJob
quartzJob
){
public
void
pauseJob
(
QuartzJob
quartzJob
){
try
{
try
{
...
...
eladmin-system/src/main/java/me/zhengjie/modules/quartz/utils/QuartzRunnable.java
View file @
bf7c1eeb
package
me.zhengjie.modules.quartz.utils
;
package
me.zhengjie.modules.quartz.utils
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
me.zhengjie.exception.BadRequestException
;
import
me.zhengjie.utils.SpringContextHolder
;
import
me.zhengjie.utils.SpringContextHolder
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.util.ReflectionUtils
;
import
org.springframework.util.ReflectionUtils
;
...
@@ -10,7 +9,7 @@ import java.util.concurrent.Callable;
...
@@ -10,7 +9,7 @@ import java.util.concurrent.Callable;
/**
/**
* 执行定时任务
* 执行定时任务
* @author
* @author
/
*/
*/
@Slf4j
@Slf4j
public
class
QuartzRunnable
implements
Callable
{
public
class
QuartzRunnable
implements
Callable
{
...
...
eladmin-system/src/main/java/me/zhengjie/modules/security/config/SecurityConfig.java
View file @
bf7c1eeb
...
@@ -25,23 +25,21 @@ import org.springframework.security.web.authentication.UsernamePasswordAuthentic
...
@@ -25,23 +25,21 @@ import org.springframework.security.web.authentication.UsernamePasswordAuthentic
@EnableGlobalMethodSecurity
(
prePostEnabled
=
true
)
@EnableGlobalMethodSecurity
(
prePostEnabled
=
true
)
public
class
SecurityConfig
extends
WebSecurityConfigurerAdapter
{
public
class
SecurityConfig
extends
WebSecurityConfigurerAdapter
{
@Autowired
private
final
JwtAuthenticationEntryPoint
unauthorizedHandler
;
private
JwtAuthenticationEntryPoint
unauthorizedHandler
;
@Autowired
private
final
JwtUserDetailsService
jwtUserDetailsService
;
private
JwtUserDetailsService
jwtUserDetailsService
;
/**
// 自定义基于JWT的安全过滤器
* 自定义基于JWT的安全过滤器
private
final
JwtAuthorizationTokenFilter
authenticationTokenFilter
;
*/
@Autowired
JwtAuthorizationTokenFilter
authenticationTokenFilter
;
@Value
(
"${jwt.header}"
)
@Value
(
"${jwt.header}"
)
private
String
tokenHeader
;
private
String
tokenHeader
;
@Value
(
"${jwt.auth.path}"
)
public
SecurityConfig
(
JwtAuthenticationEntryPoint
unauthorizedHandler
,
JwtUserDetailsService
jwtUserDetailsService
,
JwtAuthorizationTokenFilter
authenticationTokenFilter
)
{
private
String
loginPath
;
this
.
unauthorizedHandler
=
unauthorizedHandler
;
this
.
jwtUserDetailsService
=
jwtUserDetailsService
;
this
.
authenticationTokenFilter
=
authenticationTokenFilter
;
}
@Autowired
@Autowired
public
void
configureGlobal
(
AuthenticationManagerBuilder
auth
)
throws
Exception
{
public
void
configureGlobal
(
AuthenticationManagerBuilder
auth
)
throws
Exception
{
...
@@ -70,6 +68,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
...
@@ -70,6 +68,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
@Override
protected
void
configure
(
HttpSecurity
httpSecurity
)
throws
Exception
{
protected
void
configure
(
HttpSecurity
httpSecurity
)
throws
Exception
{
String
loginPath
=
"login"
;
httpSecurity
httpSecurity
// 禁用 CSRF
// 禁用 CSRF
...
@@ -91,7 +90,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
...
@@ -91,7 +90,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
"/**/*.js"
"/**/*.js"
).
anonymous
()
).
anonymous
()
.
antMatchers
(
HttpMethod
.
POST
,
"/auth/"
+
loginPath
).
anonymous
()
.
antMatchers
(
HttpMethod
.
POST
,
"/auth/"
+
loginPath
).
anonymous
()
.
antMatchers
(
"/auth/vCode"
).
anonymous
()
.
antMatchers
(
"/auth/vCode"
).
anonymous
()
// 支付宝回调
// 支付宝回调
.
antMatchers
(
"/api/aliPay/return"
).
anonymous
()
.
antMatchers
(
"/api/aliPay/return"
).
anonymous
()
...
...
eladmin-system/src/main/java/me/zhengjie/modules/security/rest/AuthenticationController.java
View file @
bf7c1eeb
...
@@ -2,6 +2,8 @@ package me.zhengjie.modules.security.rest;
...
@@ -2,6 +2,8 @@ package me.zhengjie.modules.security.rest;
import
cn.hutool.core.codec.Base64
;
import
cn.hutool.core.codec.Base64
;
import
cn.hutool.core.util.IdUtil
;
import
cn.hutool.core.util.IdUtil
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
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
;
...
@@ -15,7 +17,6 @@ import me.zhengjie.utils.EncryptUtils;
...
@@ -15,7 +17,6 @@ import me.zhengjie.utils.EncryptUtils;
import
me.zhengjie.modules.security.utils.JwtTokenUtil
;
import
me.zhengjie.modules.security.utils.JwtTokenUtil
;
import
me.zhengjie.utils.SecurityUtils
;
import
me.zhengjie.utils.SecurityUtils
;
import
me.zhengjie.utils.StringUtils
;
import
me.zhengjie.utils.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
...
@@ -23,7 +24,6 @@ import org.springframework.security.authentication.AccountExpiredException;
...
@@ -23,7 +24,6 @@ import org.springframework.security.authentication.AccountExpiredException;
import
org.springframework.security.core.userdetails.UserDetailsService
;
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.HttpServletResponse
;
import
java.io.ByteArrayOutputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
...
@@ -34,29 +34,28 @@ import java.io.IOException;
...
@@ -34,29 +34,28 @@ import java.io.IOException;
*/
*/
@Slf4j
@Slf4j
@RestController
@RestController
@RequestMapping
(
"auth"
)
@RequestMapping
(
"/auth"
)
@Api
(
tags
=
"系统:系统授权接口"
)
public
class
AuthenticationController
{
public
class
AuthenticationController
{
@Value
(
"${jwt.header}"
)
@Value
(
"${jwt.header}"
)
private
String
tokenHeader
;
private
String
tokenHeader
;
@Autowired
private
final
JwtTokenUtil
jwtTokenUtil
;
private
JwtTokenUtil
jwtTokenUtil
;
@Autowired
private
final
RedisService
redisService
;
private
RedisService
redisService
;
@Autowired
private
final
UserDetailsService
userDetailsService
;
@Qualifier
(
"jwtUserDetailsService"
)
private
UserDetailsService
userDetailsService
;
public
AuthenticationController
(
JwtTokenUtil
jwtTokenUtil
,
RedisService
redisService
,
@Qualifier
(
"jwtUserDetailsService"
)
UserDetailsService
userDetailsService
)
{
this
.
jwtTokenUtil
=
jwtTokenUtil
;
this
.
redisService
=
redisService
;
this
.
userDetailsService
=
userDetailsService
;
}
/**
* 登录授权
* @param authorizationUser
* @return
*/
@Log
(
"用户登录"
)
@Log
(
"用户登录"
)
@PostMapping
(
value
=
"${jwt.auth.path}"
)
@ApiOperation
(
"登录授权"
)
@PostMapping
(
value
=
"/login"
)
public
ResponseEntity
login
(
@Validated
@RequestBody
AuthorizationUser
authorizationUser
){
public
ResponseEntity
login
(
@Validated
@RequestBody
AuthorizationUser
authorizationUser
){
// 查询验证码
// 查询验证码
...
@@ -86,21 +85,16 @@ public class AuthenticationController {
...
@@ -86,21 +85,16 @@ public class AuthenticationController {
return
ResponseEntity
.
ok
(
new
AuthenticationInfo
(
token
,
jwtUser
));
return
ResponseEntity
.
ok
(
new
AuthenticationInfo
(
token
,
jwtUser
));
}
}
/**
@ApiOperation
(
"获取用户信息"
)
* 获取用户信息
@GetMapping
(
value
=
"/info"
)
* @return
*/
@GetMapping
(
value
=
"${jwt.auth.account}"
)
public
ResponseEntity
getUserInfo
(){
public
ResponseEntity
getUserInfo
(){
JwtUser
jwtUser
=
(
JwtUser
)
userDetailsService
.
loadUserByUsername
(
SecurityUtils
.
getUsername
());
JwtUser
jwtUser
=
(
JwtUser
)
userDetailsService
.
loadUserByUsername
(
SecurityUtils
.
getUsername
());
return
ResponseEntity
.
ok
(
jwtUser
);
return
ResponseEntity
.
ok
(
jwtUser
);
}
}
/**
@ApiOperation
(
"获取验证码"
)
* 获取验证码
@GetMapping
(
value
=
"/vCode"
)
*/
public
ImgResult
getCode
()
throws
IOException
{
@GetMapping
(
value
=
"vCode"
)
public
ImgResult
getCode
(
HttpServletResponse
response
)
throws
IOException
{
//生成随机字串
//生成随机字串
String
verifyCode
=
VerifyCodeUtils
.
generateVerifyCode
(
4
);
String
verifyCode
=
VerifyCodeUtils
.
generateVerifyCode
(
4
);
...
...
eladmin-system/src/main/java/me/zhengjie/modules/security/security/JwtAuthenticationEntryPoint.java
View file @
bf7c1eeb
...
@@ -18,9 +18,7 @@ public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Se
...
@@ -18,9 +18,7 @@ public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Se
public
void
commence
(
HttpServletRequest
request
,
public
void
commence
(
HttpServletRequest
request
,
HttpServletResponse
response
,
HttpServletResponse
response
,
AuthenticationException
authException
)
throws
IOException
{
AuthenticationException
authException
)
throws
IOException
{
/**
// 当用户尝试访问安全的REST资源而不提供任何凭据时,将调用此方法发送401 响应
* 当用户尝试访问安全的REST资源而不提供任何凭据时,将调用此方法发送401 响应
*/
response
.
sendError
(
HttpServletResponse
.
SC_UNAUTHORIZED
,
authException
==
null
?
"Unauthorized"
:
authException
.
getMessage
());
response
.
sendError
(
HttpServletResponse
.
SC_UNAUTHORIZED
,
authException
==
null
?
"Unauthorized"
:
authException
.
getMessage
());
}
}
}
}
eladmin-system/src/main/java/me/zhengjie/modules/security/service/JwtPermissionService.java
View file @
bf7c1eeb
...
@@ -3,7 +3,6 @@ package me.zhengjie.modules.security.service;
...
@@ -3,7 +3,6 @@ package me.zhengjie.modules.security.service;
import
me.zhengjie.modules.system.domain.Role
;
import
me.zhengjie.modules.system.domain.Role
;
import
me.zhengjie.modules.system.repository.RoleRepository
;
import
me.zhengjie.modules.system.repository.RoleRepository
;
import
me.zhengjie.modules.system.service.dto.UserDTO
;
import
me.zhengjie.modules.system.service.dto.UserDTO
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cache.annotation.CacheConfig
;
import
org.springframework.cache.annotation.CacheConfig
;
import
org.springframework.cache.annotation.Cacheable
;
import
org.springframework.cache.annotation.Cacheable
;
import
org.springframework.security.core.GrantedAuthority
;
import
org.springframework.security.core.GrantedAuthority
;
...
@@ -17,13 +16,16 @@ import java.util.stream.Collectors;
...
@@ -17,13 +16,16 @@ import java.util.stream.Collectors;
@CacheConfig
(
cacheNames
=
"role"
)
@CacheConfig
(
cacheNames
=
"role"
)
public
class
JwtPermissionService
{
public
class
JwtPermissionService
{
@Autowired
private
final
RoleRepository
roleRepository
;
private
RoleRepository
roleRepository
;
public
JwtPermissionService
(
RoleRepository
roleRepository
)
{
this
.
roleRepository
=
roleRepository
;
}
/**
/**
* key的名称如有修改,请同步修改 UserServiceImpl 中的 update 方法
* key的名称如有修改,请同步修改 UserServiceImpl 中的 update 方法
* @param user
* @param user
用户信息
* @return
* @return
Collection
*/
*/
@Cacheable
(
key
=
"'loadPermissionByUser:' + #p0.username"
)
@Cacheable
(
key
=
"'loadPermissionByUser:' + #p0.username"
)
public
Collection
<
GrantedAuthority
>
mapToGrantedAuthorities
(
UserDTO
user
)
{
public
Collection
<
GrantedAuthority
>
mapToGrantedAuthorities
(
UserDTO
user
)
{
...
...
Prev
1
2
3
4
5
6
7
8
Next
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment