提交短信认证后端逻辑判断

This commit is contained in:
shibanglin 2022-11-18 10:14:47 +08:00
parent 756993053b
commit 0bd384b76a
2 changed files with 139 additions and 123 deletions

View File

@ -1,19 +1,19 @@
/* /*
* Copyright [2020] [MaxKey of copyright http://www.maxkey.top] * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.maxkey.authn; package org.maxkey.authn;
@ -25,9 +25,9 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
public class LoginCredential implements Authentication { public class LoginCredential implements Authentication {
/** /**
* *
*/ */
private static final long serialVersionUID = 3125709257481600320L; private static final long serialVersionUID = 3125709257481600320L;
String congress; String congress;
@ -44,12 +44,12 @@ public class LoginCredential implements Authentication {
String code; String code;
String message = WebConstants.LOGIN_RESULT.SUCCESS; String message = WebConstants.LOGIN_RESULT.SUCCESS;
String instId; String instId;
ArrayList<GrantedAuthority> grantedAuthority; ArrayList<GrantedAuthority> grantedAuthority;
boolean authenticated; boolean authenticated;
boolean roleAdministrators; boolean roleAdministrators;
String mobile;
/** /**
* BasicAuthentication. * BasicAuthentication.
*/ */
@ -64,16 +64,16 @@ public class LoginCredential implements Authentication {
this.password = password; this.password = password;
this.authType = authType; this.authType = authType;
} }
public String getCongress() { public String getCongress() {
return congress; return congress;
} }
public void setCongress(String congress) { public void setCongress(String congress) {
this.congress = congress; this.congress = congress;
} }
@Override @Override
public String getName() { public String getName() {
return "Login Credential"; return "Login Credential";
} }
@ -127,14 +127,14 @@ public class LoginCredential implements Authentication {
} }
public String getState() { public String getState() {
return state; return state;
} }
public void setState(String state) { public void setState(String state) {
this.state = state; this.state = state;
} }
public String getCaptcha() { public String getCaptcha() {
return captcha; return captcha;
} }
@ -223,51 +223,62 @@ public class LoginCredential implements Authentication {
} }
public String getInstId() { public String getInstId() {
return instId; return instId;
} }
public void setInstId(String instId) { public void setInstId(String instId) {
this.instId = instId; this.instId = instId;
} }
@Override public String getMobile() {
public String toString() { return mobile;
StringBuilder builder = new StringBuilder(); }
builder.append("LoginCredential [congress=");
builder.append(congress); public void setMobile(String mobile) {
builder.append(", username="); this.mobile = mobile;
builder.append(username); }
builder.append(", password=");
builder.append(password);
builder.append(", state="); @Override
builder.append(state); public String toString() {
builder.append(", captcha="); StringBuilder builder = new StringBuilder();
builder.append(captcha); builder.append("LoginCredential [congress=");
builder.append(", otpCaptcha="); builder.append(congress);
builder.append(otpCaptcha); builder.append(", username=");
builder.append(", remeberMe="); builder.append(username);
builder.append(remeberMe); builder.append(", password=");
builder.append(", authType="); builder.append(password);
builder.append(authType); builder.append(", state=");
builder.append(", jwtToken="); builder.append(state);
builder.append(jwtToken); builder.append(", mobile=");
builder.append(", onlineTicket="); builder.append(mobile);
builder.append(onlineTicket); builder.append(", captcha=");
builder.append(", provider="); builder.append(captcha);
builder.append(provider); builder.append(", otpCaptcha=");
builder.append(", code="); builder.append(otpCaptcha);
builder.append(code); builder.append(", remeberMe=");
builder.append(", message="); builder.append(remeberMe);
builder.append(message); builder.append(", authType=");
builder.append(", instId="); builder.append(authType);
builder.append(instId); builder.append(", jwtToken=");
builder.append(", grantedAuthority="); builder.append(jwtToken);
builder.append(grantedAuthority); builder.append(", onlineTicket=");
builder.append(", authenticated="); builder.append(onlineTicket);
builder.append(authenticated); builder.append(", provider=");
builder.append(", roleAdministrators="); builder.append(provider);
builder.append(roleAdministrators); builder.append(", code=");
builder.append("]"); builder.append(code);
return builder.toString(); builder.append(", message=");
} builder.append(message);
} builder.append(", instId=");
builder.append(instId);
builder.append(", grantedAuthority=");
builder.append(grantedAuthority);
builder.append(", authenticated=");
builder.append(authenticated);
builder.append(", roleAdministrators=");
builder.append(roleAdministrators);
builder.append("]");
return builder.toString();
}
}

View File

