|
@@ -14,57 +14,56 @@ import org.springframework.security.core.AuthenticationException;
|
|
|
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
|
|
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
|
|
|
|
|
+import com.ym.mec.auth.config.constant.SecurityConstants;
|
|
|
import com.ym.mec.auth.config.token.PhoneAuthenticationToken;
|
|
|
|
|
|
public class PhoneLoginAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
|
|
|
|
|
|
- private static final String SPRING_SECURITY_RESTFUL_PHONE_KEY = "phone";
|
|
|
- private static final String SPRING_SECURITY_RESTFUL_VERIFY_CODE_KEY = "smsCode";
|
|
|
+ private static final String SPRING_SECURITY_RESTFUL_PHONE_KEY = "phone";
|
|
|
+ private static final String SPRING_SECURITY_RESTFUL_VERIFY_CODE_KEY = "smsCode";
|
|
|
|
|
|
- private static final String SPRING_SECURITY_RESTFUL_LOGIN_URL = "/smsLogin";
|
|
|
- private boolean postOnly = true;
|
|
|
+ private static final String SPRING_SECURITY_RESTFUL_LOGIN_URL = "/smsLogin";
|
|
|
+ private boolean postOnly = true;
|
|
|
|
|
|
- public PhoneLoginAuthenticationFilter() {
|
|
|
- super(new AntPathRequestMatcher(SPRING_SECURITY_RESTFUL_LOGIN_URL, "POST"));
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
|
|
|
- if (postOnly && !request.getMethod().equals("POST")) {
|
|
|
- throw new AuthenticationServiceException(
|
|
|
- "Authentication method not supported: " + request.getMethod());
|
|
|
- }
|
|
|
+ public PhoneLoginAuthenticationFilter() {
|
|
|
+ super(new AntPathRequestMatcher(SPRING_SECURITY_RESTFUL_LOGIN_URL, "POST"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
|
|
|
+ if (postOnly && !request.getMethod().equals("POST")) {
|
|
|
+ throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
|
|
|
+ }
|
|
|
|
|
|
- AbstractAuthenticationToken authRequest;
|
|
|
- String principal;
|
|
|
- String credentials;
|
|
|
+ AbstractAuthenticationToken authRequest;
|
|
|
+ String principal;
|
|
|
+ String credentials;
|
|
|
|
|
|
- // 手机验证码登陆
|
|
|
- principal = obtainParameter(request, SPRING_SECURITY_RESTFUL_PHONE_KEY);
|
|
|
- credentials = obtainParameter(request, SPRING_SECURITY_RESTFUL_VERIFY_CODE_KEY);
|
|
|
+ // 手机验证码登陆
|
|
|
+ principal = obtainParameter(request, SPRING_SECURITY_RESTFUL_PHONE_KEY);
|
|
|
+ credentials = obtainParameter(request, SPRING_SECURITY_RESTFUL_VERIFY_CODE_KEY);
|
|
|
|
|
|
- principal = principal.trim();
|
|
|
- authRequest = new PhoneAuthenticationToken(principal, credentials);
|
|
|
+ principal = principal.trim();
|
|
|
+ authRequest = new PhoneAuthenticationToken(SecurityConstants.PHONE_PRINCIPAL_PREFIX + principal, credentials);
|
|
|
|
|
|
- // Allow subclasses to set the "details" property
|
|
|
- setDetails(request, authRequest);
|
|
|
- return this.getAuthenticationManager().authenticate(authRequest);
|
|
|
- }
|
|
|
+ // Allow subclasses to set the "details" property
|
|
|
+ setDetails(request, authRequest);
|
|
|
+ return this.getAuthenticationManager().authenticate(authRequest);
|
|
|
+ }
|
|
|
|
|
|
@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);
|
|
|
}
|
|
|
|
|
|
- private void setDetails(HttpServletRequest request,
|
|
|
- AbstractAuthenticationToken authRequest) {
|
|
|
- authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
|
|
|
- }
|
|
|
+ private void setDetails(HttpServletRequest request, AbstractAuthenticationToken authRequest) {
|
|
|
+ authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
|
|
|
+ }
|
|
|
|
|
|
- private String obtainParameter(HttpServletRequest request, String parameter) {
|
|
|
- String result = request.getParameter(parameter);
|
|
|
- return result == null ? "" : result;
|
|
|
- }
|
|
|
+ private String obtainParameter(HttpServletRequest request, String parameter) {
|
|
|
+ String result = request.getParameter(parameter);
|
|
|
+ return result == null ? "" : result;
|
|
|
+ }
|
|
|
}
|