|
@@ -1,25 +1,26 @@
|
|
|
package com.ym.mec.auth.core.filter;
|
|
|
|
|
|
-import com.ym.mec.auth.api.dto.SysUserInfo;
|
|
|
-import com.ym.mec.auth.config.token.PhoneAuthenticationToken;
|
|
|
-import com.ym.mec.auth.service.SysUserService;
|
|
|
-import com.ym.mec.common.security.SecurityConstants;
|
|
|
-import com.ym.mec.common.service.IdGeneratorService;
|
|
|
+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.BadCredentialsException;
|
|
|
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 {
|
|
|
|
|
@@ -31,9 +32,8 @@ public class PhoneLoginAuthenticationFilter extends AbstractAuthenticationProces
|
|
|
|
|
|
private static final String SPRING_SECURITY_RESTFUL_LOGIN_URL = "/smsLogin";
|
|
|
private boolean postOnly = true;
|
|
|
-
|
|
|
+
|
|
|
private SysUserService sysUserService;
|
|
|
- private IdGeneratorService smsCodeService;
|
|
|
|
|
|
public PhoneLoginAuthenticationFilter() {
|
|
|
super(new AntPathRequestMatcher(SPRING_SECURITY_RESTFUL_LOGIN_URL, "POST"));
|
|
@@ -50,29 +50,32 @@ public class PhoneLoginAuthenticationFilter extends AbstractAuthenticationProces
|
|
|
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);
|
|
|
- // 验证码验证
|
|
|
- boolean b = smsCodeService.verifyValidCode(StringUtils.substringAfter(principal, SecurityConstants.PHONE_PRINCIPAL_PREFIX), credentials);
|
|
|
- if(!b) throw new BadCredentialsException("验证码校验失败");
|
|
|
-
|
|
|
- SysUserInfo userInfo = sysUserService.queryUserInfoByPhone(principal);
|
|
|
|
|
|
String clientId = request.getParameter(clientIdParameter).toUpperCase();
|
|
|
- if (userInfo == null) {
|
|
|
- userInfo = sysUserService.initUser(principal,clientId,isRegister,isLessee);
|
|
|
- }else if(StringUtils.isNotEmpty(isLessee)){
|
|
|
+
|
|
|
+ SysUserInfo userInfo = sysUserService.queryUserInfoByPhone(principal);
|
|
|
+ if (userInfo != null && StringUtils.isNotEmpty(isLessee)) {
|
|
|
throw new LockedException("用户已存在");
|
|
|
}
|
|
|
- if("EDUCATION".equals(clientId)){
|
|
|
+ if ("EDUCATION".equals(clientId)) {
|
|
|
clientId = "SYSTEM";
|
|
|
}
|
|
|
- if (!userInfo.getSysUser().getUserType().contains(clientId)) {
|
|
|
+ if (userInfo != null && !userInfo.getSysUser().getUserType().contains(clientId)) {
|
|
|
throw new LockedException("用户不存在,请联系教务老师");
|
|
|
}
|
|
|
|
|
|
principal = principal.trim();
|
|
|
- authRequest = new PhoneAuthenticationToken(SecurityConstants.PHONE_PRINCIPAL_PREFIX + principal, credentials);
|
|
|
+
|
|
|
+ LoginEntity loginEntity = new LoginEntity();
|
|
|
+ loginEntity.setClientId(clientId);
|
|
|
+ loginEntity.setIsLessee(isLessee);
|
|
|
+ loginEntity.setPhone(principal);
|
|
|
+ loginEntity.setSmsCode(credentials);
|
|
|
+ loginEntity.setIsRegister(isRegister);
|
|
|
+
|
|
|
+ authRequest = new PhoneAuthenticationToken(SecurityConstants.PHONE_PRINCIPAL_PREFIX + principal, loginEntity);
|
|
|
|
|
|
// Allow subclasses to set the "details" property
|
|
|
setDetails(request, authRequest);
|
|
@@ -83,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) {
|
|
@@ -99,7 +102,4 @@ public class PhoneLoginAuthenticationFilter extends AbstractAuthenticationProces
|
|
|
this.sysUserService = sysUserService;
|
|
|
}
|
|
|
|
|
|
- public void setSmsCodeService(IdGeneratorService smsCodeService) {
|
|
|
- this.smsCodeService = smsCodeService;
|
|
|
- }
|
|
|
}
|