mirror of
https://gitee.com/dromara/MaxKey.git
synced 2026-05-17 22:10:43 +08:00
Dynamic Groups
动态用户组
This commit is contained in:
@@ -24,10 +24,20 @@ import org.maxkey.authz.oauth2.provider.token.TokenStore;
|
||||
import org.maxkey.authz.oauth2.provider.token.store.InMemoryTokenStore;
|
||||
import org.maxkey.authz.oauth2.provider.token.store.JdbcTokenStore;
|
||||
import org.maxkey.authz.oauth2.provider.token.store.RedisTokenStore;
|
||||
import org.maxkey.authz.oidc.idtoken.OIDCIdTokenEnhancer;
|
||||
import org.maxkey.constants.ConstantsProperties;
|
||||
import org.maxkey.crypto.password.opt.impl.TimeBasedOtpAuthn;
|
||||
import org.maxkey.jobs.DynamicGroupsJob;
|
||||
import org.maxkey.persistence.redis.RedisConnectionFactory;
|
||||
import org.maxkey.persistence.service.GroupsService;
|
||||
import org.opensaml.xml.ConfigurationException;
|
||||
import org.quartz.CronScheduleBuilder;
|
||||
import org.quartz.CronTrigger;
|
||||
import org.quartz.JobBuilder;
|
||||
import org.quartz.JobDataMap;
|
||||
import org.quartz.JobDetail;
|
||||
import org.quartz.Scheduler;
|
||||
import org.quartz.SchedulerException;
|
||||
import org.quartz.TriggerBuilder;
|
||||
import org.maxkey.authn.realm.jdbc.JdbcAuthenticationRealm;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -37,6 +47,7 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
@Configuration
|
||||
@@ -44,9 +55,8 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
public class MaxKeyMgtConfig implements InitializingBean {
|
||||
private static final Logger _logger = LoggerFactory.getLogger(MaxKeyMgtConfig.class);
|
||||
|
||||
|
||||
|
||||
@Bean(name = "oauth20JdbcClientDetailsService")
|
||||
|
||||
@Bean(name = "oauth20JdbcClientDetailsService")
|
||||
public JdbcClientDetailsService JdbcClientDetailsService(
|
||||
DataSource dataSource,PasswordEncoder passwordReciprocal) {
|
||||
JdbcClientDetailsService clientDetailsService = new JdbcClientDetailsService(dataSource);
|
||||
@@ -111,9 +121,47 @@ public class MaxKeyMgtConfig implements InitializingBean {
|
||||
return tfaOptAuthn;
|
||||
}
|
||||
|
||||
/**
|
||||
* schedulerJobsInit.
|
||||
* @return schedulerJobsInit
|
||||
* @throws ConfigurationException
|
||||
* @throws SchedulerException
|
||||
*/
|
||||
@Bean(name = "schedulerJobs")
|
||||
public Scheduler schedulerJobs(
|
||||
SchedulerFactoryBean schedulerFactoryBean,
|
||||
GroupsService groupsService,
|
||||
@Value("${config.job.cron.dynamicgroups}") String cronScheduleDynamicGroups
|
||||
) throws SchedulerException {
|
||||
|
||||
Scheduler scheduler = schedulerFactoryBean.getScheduler();
|
||||
dynamicGroupsJob(scheduler,cronScheduleDynamicGroups,groupsService);
|
||||
|
||||
return scheduler;
|
||||
}
|
||||
|
||||
|
||||
private void dynamicGroupsJob(Scheduler scheduler ,
|
||||
String cronSchedule,
|
||||
GroupsService groupsService) throws SchedulerException {
|
||||
JobDetail jobDetail =
|
||||
JobBuilder.newJob(DynamicGroupsJob.class)
|
||||
.withIdentity("DynamicGroupsJob", "DynamicGroups")
|
||||
.build();
|
||||
JobDataMap jobDataMap = new JobDataMap();
|
||||
jobDataMap.put("groupsService", groupsService);
|
||||
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronSchedule);
|
||||
CronTrigger cronTrigger =
|
||||
TriggerBuilder.newTrigger()
|
||||
.withIdentity("triggerDynamicGroups", "DynamicGroups")
|
||||
.usingJobData(jobDataMap)
|
||||
.withSchedule(scheduleBuilder)
|
||||
.build();
|
||||
scheduler.scheduleJob(jobDetail,cronTrigger);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.maxkey.jobs;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.maxkey.domain.Groups;
|
||||
import org.maxkey.persistence.service.GroupsService;
|
||||
import org.quartz.Job;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class DynamicGroupsJob implements Job {
|
||||
final static Logger _logger = LoggerFactory.getLogger(DynamicGroupsJob.class);
|
||||
|
||||
private static GroupsService groupsService = null;
|
||||
|
||||
public static class JOBSTATUS{
|
||||
public static int STOP = 0;
|
||||
public static int RUNNING = 1;
|
||||
public static int FINISHED = 2;
|
||||
}
|
||||
|
||||
private static int jobStatus = JOBSTATUS.STOP;
|
||||
|
||||
@Override
|
||||
public void execute(JobExecutionContext context){
|
||||
if(jobStatus == JOBSTATUS.RUNNING) {
|
||||
_logger.info("DynamicGroupsJob is in running . " );
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.debug("DynamicGroupsJob is running ... " );
|
||||
jobStatus = JOBSTATUS.RUNNING;
|
||||
try {
|
||||
if(groupsService == null) {
|
||||
groupsService = (GroupsService) context.getMergedJobDataMap().get("groupsService");
|
||||
}
|
||||
|
||||
List<Groups> groupsList = groupsService.queryDynamicGroups(null);
|
||||
for(Groups group : groupsList) {
|
||||
_logger.debug("group " + group);
|
||||
groupsService.refreshDynamicGroups(group);
|
||||
}
|
||||
Thread.sleep(10 *1000);
|
||||
_logger.debug("DynamicGroupsJob is success " );
|
||||
}catch(Exception e) {
|
||||
_logger.error("Exception " ,e);
|
||||
jobStatus = JOBSTATUS.STOP;
|
||||
}
|
||||
jobStatus = JOBSTATUS.FINISHED;
|
||||
_logger.debug("DynamicGroupsJob is finished . " );
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.maxkey.jobs;
|
||||
|
||||
public class DynamicRolesJob {
|
||||
|
||||
}
|
||||
@@ -85,6 +85,7 @@ public class GroupsController {
|
||||
_logger.debug("-Add :" + group);
|
||||
|
||||
if (groupsService.insert(group)) {
|
||||
groupsService.refreshDynamicGroups(group);
|
||||
return new Message(WebContext.getI18nValue(ConstantsOperateMessage.INSERT_SUCCESS),MessageType.success);
|
||||
|
||||
} else {
|
||||
@@ -122,6 +123,7 @@ public class GroupsController {
|
||||
_logger.debug("-update group :" + group);
|
||||
|
||||
if (groupsService.update(group)) {
|
||||
groupsService.refreshDynamicGroups(group);
|
||||
return new Message(WebContext.getI18nValue(ConstantsOperateMessage.UPDATE_SUCCESS),MessageType.success);
|
||||
|
||||
} else {
|
||||
@@ -136,7 +138,7 @@ public class GroupsController {
|
||||
public Message delete(@ModelAttribute("group") Groups group) {
|
||||
_logger.debug("-delete group :" + group);
|
||||
|
||||
if (groupsService.remove(group.getId())) {
|
||||
if (groupsService.deleteById(group.getId())) {
|
||||
return new Message(WebContext.getI18nValue(ConstantsOperateMessage.DELETE_SUCCESS),MessageType.success);
|
||||
|
||||
} else {
|
||||
|
||||
@@ -85,6 +85,7 @@ public class RolesController {
|
||||
_logger.debug("-Add :" + role);
|
||||
|
||||
if (rolesService.insert(role)) {
|
||||
rolesService.refreshDynamicRoles(role);
|
||||
return new Message(WebContext.getI18nValue(ConstantsOperateMessage.INSERT_SUCCESS),MessageType.success);
|
||||
|
||||
} else {
|
||||
@@ -122,6 +123,7 @@ public class RolesController {
|
||||
_logger.debug("-update role :" + role);
|
||||
|
||||
if (rolesService.update(role)) {
|
||||
rolesService.refreshDynamicRoles(role);
|
||||
return new Message(WebContext.getI18nValue(ConstantsOperateMessage.UPDATE_SUCCESS),MessageType.success);
|
||||
|
||||
} else {
|
||||
@@ -136,7 +138,7 @@ public class RolesController {
|
||||
public Message delete(@ModelAttribute("role") Roles role) {
|
||||
_logger.debug("-delete role :" + role);
|
||||
|
||||
if (rolesService.remove(role.getId())) {
|
||||
if (rolesService.deleteById(role.getId())) {
|
||||
return new Message(WebContext.getI18nValue(ConstantsOperateMessage.DELETE_SUCCESS),MessageType.success);
|
||||
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user