LoginHistoryRepository.java 4.0 KB
Newer Older
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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.
 */
 

M
MaxKey 已提交
18
package org.maxkey.persistence.repository;
19 20

import java.sql.Types;
M
MaxKey 已提交
21
import org.maxkey.entity.HistoryLogin;
22 23 24 25 26
import org.maxkey.web.WebContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

M
MaxKey 已提交
27
public class LoginHistoryRepository {
M
MaxKey 已提交
28
    private static Logger logger = LoggerFactory.getLogger(LoginHistoryRepository.class);
29
    
M
MaxKey 已提交
30
    private static final String HISTORY_LOGIN_INSERT_STATEMENT = "insert into mxk_history_login (id , sessionid , userid , username , displayname , logintype , message , code , provider , sourceip , ipregion , iplocation, browser , platform , application , loginurl , sessionstatus ,instid)values( ? , ? , ? , ? , ? , ? , ? , ? , ?, ? , ? , ?, ? , ? , ?, ? , ? , ?)";
31 32 33

    protected JdbcTemplate jdbcTemplate;
    
M
MaxKey 已提交
34
    public LoginHistoryRepository(JdbcTemplate jdbcTemplate) {
35 36 37
        this.jdbcTemplate = jdbcTemplate;
    }
        
M
MaxKey 已提交
38 39 40
    public void login(HistoryLogin historyLogin) {
        historyLogin.setId(WebContext.genId());
        historyLogin.setLoginUrl(WebContext.getRequest().getRequestURI());
M
MaxKey 已提交
41 42
        //Thread insert 
        new Thread(new HistoryLoginRunnable(jdbcTemplate,historyLogin)).start();
M
MaxKey 已提交
43 44
    }
    
M
MaxKey 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	public class HistoryLoginRunnable implements Runnable{
		
		JdbcTemplate jdbcTemplate;
		
		HistoryLogin historyLogin;
		
		public HistoryLoginRunnable(JdbcTemplate jdbcTemplate, HistoryLogin historyLogin) {
			super();
			this.jdbcTemplate = jdbcTemplate;
			this.historyLogin = historyLogin;
		}

		@Override
	    public void run() {
			logger.debug("History Login {}" , 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.getIpRegion(),
	                        historyLogin.getIpLocation(),
	                        historyLogin.getBrowser(),
	                        historyLogin.getPlatform(),
	                        "Browser",
	                        historyLogin.getLoginUrl(),
	                        historyLogin.getSessionStatus(),
	                        historyLogin.getInstId()
	                        },
	                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.VARCHAR,
	                        Types.VARCHAR,
	                        Types.INTEGER,
	                        Types.VARCHAR
	                        });
		}
	}
    
105
}