|
@@ -21,10 +21,17 @@ import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
|
import lombok.Getter;
|
|
import lombok.Getter;
|
|
import lombok.Setter;
|
|
import lombok.Setter;
|
|
import org.springframework.http.HttpMethod;
|
|
import org.springframework.http.HttpMethod;
|
|
|
|
+import org.springframework.security.authentication.AuthenticationEventPublisher;
|
|
import org.springframework.security.authentication.AuthenticationServiceException;
|
|
import org.springframework.security.authentication.AuthenticationServiceException;
|
|
|
|
+import org.springframework.security.authentication.BadCredentialsException;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.AuthenticationException;
|
|
import org.springframework.security.core.AuthenticationException;
|
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
|
+import org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint;
|
|
|
|
+import org.springframework.security.web.AuthenticationEntryPoint;
|
|
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
|
|
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
|
|
|
|
+import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
|
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
@@ -37,6 +44,8 @@ import javax.servlet.http.HttpServletResponse;
|
|
*/
|
|
*/
|
|
public class MobileAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
|
|
public class MobileAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
|
|
private static final String SPRING_SECURITY_FORM_MOBILE_KEY = "mobile";
|
|
private static final String SPRING_SECURITY_FORM_MOBILE_KEY = "mobile";
|
|
|
|
+ private AuthenticationEventPublisher eventPublisher = new MobileAuthenticationFilter.NullEventPublisher();
|
|
|
|
+ private AuthenticationEntryPoint authenticationEntryPoint = new MobileAuthenticationEntryPoint();
|
|
@Getter
|
|
@Getter
|
|
@Setter
|
|
@Setter
|
|
private String mobileParameter = SPRING_SECURITY_FORM_MOBILE_KEY;
|
|
private String mobileParameter = SPRING_SECURITY_FORM_MOBILE_KEY;
|
|
@@ -68,7 +77,24 @@ public class MobileAuthenticationFilter extends AbstractAuthenticationProcessing
|
|
|
|
|
|
setDetails(request, mobileAuthenticationToken);
|
|
setDetails(request, mobileAuthenticationToken);
|
|
|
|
|
|
- return this.getAuthenticationManager().authenticate(mobileAuthenticationToken);
|
|
|
|
|
|
+ try {
|
|
|
|
+ return this.getAuthenticationManager().authenticate(mobileAuthenticationToken);
|
|
|
|
+
|
|
|
|
+ } catch (Exception failed) {
|
|
|
|
+ SecurityContextHolder.clearContext();
|
|
|
|
+ logger.debug("Authentication request failed: " + failed);
|
|
|
|
+
|
|
|
|
+ eventPublisher.publishAuthenticationFailure(new BadCredentialsException(failed.getMessage(), failed),
|
|
|
|
+ new PreAuthenticatedAuthenticationToken("access-token", "N/A"));
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ authenticationEntryPoint.commence(request, response,
|
|
|
|
+ new UsernameNotFoundException(failed.getMessage(), failed));
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error("authenticationEntryPoint handle error:{}", failed);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
}
|
|
}
|
|
|
|
|
|
private String obtainMobile(HttpServletRequest request) {
|
|
private String obtainMobile(HttpServletRequest request) {
|
|
@@ -79,5 +105,15 @@ public class MobileAuthenticationFilter extends AbstractAuthenticationProcessing
|
|
MobileAuthenticationToken authRequest) {
|
|
MobileAuthenticationToken authRequest) {
|
|
authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
|
|
authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ private static final class NullEventPublisher implements AuthenticationEventPublisher {
|
|
|
|
+ @Override
|
|
|
|
+ public void publishAuthenticationFailure(AuthenticationException exception, Authentication authentication) {
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void publishAuthenticationSuccess(Authentication authentication) {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|