会话管理优化

This commit is contained in:
shimingxy
2024-12-03 09:08:09 +08:00
parent 3ab9b80dfd
commit 2237c295df
8 changed files with 32 additions and 29 deletions

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.maxkey.listener;
package org.dromara.maxkey.authn.listener;
import java.io.Serializable;
import java.util.Date;
@@ -36,6 +36,8 @@ public class SessionListenerAdapter extends ScheduleAdapter implements Job , S
transient SessionManager sessionManager;
Integer category;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
if(jobStatus == JOBSTATUS.RUNNING) {return;}
@@ -46,7 +48,7 @@ public class SessionListenerAdapter extends ScheduleAdapter implements Job , S
try {
if(sessionManager != null) {
int sessionCount = 0;
for (HistoryLogin login : sessionManager.querySessions()) {
for (HistoryLogin login : sessionManager.querySessions(category)) {
Session session = sessionManager.get(login.getSessionId());
if(session == null) {
logger.debug("TimeOut user {} session {} Login at {} and at {} ." ,
@@ -86,6 +88,7 @@ public class SessionListenerAdapter extends ScheduleAdapter implements Job , S
super.init(context);
if(sessionManager == null) {
sessionManager = getParameter("sessionManager",SessionManager.class);
category = getParameter("category",Integer.class);
}
}
}

View File

@@ -34,7 +34,7 @@ public interface SessionManager {
public Session refresh(String sessionId);
public List<HistoryLogin> querySessions();
public List<HistoryLogin> querySessions(Integer category);
public int getValiditySeconds();

View File

@@ -106,7 +106,7 @@ public class InMemorySessionManager implements SessionManager{
}
@Override
public List<HistoryLogin> querySessions() {
public List<HistoryLogin> querySessions(Integer category) {
// not need implement
return null;
}

View File

@@ -128,7 +128,7 @@ public class RedisSessionManager implements SessionManager {
}
@Override
public List<HistoryLogin> querySessions() {
public List<HistoryLogin> querySessions(Integer category) {
// not need implement
return null;
}

View File

@@ -46,8 +46,7 @@ import org.springframework.jdbc.core.RowMapper;
*
*/
public class SessionManagerImpl implements SessionManager{
private static final Logger _logger =
LoggerFactory.getLogger(SessionManagerImpl.class);
private static final Logger _logger = LoggerFactory.getLogger(SessionManagerImpl.class);
private static final String DEFAULT_DEFAULT_SELECT_STATEMENT =
"select id,sessionid,userId,username,displayname,logintime from mxk_history_login where sessionstatus = 1";
@@ -143,12 +142,17 @@ public class SessionManagerImpl implements SessionManager{
}
@Override
public List<HistoryLogin> querySessions() {
public List<HistoryLogin> querySessions(Integer category) {
//clear session id is null
jdbcTemplate.execute(NO_SESSION_UPDATE_STATEMENT);
String sessionSql = DEFAULT_DEFAULT_SELECT_STATEMENT;
if(!isRedis) {
sessionSql = sessionSql + " and category = " + category;
}
_logger.trace("sessionSql {} " ,sessionSql);
//query on line session
List<HistoryLogin> listSessions = jdbcTemplate.query(
DEFAULT_DEFAULT_SELECT_STATEMENT,
sessionSql,
new OnlineTicketRowMapper());
return listSessions;
}

View File

@@ -27,7 +27,7 @@ export const environment = {
production: false,
useHash: true,
api: {
baseUrl: 'http://localhost:9526/maxkey-mgt-api',
baseUrl: 'http://mgt.maxkey.top/maxkey-mgt-api',
refreshTokenEnabled: true,
refreshTokenType: 're-request'
},

View File

@@ -18,11 +18,11 @@
package org.dromara.maxkey.web.contorller;
import java.io.IOException;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.dromara.maxkey.authn.jwt.AuthTokenService;
import org.dromara.maxkey.configuration.ApplicationConfig;
import org.dromara.maxkey.constants.ConstsRegex;
import org.dromara.maxkey.constants.ConstsStatus;
import org.dromara.maxkey.crypto.password.PasswordReciprocal;
import org.dromara.maxkey.entity.Message;
@@ -36,22 +36,21 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import jakarta.servlet.ServletException;
@Controller
@RestController
@RequestMapping(value={"/signup"})
public class RegisterController {
private static Logger logger = LoggerFactory.getLogger(RegisterController.class);
Pattern mobileRegex = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$");
@Autowired
AuthTokenService authTokenService;
@@ -67,14 +66,12 @@ public class RegisterController {
@Autowired
PasswordEncoder passwordEncoder;
@ResponseBody
@RequestMapping(value = { "/produceOtp" }, produces = {MediaType.APPLICATION_JSON_VALUE})
public Message<?> produceOtp(
@RequestParam String mobile) {
@GetMapping(value = { "/produceOtp" }, produces = {MediaType.APPLICATION_JSON_VALUE})
public Message<?> produceOtp(@RequestParam String mobile) {
logger.debug("/signup/produceOtp Mobile {}: " ,mobile);
logger.debug("Mobile Regex matches {}",mobileRegex.matcher(mobile).matches());
if(StringUtils.isNotBlank(mobile) && mobileRegex.matcher(mobile).matches()) {
logger.debug("Mobile Regex matches {}",ConstsRegex.MOBILE_PATTERN.matcher(mobile).matches());
if(StringUtils.isNotBlank(mobile) && ConstsRegex.MOBILE_PATTERN.matcher(mobile).matches()) {
UserInfo userInfo = new UserInfo();
userInfo.setUsername(mobile);
userInfo.setMobile(mobile);
@@ -87,11 +84,8 @@ public class RegisterController {
}
//直接注册
@RequestMapping(value={"/register"})
@ResponseBody
public Message<?> register(
@ModelAttribute UserInfo userInfo,
@RequestParam String captcha) throws ServletException, IOException {
@PostMapping(value={"/register"})
public Message<?> register(@ModelAttribute UserInfo userInfo , @RequestParam String captcha) throws ServletException, IOException {
UserInfo validateUserInfo = new UserInfo();
validateUserInfo.setUsername(userInfo.getMobile());
validateUserInfo.setMobile(userInfo.getMobile());

View File

@@ -17,12 +17,13 @@
package org.dromara.maxkey.autoconfigure;
import org.dromara.maxkey.authn.listener.SessionListenerAdapter;
import org.dromara.maxkey.authn.session.Session.CATEGORY;
import org.dromara.maxkey.authn.session.SessionManager;
import org.dromara.maxkey.configuration.ApplicationConfig;
import org.dromara.maxkey.listener.DynamicGroupsListenerAdapter;
import org.dromara.maxkey.listener.DynamicRolesListenerAdapter;
import org.dromara.maxkey.listener.ReorgDeptListenerAdapter;
import org.dromara.maxkey.listener.SessionListenerAdapter;
import org.dromara.maxkey.persistence.service.ConnectorsService;
import org.dromara.maxkey.persistence.service.GroupsService;
import org.dromara.maxkey.persistence.service.OrganizationsService;
@@ -52,6 +53,7 @@ public class MaxKeyMgtListenerConfig {
.setCron("0 0/10 * * * ?")
.setJobClass(SessionListenerAdapter.class)
.setJobData("sessionManager",sessionManager)
.setJobData("category", CATEGORY.MGMT)
.build();
logger.debug("Session ListenerAdapter inited .");
return "sessionListenerAdapter";