v2.9.0 FIX

This commit is contained in:
MaxKey
2021-08-23 11:12:49 +08:00
parent 68d462003a
commit bc91908667
14 changed files with 197 additions and 162 deletions

View File

@@ -25,6 +25,9 @@ import javax.servlet.Filter;
import org.maxkey.constants.ConstantsProperties;
import org.maxkey.constants.ConstantsTimeInterval;
import org.maxkey.persistence.db.LoginHistoryService;
import org.maxkey.persistence.db.LoginService;
import org.maxkey.web.SessionListenerAdapter;
import org.maxkey.web.WebXssRequestFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -286,6 +289,17 @@ public class MvcAutoConfiguration implements InitializingBean {
return registrationBean;
}
@Bean(name = "sessionListenerAdapter")
public SessionListenerAdapter sessionListenerAdapter(
LoginService loginService,
LoginHistoryService loginHistoryService
) {
SessionListenerAdapter sessionListenerAdapter =new SessionListenerAdapter();
sessionListenerAdapter.setLoginService(loginService);
sessionListenerAdapter.setLoginHistoryService(loginHistoryService);
return sessionListenerAdapter;
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub

View File

@@ -76,6 +76,8 @@ public class HistoryLogin extends JpaBaseEntity implements Serializable{
@Column
String logoutTime;
int sessionStatus;
String startDate;
String endDate;
@@ -227,7 +229,15 @@ public class HistoryLogin extends JpaBaseEntity implements Serializable{
this.endDate = endDate;
}
@Override
public int getSessionStatus() {
return sessionStatus;
}
public void setSessionStatus(int sessionStatus) {
this.sessionStatus = sessionStatus;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("HistoryLogin [id=");

View File

@@ -1267,10 +1267,6 @@ public class UserInfo extends JpaBaseEntity {
builder.append(id);
builder.append(", username=");
builder.append(username);
builder.append(", password=");
builder.append(password);
builder.append(", decipherable=");
builder.append(decipherable);
builder.append(", sharedSecret=");
builder.append(sharedSecret);
builder.append(", sharedCounter=");

View File

@@ -19,6 +19,7 @@ package org.maxkey.persistence.db;
import java.sql.Types;
import org.maxkey.entity.HistoryLogin;
import org.maxkey.entity.UserInfo;
import org.maxkey.web.WebContext;
import org.slf4j.Logger;
@@ -38,23 +39,54 @@ public class LoginHistoryService {
this.jdbcTemplate = jdbcTemplate;
}
@Deprecated
public void login(UserInfo userInfo,String sessionId,
String type, String message, String code, String provider,String browser, String platform,int sessionStatus) {
jdbcTemplate.update(HISTORY_LOGIN_INSERT_STATEMENT,
new Object[] { WebContext.genId(), sessionId, userInfo.getId(), userInfo.getUsername(),
userInfo.getDisplayName(), type, message, code, provider, userInfo.getLastLoginIp(), browser, platform,
"Browser", WebContext.getRequest().getRequestURI() , sessionStatus},
new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR ,Types.INTEGER});
new int[] {
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR ,Types.INTEGER
});
}
public void login(HistoryLogin historyLogin) {
historyLogin.setId(WebContext.genId());
historyLogin.setLoginUrl(WebContext.getRequest().getRequestURI());
_logger.debug(" historyLogin " + historyLogin);
jdbcTemplate.update(HISTORY_LOGIN_INSERT_STATEMENT,
new Object[] {
historyLogin.getId(), historyLogin.getSessionId(), historyLogin.getUserId(), historyLogin.getUsername(),
historyLogin.getDisplayName(), historyLogin.getLoginType(), historyLogin.getMessage(), historyLogin.getCode(),
historyLogin.getProvider(), historyLogin.getSourceIp(), historyLogin.getBrowser(), historyLogin.getPlatform(),
"Browser", historyLogin.getLoginUrl() , historyLogin.getSessionStatus()
},
new int[] {
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR ,Types.INTEGER
});
}
public void logoff(String lastLogoffTime,String sessionId) {
_logger.debug(" sessionId " +sessionId +" , lastlogofftime " + lastLogoffTime);
jdbcTemplate.update(HISTORY_LOGOUT_UPDATE_STATEMENT,
new Object[] { lastLogoffTime, sessionId },
new int[] { Types.VARCHAR, Types.VARCHAR });
}
public void logoff(HistoryLogin historyLogin) {
_logger.debug(" sessionId " +historyLogin.getSessionId() +" , LogoutTime " + historyLogin.getLogoutTime());
jdbcTemplate.update(HISTORY_LOGOUT_UPDATE_STATEMENT,
new Object[] { historyLogin.getLogoutTime(), historyLogin.getSessionId() },
new int[] { Types.VARCHAR, Types.VARCHAR });
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright [2021] [MaxKey of copyright http://www.maxkey.top]
*
* 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 org.maxkey.web;
import java.util.Date;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.apache.mybatis.jpa.util.WebContext;
import org.maxkey.entity.UserInfo;
import org.maxkey.persistence.db.LoginHistoryService;
import org.maxkey.persistence.db.LoginService;
import org.maxkey.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WebListener
public class SessionListenerAdapter implements HttpSessionListener {
private static final Logger _logger = LoggerFactory.getLogger(SessionListenerAdapter.class);
LoginService loginService;
LoginHistoryService loginHistoryService;
public SessionListenerAdapter() {
super();
_logger.debug("SessionListenerAdapter inited . ");
}
public void init() {
if(loginService == null ) {
loginService = (LoginService)WebContext.getBean("loginService");
loginHistoryService = (LoginHistoryService)WebContext.getBean("loginHistoryService");
_logger.debug("SessionListenerAdapter function inited . ");
}
}
/**
* session Created
*/
@Override
public void sessionCreated(HttpSessionEvent sessionEvent) {
_logger.trace("new session Created :" + sessionEvent.getSession().getId());
}
/**
* session Destroyed
*/
@Override
public void sessionDestroyed(HttpSessionEvent sessionEvent) {
HttpSession session = sessionEvent.getSession();
Object sessionIdAttribute = session.getAttribute(WebConstants.CURRENT_USER_SESSION_ID);
_logger.trace("session Id : " + session.getId());
if(sessionIdAttribute != null) {
init();
UserInfo userInfo = (UserInfo)session.getAttribute(WebConstants.CURRENT_USER);
userInfo.setLastLogoffTime(DateUtils.formatDateTime(new Date()));
loginService.setLastLogoffInfo(userInfo);
loginHistoryService.logoff(userInfo.getLastLogoffTime(), sessionIdAttribute.toString());
_logger.debug(
"session {} Destroyed as {} userId : {} , username : {}" ,
sessionIdAttribute,
userInfo.getLastLogoffTime(),
userInfo.getId(),
userInfo.getUsername());
}
}
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
public void setLoginHistoryService(LoginHistoryService loginHistoryService) {
this.loginHistoryService = loginHistoryService;
}
}

View File

@@ -76,6 +76,8 @@ public final class WebContext {
sessionAttributeNameList.add(WebConstants.CURRENT_USER_SESSION_ID);
sessionAttributeNameList.add(WebConstants.FIRST_SAVED_REQUEST_PARAMETER);
sessionAttributeNameList.add(WebConstants.REMEBER_ME_SESSION);
}
/**
@@ -451,7 +453,7 @@ public final class WebContext {
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
}
LogFactory.getLog(WebContext.class).debug(
LogFactory.getLog(WebContext.class).trace(
"getRequestIpAddress() RequestIpAddress:" + ipAddress);
return ipAddress;
}