|
@@ -7,23 +7,31 @@ 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.AuthenticationServiceException;
|
|
|
+import org.springframework.security.authentication.LockedException;
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.AuthenticationException;
|
|
|
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
|
|
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
+import com.ym.mec.auth.api.dto.SysUserInfo;
|
|
|
import com.ym.mec.auth.config.constant.SecurityConstants;
|
|
|
+import com.ym.mec.auth.service.SysUserService;
|
|
|
|
|
|
public class UsernameAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
|
|
|
|
|
|
+ private SysUserService sysUserService;
|
|
|
+
|
|
|
// ~ Static fields/initializers
|
|
|
// =====================================================================================
|
|
|
|
|
|
private String usernameParameter = "username";
|
|
|
private String passwordParameter = "password";
|
|
|
+ private String clientIdParameter = "clientId";
|
|
|
private boolean postOnly = true;
|
|
|
|
|
|
// ~ Constructors
|
|
@@ -36,12 +44,9 @@ public class UsernameAuthenticationFilter extends AbstractAuthenticationProcessi
|
|
|
// ~ Methods
|
|
|
// ========================================================================================================
|
|
|
|
|
|
-
|
|
|
- public Authentication attemptAuthentication(HttpServletRequest request,
|
|
|
- HttpServletResponse response) throws AuthenticationException {
|
|
|
+ public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
|
|
|
if (postOnly && !request.getMethod().equals("POST")) {
|
|
|
- throw new AuthenticationServiceException(
|
|
|
- "Authentication method not supported: " + request.getMethod());
|
|
|
+ throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
|
|
|
}
|
|
|
|
|
|
String username = obtainUsername(request);
|
|
@@ -57,8 +62,28 @@ public class UsernameAuthenticationFilter extends AbstractAuthenticationProcessi
|
|
|
username = username.trim();
|
|
|
password = password.trim();
|
|
|
|
|
|
- UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
|
|
|
- SecurityConstants.USERNAME_PRINCIPAL_PREFIX + username, password);
|
|
|
+ SysUserInfo userInfo = null;
|
|
|
+
|
|
|
+ if (StringUtils.startsWith(username, SecurityConstants.PHONE_PRINCIPAL_PREFIX)) {
|
|
|
+ userInfo = sysUserService.queryUserInfoByPhone(StringUtils.substringAfter(username, SecurityConstants.PHONE_PRINCIPAL_PREFIX));
|
|
|
+ } else if (StringUtils.startsWith(username, SecurityConstants.USERNAME_PRINCIPAL_PREFIX)) {
|
|
|
+ userInfo = sysUserService.queryUserInfoByUsername(StringUtils.substringAfter(username, SecurityConstants.USERNAME_PRINCIPAL_PREFIX));
|
|
|
+ } else {
|
|
|
+ userInfo = sysUserService.queryUserInfoByUsername(username);
|
|
|
+ }
|
|
|
+
|
|
|
+ String clientId = request.getParameter(clientIdParameter);
|
|
|
+
|
|
|
+ if (userInfo == null) {
|
|
|
+ throw new UsernameNotFoundException("用户名或密码错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!StringUtils.equalsIgnoreCase(clientId, userInfo.getSysUser().getUserType().getCode())) {
|
|
|
+ throw new LockedException("用户名或密码错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(SecurityConstants.USERNAME_PRINCIPAL_PREFIX + username,
|
|
|
+ password);
|
|
|
|
|
|
// Allow subclasses to set the "details" property
|
|
|
setDetails(request, authRequest);
|
|
@@ -67,10 +92,10 @@ public class UsernameAuthenticationFilter extends AbstractAuthenticationProcessi
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
|
|
|
- FilterChain chain, Authentication authResult) throws IOException, ServletException {
|
|
|
+ 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);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -113,8 +138,7 @@ public class UsernameAuthenticationFilter extends AbstractAuthenticationProcessi
|
|
|
* @param authRequest the authentication request object that should have its details
|
|
|
* set
|
|
|
*/
|
|
|
- protected void setDetails(HttpServletRequest request,
|
|
|
- UsernamePasswordAuthenticationToken authRequest) {
|
|
|
+ protected void setDetails(HttpServletRequest request, UsernamePasswordAuthenticationToken authRequest) {
|
|
|
authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
|
|
|
}
|
|
|
|
|
@@ -161,4 +185,8 @@ public class UsernameAuthenticationFilter extends AbstractAuthenticationProcessi
|
|
|
return passwordParameter;
|
|
|
}
|
|
|
|
|
|
+ public void setSysUserService(SysUserService sysUserService) {
|
|
|
+ this.sysUserService = sysUserService;
|
|
|
+ }
|
|
|
+
|
|
|
}
|