@ -1,19 +1,19 @@
/* /*
* Copyright [2022] [MaxKey of copyright http://www.maxkey.top] * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.maxkey.authn.provider.impl; package org.maxkey.authn.provider.impl;
@ -42,85 +42,90 @@ import org.springframework.security.core.AuthenticationException;
* *
*/ */
public class MobileAuthenticationProvider extends AbstractAuthenticationProvider { public class MobileAuthenticationProvider extends AbstractAuthenticationProvider {
private static final Logger _logger = private static final Logger _logger =
LoggerFactory.getLogger(MobileAuthenticationProvider.class); LoggerFactory.getLogger(MobileAuthenticationProvider.class);
public String getProviderName() { public String getProviderName() {
return "mobile" + PROVIDER_SUFFIX; return "mobile" + PROVIDER_SUFFIX;
} }
public MobileAuthenticationProvider() { public MobileAuthenticationProvider() {
super(); super();
} }
public MobileAuthenticationProvider( public MobileAuthenticationProvider(
AbstractAuthenticationRealm authenticationRealm, AbstractAuthenticationRealm authenticationRealm,
ApplicationConfig applicationConfig, ApplicationConfig applicationConfig,
OtpAuthnService otpAuthnService, OtpAuthnService otpAuthnService,
SessionManager sessionManager) { SessionManager sessionManager) {
this.authenticationRealm = authenticationRealm; this.authenticationRealm = authenticationRealm;
this.applicationConfig = applicationConfig; this.applicationConfig = applicationConfig;
this.otpAuthnService = otpAuthnService; this.otpAuthnService = otpAuthnService;
this.sessionManager = sessionManager; this.sessionManager = sessionManager;
} }
@Override @Override
public Authentication doAuthenticate(LoginCredential loginCredential) { public Authentication doAuthenticate(LoginCredential loginCredential) {
UsernamePasswordAuthenticationToken authenticationToken = null; UsernamePasswordAuthenticationToken authenticationToken = null;
_logger.debug("Trying to authenticate user '{}' via {}", _logger.debug("Trying to authenticate user '{}' via {}",
loginCredential.getPrincipal(), getProviderName()); loginCredential.getPrincipal(), getProviderName());
try { try {
_logger.debug("authentication " + loginCredential);
emptyPasswordValid(loginCredential.getPassword()); //如果是验证码登录设置mobile为username
loginCredential.setUsername(loginCredential.getMobile());
emptyUsernameValid(loginCredential.getUsername()); //设置密码为验证码
loginCredential.setPassword(loginCredential.getOtpCaptcha());
UserInfo userInfo = loadUserInfo(loginCredential.getUsername(),loginCredential.getPassword());
statusValid(loginCredential , userInfo);
//Validate PasswordPolicy _logger.debug("authentication " + loginCredential);
authenticationRealm.getPasswordPolicyValidator().passwordPolicyValid(userInfo);
mobileCaptchaValid(loginCredential.getPassword(),userInfo);
//apply PasswordSetType and resetBadPasswordCount emptyPasswordValid(loginCredential.getPassword());
authenticationRealm.getPasswordPolicyValidator().applyPasswordPolicy(userInfo);
emptyUsernameValid(loginCredential.getUsername());
authenticationToken = createOnlineTicket(loginCredential,userInfo);
// user authenticated UserInfo userInfo = loadUserInfo(loginCredential.getUsername(),loginCredential.getPassword());
_logger.debug("'{}' authenticated successfully by {}.",
loginCredential.getPrincipal(), getProviderName()); statusValid(loginCredential , userInfo);
authenticationRealm.insertLoginHistory(userInfo, //Validate PasswordPolicy 取消密码策略验证
ConstsLoginType.LOCAL, //authenticationRealm.getPasswordPolicyValidator().passwordPolicyValid(userInfo);
"",
"xe00000004", mobileCaptchaValid(loginCredential.getPassword(),userInfo);
WebConstants.LOGIN_RESULT.SUCCESS);
//apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getPasswordPolicyValidator().applyPasswordPolicy(userInfo);
authenticationToken = createOnlineTicket(loginCredential,userInfo);
// user authenticated
_logger.debug("'{}' authenticated successfully by {}.",
loginCredential.getPrincipal(), getProviderName());
authenticationRealm.insertLoginHistory(userInfo,
ConstsLoginType.LOCAL,
"",
"xe00000004",
WebConstants.LOGIN_RESULT.SUCCESS);
} catch (AuthenticationException e) { } catch (AuthenticationException e) {
_logger.error("Failed to authenticate user {} via {}: {}", _logger.error("Failed to authenticate user {} via {}: {}",
new Object[] { loginCredential.getPrincipal(), new Object[] { loginCredential.getPrincipal(),
getProviderName(), getProviderName(),
e.getMessage() }); e.getMessage() });
WebContext.setAttribute( WebContext.setAttribute(
WebConstants.LOGIN_ERROR_SESSION_MESSAGE, e.getMessage()); WebConstants.LOGIN_ERROR_SESSION_MESSAGE, e.getMessage());
} catch (Exception e) { } catch (Exception e) {
_logger.error("Login error Unexpected exception in {} authentication:\n{}" , _logger.error("Login error Unexpected exception in {} authentication:\n{}" ,
getProviderName(), e.getMessage()); getProviderName(), e.getMessage());
} }
return authenticationToken; return authenticationToken;
} }
/** /**
* mobile validate. * mobile validate.
* *
* @param otpCaptcha String * @param otpCaptcha String
* @param authType String * @param authType String
* @param userInfo UserInfo * @param userInfo UserInfo
@ -139,5 +144,5 @@ public class MobileAuthenticationProvider extends AbstractAuthenticationProvider
} }
} }
} }
} }