This commit is contained in:
MaxKey
2022-03-31 11:02:51 +08:00
parent 476ebfdc55
commit a2740e15de
61 changed files with 955 additions and 1169 deletions
@@ -28,7 +28,6 @@ import org.maxkey.constants.ConstsTimeInterval;
import org.maxkey.persistence.repository.InstitutionsRepository;
import org.maxkey.persistence.repository.LoginHistoryRepository;
import org.maxkey.persistence.repository.LoginRepository;
import org.maxkey.web.SessionListenerAdapter;
import org.maxkey.web.WebXssRequestFilter;
import org.maxkey.web.WebInstRequestFilter;
import org.slf4j.Logger;
@@ -314,16 +313,6 @@ public class MvcAutoConfiguration implements InitializingBean , WebMvcConfigurer
return registrationBean;
}
@Bean(name = "sessionListenerAdapter")
public SessionListenerAdapter sessionListenerAdapter(
LoginRepository loginRepository,
LoginHistoryRepository loginHistoryRepository
) {
SessionListenerAdapter sessionListenerAdapter =
new SessionListenerAdapter(loginRepository,loginHistoryRepository);
return sessionListenerAdapter;
}
@Override
public void afterPropertiesSet() throws Exception {
@@ -48,6 +48,8 @@ public class UserInfo extends JpaBaseEntity {
public static final String DEFAULT_PASSWORD_SUFFIX = "MaxKey@888";
String onlineTicket;
//
@Id
@Column
@@ -385,7 +387,15 @@ public class UserInfo extends JpaBaseEntity {
this.id = id;
}
/**
public String getOnlineTicket() {
return onlineTicket;
}
public void setOnlineTicket(String onlineTicket) {
this.onlineTicket = onlineTicket;
}
/**
* @param username
*/
public UserInfo(String username) {
@@ -40,7 +40,6 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
@@ -63,9 +62,6 @@ public class InitializeContext extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
_logger.info("SecurityContextHolder StrategyName " + SessionSecurityContextHolderStrategy.class.getCanonicalName());
SecurityContextHolder.setStrategyName(SessionSecurityContextHolderStrategy.class.getCanonicalName());
WebContext.applicationContext = applicationContext;
org.apache.mybatis.jpa.util.WebContext.applicationContext = applicationContext;
@@ -1,102 +0,0 @@
/*
* 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.repository.LoginHistoryRepository;
import org.maxkey.persistence.repository.LoginRepository;
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);
LoginRepository loginRepository;
LoginHistoryRepository loginHistoryRepository;
public SessionListenerAdapter() {
super();
_logger.debug("SessionListenerAdapter inited . ");
}
public SessionListenerAdapter(LoginRepository loginRepository, LoginHistoryRepository loginHistoryRepository) {
super();
this.loginRepository = loginRepository;
this.loginHistoryRepository = loginHistoryRepository;
_logger.debug("SessionListenerAdapter inited . ");
}
public void init() {
if(loginRepository == null ) {
loginRepository = (LoginRepository)WebContext.getBean("loginRepository");
loginHistoryRepository = (LoginHistoryRepository)WebContext.getBean("loginHistoryRepository");
_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()));
loginRepository.updateLastLogoff(userInfo);
loginHistoryRepository.logoff(userInfo.getLastLogoffTime(), sessionIdAttribute.toString());
_logger.debug(
"session {} Destroyed as {} userId : {} , username : {}" ,
sessionIdAttribute,
userInfo.getLastLogoffTime(),
userInfo.getId(),
userInfo.getUsername());
}
}
public void setLoginRepository(LoginRepository loginRepository) {
this.loginRepository = loginRepository;
}
public void setLoginHistoryRepository(LoginHistoryRepository loginHistoryRepository) {
this.loginHistoryRepository = loginHistoryRepository;
}
}
@@ -1,68 +0,0 @@
/*
* Copyright [2020] [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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolderStrategy;
import org.springframework.security.core.context.SecurityContextImpl;
/**
* SecurityContext Session for Request , use SecurityContextHolderAwareRequestFilter
* @author Crystal.Sea
*
*/
public class SessionSecurityContextHolderStrategy implements SecurityContextHolderStrategy {
private static final Logger _logger =
LoggerFactory.getLogger(SessionSecurityContextHolderStrategy.class);
@Override
public void clearContext() {
WebContext.removeAttribute(WebConstants.AUTHENTICATION);
}
@Override
public SecurityContext getContext() {
SecurityContext ctx = createEmptyContext();
Authentication authentication = null;
try {
authentication = (Authentication)WebContext.getAuthentication();
if (authentication != null) {
ctx.setAuthentication(authentication);
}
}catch(Exception e) {
_logger.trace("a session ", e);
}
return ctx;
}
@Override
public void setContext(SecurityContext context) {
WebContext.setAuthentication(context.getAuthentication());
}
@Override
public SecurityContext createEmptyContext() {
return new SecurityContextImpl();
}
}
@@ -31,8 +31,6 @@ public class WebConstants {
public static final String CURRENT_USER = "current_user";
public static final String CURRENT_USER_SESSION_ID = "current_user_session_id";
public static final String CURRENT_COMPANY = "current_user_company";
public static final String CURRENT_DEPARTMENT = "current_user_department";
@@ -84,7 +84,6 @@ public final class WebContext {
sessionAttributeNameList.add(WebConstants.CURRENT_USER);
sessionAttributeNameList.add(WebConstants.CURRENT_USER_PASSWORD_SET_TYPE);
sessionAttributeNameList.add(WebConstants.CURRENT_USER_SESSION_ID);
sessionAttributeNameList.add(WebConstants.CURRENT_INST);
@@ -100,7 +99,6 @@ public final class WebContext {
logoutAttributeNameList.add(WebConstants.CURRENT_USER);
logoutAttributeNameList.add(WebConstants.CURRENT_USER_PASSWORD_SET_TYPE);
logoutAttributeNameList.add(WebConstants.CURRENT_USER_SESSION_ID);
logoutAttributeNameList.add(WebConstants.FIRST_SAVED_REQUEST_PARAMETER);