|
@@ -1,20 +1,26 @@
|
|
|
package com.ym.mec.auth.core.filter;
|
|
|
|
|
|
-import com.ym.mec.auth.api.entity.LoginEntity;
|
|
|
-import com.ym.mec.auth.config.token.PhoneAuthenticationToken;
|
|
|
-import com.ym.mec.common.security.SecurityConstants;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+import javax.servlet.FilterChain;
|
|
|
+import javax.servlet.ServletException;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.security.authentication.AbstractAuthenticationToken;
|
|
|
import org.springframework.security.authentication.AuthenticationServiceException;
|
|
|
+import org.springframework.security.authentication.LockedException;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.AuthenticationException;
|
|
|
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
|
|
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
|
|
|
|
|
-import javax.servlet.FilterChain;
|
|
|
-import javax.servlet.ServletException;
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.io.IOException;
|
|
|
+import com.ym.mec.auth.api.dto.SysUserInfo;
|
|
|
+import com.ym.mec.auth.api.entity.LoginEntity;
|
|
|
+import com.ym.mec.auth.config.token.PhoneAuthenticationToken;
|
|
|
+import com.ym.mec.auth.service.SysUserService;
|
|
|
+import com.ym.mec.common.security.SecurityConstants;
|
|
|
|
|
|
public class PhoneLoginAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
|
|
|
|
|
@@ -27,6 +33,8 @@ public class PhoneLoginAuthenticationFilter extends AbstractAuthenticationProces
|
|
|
private static final String SPRING_SECURITY_RESTFUL_LOGIN_URL = "/smsLogin";
|
|
|
private boolean postOnly = true;
|
|
|
|
|
|
+ private SysUserService sysUserService;
|
|
|
+
|
|
|
public PhoneLoginAuthenticationFilter() {
|
|
|
super(new AntPathRequestMatcher(SPRING_SECURITY_RESTFUL_LOGIN_URL, "POST"));
|
|
|
}
|
|
@@ -36,16 +44,37 @@ public class PhoneLoginAuthenticationFilter extends AbstractAuthenticationProces
|
|
|
if (postOnly && !request.getMethod().equals("POST")) {
|
|
|
throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
|
|
|
}
|
|
|
+
|
|
|
AbstractAuthenticationToken authRequest;
|
|
|
// 手机验证码登陆
|
|
|
String principal = obtainParameter(request, SPRING_SECURITY_RESTFUL_PHONE_KEY);
|
|
|
+ String credentials = obtainParameter(request, SPRING_SECURITY_RESTFUL_VERIFY_CODE_KEY);
|
|
|
+ String isRegister = obtainParameter(request, IS_REGISTER_PARAMETER);
|
|
|
+ // 是否是租户
|
|
|
+ String isLessee = obtainParameter(request, IS_LESSEE);
|
|
|
+
|
|
|
+ String clientId = request.getParameter(clientIdParameter).toUpperCase();
|
|
|
+
|
|
|
+ SysUserInfo userInfo = sysUserService.queryUserInfoByPhone(principal);
|
|
|
+ if (userInfo != null && StringUtils.isNotEmpty(isLessee)) {
|
|
|
+ throw new LockedException("用户已存在");
|
|
|
+ }
|
|
|
+ if ("EDUCATION".equals(clientId)) {
|
|
|
+ clientId = "SYSTEM";
|
|
|
+ }
|
|
|
+ if (userInfo != null && !userInfo.getSysUser().getUserType().contains(clientId)) {
|
|
|
+ throw new LockedException("用户不存在,请联系教务老师");
|
|
|
+ }
|
|
|
+
|
|
|
principal = principal.trim();
|
|
|
+
|
|
|
LoginEntity loginEntity = new LoginEntity();
|
|
|
- loginEntity.setClientId(request.getParameter(clientIdParameter).toUpperCase());
|
|
|
- loginEntity.setIsLessee(obtainParameter(request, IS_LESSEE));
|
|
|
+ loginEntity.setClientId(clientId);
|
|
|
+ loginEntity.setIsLessee(isLessee);
|
|
|
loginEntity.setPhone(principal);
|
|
|
- loginEntity.setSmsCode(obtainParameter(request, SPRING_SECURITY_RESTFUL_VERIFY_CODE_KEY));
|
|
|
- loginEntity.setIsRegister(obtainParameter(request, IS_REGISTER_PARAMETER));
|
|
|
+ loginEntity.setSmsCode(credentials);
|
|
|
+ loginEntity.setIsRegister(isRegister);
|
|
|
+
|
|
|
authRequest = new PhoneAuthenticationToken(SecurityConstants.PHONE_PRINCIPAL_PREFIX + principal, loginEntity);
|
|
|
|
|
|
// Allow subclasses to set the "details" property
|
|
@@ -57,7 +86,7 @@ public class PhoneLoginAuthenticationFilter extends AbstractAuthenticationProces
|
|
|
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult)
|
|
|
throws IOException, ServletException {
|
|
|
super.successfulAuthentication(request, response, chain, authResult);
|
|
|
- //chain.doFilter(request, response);
|
|
|
+ // chain.doFilter(request, response);
|
|
|
}
|
|
|
|
|
|
private void setDetails(HttpServletRequest request, AbstractAuthenticationToken authRequest) {
|
|
@@ -68,4 +97,9 @@ public class PhoneLoginAuthenticationFilter extends AbstractAuthenticationProces
|
|
|
String result = request.getParameter(parameter);
|
|
|
return result == null ? "" : result;
|
|
|
}
|
|
|
+
|
|
|
+ public void setSysUserService(SysUserService sysUserService) {
|
|
|
+ this.sysUserService = sysUserService;
|
|
|
+ }
|
|
|
+
|
|
|
}
|