| | |
| | | |
| | | import java.security.GeneralSecurityException; |
| | | |
| | | import java.util.Base64; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | |
| | | 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; |
| | | |
| | | /** |
| | | * @author Tanbin |
| | | * @date 2018-12-12 |
| | | */ |
| | | @Component |
| | | public class QueryDatabaseAuthenticationHandler extends AbstractJdbcUsernamePasswordAuthenticationHandler { |
| | | private String sql; |
| | |
| | | @Autowired |
| | | private OjdbcProperties properties; |
| | | |
| | | private static String m0 = "0"; |
| | | |
| | | private static String mIsFirst = "ISFIRSTLOGIN"; |
| | | |
| | | @Override |
| | | protected HandlerResult authenticateUsernamePasswordInternal(UsernamePasswordCredential credential, String originalPassword) throws GeneralSecurityException, PreventedException { |
| | | if (StringUtils.isBlank(this.sql) || getJdbcTemplate() == null) |
| | | 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 { |
| | | username = new String(Base64.getDecoder().decode(username)); |
| | | credential.setUsername(username); |
| | | originalPassword = new String(Base64.getDecoder().decode(originalPassword)); |
| | | |
| | | 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))) |
| | | if (isFirstLogin(username)) { |
| | | boolean b1 = StringUtils.isNotBlank(originalPassword) && !matches(originalPassword, dbPassword); |
| | | boolean b2 = StringUtils.isBlank(originalPassword) && !StringUtils.equals(password, dbPassword); |
| | | if (b1 || b2) { |
| | | throw new FailedLoginException("Password does not match value on record."); |
| | | } |
| | | } else { |
| | | if ((StringUtils.isNotBlank(originalPassword) && !validatePass(originalPassword, dbPassword))) |
| | | if ((StringUtils.isNotBlank(originalPassword) && !validatePass(originalPassword, dbPassword))) { |
| | | throw new FailedLoginException("Password does not match value on record."); |
| | | } |
| | | } |
| | | |
| | | } catch (IncorrectResultSizeDataAccessException e) { |
| | | if (e.getActualSize() == 0) |
| | | 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); |
| | |
| | | return createHandlerResult(credential, this.principalFactory.createPrincipal(username), null); |
| | | } |
| | | |
| | | @Override |
| | | protected HandlerResult authenticateUsernamePasswordInternal(UsernamePasswordCredential credential) throws GeneralSecurityException, PreventedException { |
| | | return authenticateUsernamePasswordInternal(credential, null); |
| | | } |
| | |
| | | List<Map<String, Object>> list = getJdbcTemplate().queryForList(querysql); |
| | | if (list != null && !list.isEmpty()) { |
| | | |
| | | if ("0".equals(list.get(0).get("ISFIRSTLOGIN").toString())) { |
| | | if (m0.equals(list.get(0).get(mIsFirst).toString())) { |
| | | throw new AccountPasswordMustChangeException(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public boolean IsFirstLogin(String username) throws 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())) { |
| | | if (m0.equals(list.get(0).get(mIsFirst).toString())) { |
| | | return true; |
| | | } |
| | | } |