1、完善用户登录缓存
pull/411/head
liaojinlong 2020-06-16 10:27:33 +08:00
parent 22e6bdef8e
commit d49aedda01
3 changed files with 26 additions and 9 deletions

View File

@ -19,9 +19,12 @@ import lombok.RequiredArgsConstructor;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import java.util.List; import java.util.List;
/** /**
@ -31,19 +34,20 @@ import java.util.List;
@Component @Component
@RequiredArgsConstructor @RequiredArgsConstructor
public class JobRunner implements ApplicationRunner { public class JobRunner implements ApplicationRunner {
private static final Logger log = LoggerFactory.getLogger(JobRunner.class);
private final QuartzJobRepository quartzJobRepository; private final QuartzJobRepository quartzJobRepository;
private final QuartzManage quartzManage; private final QuartzManage quartzManage;
/** /**
* *
*
* @param applicationArguments / * @param applicationArguments /
*/ */
@Override @Override
public void run(ApplicationArguments applicationArguments){ public void run(ApplicationArguments applicationArguments) {
System.out.println("--------------------注入定时任务---------------------"); log.info("--------------------注入定时任务---------------------");
List<QuartzJob> quartzJobs = quartzJobRepository.findByIsPauseIsFalse(); List<QuartzJob> quartzJobs = quartzJobRepository.findByIsPauseIsFalse();
quartzJobs.forEach(quartzManage::addJob); quartzJobs.forEach(quartzManage::addJob);
System.out.println("--------------------定时任务注入完成---------------------"); log.info("--------------------定时任务注入完成---------------------");
} }
} }

View File

@ -19,10 +19,12 @@ package me.zhengjie.modules.security.config.bean;
import com.wf.captcha.*; import com.wf.captcha.*;
import com.wf.captcha.base.Captcha; import com.wf.captcha.base.Captcha;
import me.zhengjie.exception.BadConfigurationException; import me.zhengjie.exception.BadConfigurationException;
import java.util.Objects; import java.util.Objects;
/** /**
* *
*
* @author liaojinlong * @author liaojinlong
* @date loginCode.length0loginCode.length0/6/10 17:loginCode.length6 * @date loginCode.length0loginCode.length0/6/10 17:loginCode.length6
*/ */
@ -34,6 +36,10 @@ public class LoginProperties {
private boolean singleLogin = false; private boolean singleLogin = false;
private LoginCode loginCode; private LoginCode loginCode;
/**
*
*/
private boolean cacheEnable;
public boolean isSingleLogin() { public boolean isSingleLogin() {
return singleLogin; return singleLogin;
@ -51,6 +57,14 @@ public class LoginProperties {
this.loginCode = loginCode; this.loginCode = loginCode;
} }
public boolean isCacheEnable() {
return cacheEnable;
}
public void setCacheEnable(boolean cacheEnable) {
this.cacheEnable = cacheEnable;
}
/** /**
* *
* *

View File

@ -18,12 +18,12 @@ package me.zhengjie.modules.security.service;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.exception.EntityNotFoundException; import me.zhengjie.exception.EntityNotFoundException;
import me.zhengjie.modules.security.config.bean.LoginProperties;
import me.zhengjie.modules.security.service.dto.JwtUserDto; import me.zhengjie.modules.security.service.dto.JwtUserDto;
import me.zhengjie.modules.system.service.DataService; import me.zhengjie.modules.system.service.DataService;
import me.zhengjie.modules.system.service.RoleService; import me.zhengjie.modules.system.service.RoleService;
import me.zhengjie.modules.system.service.UserService; import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.UserDto; import me.zhengjie.modules.system.service.dto.UserDto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -41,11 +41,10 @@ public class UserDetailsServiceImpl implements UserDetailsService {
private final UserService userService; private final UserService userService;
private final RoleService roleService; private final RoleService roleService;
private final DataService dataService; private final DataService dataService;
@Value("${login.cache-enable:true}") private final LoginProperties loginProperties;
private boolean enableCache = true;
public void setEnableCache(boolean enableCache) { public void setEnableCache(boolean enableCache) {
this.enableCache = enableCache; this.loginProperties.setCacheEnable(enableCache);
} }
/** /**
@ -59,7 +58,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
public JwtUserDto loadUserByUsername(String username) { public JwtUserDto loadUserByUsername(String username) {
boolean searchDb = true; boolean searchDb = true;
JwtUserDto jwtUserDto = null; JwtUserDto jwtUserDto = null;
if (enableCache && userDtoCache.containsKey(username)) { if (loginProperties.isCacheEnable() && userDtoCache.containsKey(username)) {
jwtUserDto = userDtoCache.get(username); jwtUserDto = userDtoCache.get(username);
searchDb = false; searchDb = false;
} }