1
13693261870
2022-09-16 58d012f11dd34564d81b4eb3a6099eb689876597
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package org.apereo.cas.adaptors.jdbc;
 
import java.security.GeneralSecurityException;
 
import java.util.List;
import java.util.Map;
 
import javax.security.auth.login.AccountNotFoundException;
import javax.security.auth.login.FailedLoginException;
import javax.sql.DataSource;
 
import org.apache.commons.lang3.StringUtils;
import org.apereo.cas.authentication.AccountPasswordMustChangeException;
import org.apereo.cas.authentication.HandlerResult;
import org.apereo.cas.authentication.PreventedException;
import org.apereo.cas.authentication.UsernamePasswordCredential;
import org.apereo.cas.web.landtool.terra.OjdbcProperties;
import org.apereo.cas.web.landtool.utils.Md5Util;
import org.pac4j.oauth.profile.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.webflow.execution.Event;
 
import com.zaxxer.hikari.HikariDataSource;
 
@Component
public class QueryDatabaseAuthenticationHandler extends AbstractJdbcUsernamePasswordAuthenticationHandler {
    private String sql;
 
    @Autowired
    private OjdbcProperties properties;
 
    protected HandlerResult authenticateUsernamePasswordInternal(UsernamePasswordCredential credential, String originalPassword) throws GeneralSecurityException, PreventedException {
        if (StringUtils.isBlank(this.sql) || getJdbcTemplate() == null)
            throw new GeneralSecurityException("Authentication handler is not configured correctly. No SQL statement or JDBC template is found.");
        String username = credential.getUsername();
        String password = credential.getPassword();
        try {
            String dbPassword = (String) getJdbcTemplate().queryForObject(this.sql, String.class, new Object[]{username});
 
            //第一次登录,仍使用源方式认证
            if (IsFirstLogin(username)) {
                if ((StringUtils.isNotBlank(originalPassword) && !matches(originalPassword, dbPassword)) || (
                        StringUtils.isBlank(originalPassword) && !StringUtils.equals(password, dbPassword)))
                    throw new FailedLoginException("Password does not match value on record.");
            } else {
                if ((StringUtils.isNotBlank(originalPassword) && !validatePass(originalPassword, dbPassword)))
                    throw new FailedLoginException("Password does not match value on record.");
            }
 
        } catch (IncorrectResultSizeDataAccessException e) {
            if (e.getActualSize() == 0)
                throw new AccountNotFoundException(username + " not found with SQL query");
            throw new FailedLoginException("Multiple records found for " + username);
        } catch (DataAccessException e) {
            throw new PreventedException("SQL exception while executing query for " + username, e);
        }
        checkFirstLogin(username);
        return createHandlerResult(credential, this.principalFactory.createPrincipal(username), null);
    }
 
    protected HandlerResult authenticateUsernamePasswordInternal(UsernamePasswordCredential credential) throws GeneralSecurityException, PreventedException {
        return authenticateUsernamePasswordInternal(credential, null);
    }
 
    public void setSql(String sql) {
        this.sql = sql;
    }
 
    public void checkFirstLogin(String username) throws AccountPasswordMustChangeException {
        String tempsql = "SELECT * FROM MAGBG.ORG_USER WHERE LOGINNAME='%s'";
        String querysql = String.format(tempsql, username);
        List<Map<String, Object>> list = getJdbcTemplate().queryForList(querysql);
        if (list != null && !list.isEmpty()) {
 
            if ("0".equals(list.get(0).get("ISFIRSTLOGIN").toString())) {
                throw new AccountPasswordMustChangeException();
            }
        }
    }
 
    public boolean IsFirstLogin(String username) throws AccountPasswordMustChangeException {
        String tempsql = "SELECT * FROM MAGBG.ORG_USER WHERE LOGINNAME='%s'";
        String querysql = String.format(tempsql, username);
        List<Map<String, Object>> list = getJdbcTemplate().queryForList(querysql);
        if (list != null && !list.isEmpty()) {
 
            if ("0".equals(list.get(0).get("ISFIRSTLOGIN").toString())) {
                return true;
            }
        }
        return false;
    }
 
    @Override
    public void setDataSource(DataSource dataSource) {
 
        super.setDataSource(dataSource);
    }
 
    @Override
    protected JdbcTemplate getJdbcTemplate() {
        JdbcTemplate jt = super.getJdbcTemplate();
 
        return jt;
    }
 
    public boolean validatePass(String originalPassword, String dbpassword) {
 
        return Md5Util.verify(originalPassword, Md5Util.reverse(dbpassword));
    }
}