|
@@ -12,16 +12,11 @@ package com.yami.shop.security.filter;
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.extra.servlet.ServletUtil;
|
|
|
-import com.yami.shop.common.util.Json;
|
|
|
-import com.yami.shop.security.constants.SecurityConstants;
|
|
|
-import com.yami.shop.security.exception.UnknownGrantTypeExceptionBase;
|
|
|
-import com.yami.shop.security.token.AdminAuthenticationToken;
|
|
|
-import com.yami.shop.security.token.MiniAppAuthenticationToken;
|
|
|
+import com.yami.shop.security.provider.AuthenticationTokenParser;
|
|
|
import org.springframework.security.authentication.AbstractAuthenticationToken;
|
|
|
import org.springframework.security.authentication.AuthenticationServiceException;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.AuthenticationException;
|
|
|
-import org.springframework.security.oauth2.common.util.OAuth2Utils;
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
|
|
|
|
import javax.servlet.ServletInputStream;
|
|
@@ -32,44 +27,28 @@ import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
/**
|
|
|
* 小程序登陆:此时principal为code
|
|
|
- * post:http://127.0.0.1:8086/login?grant_type=mini_app
|
|
|
+ * post:http://127.0.0.1:8086/login
|
|
|
* {principal:code}
|
|
|
* 管理员登陆:
|
|
|
- * post: http://127.0.0.1:8086/login?grant_type=admin
|
|
|
+ * post: http://127.0.0.1:8086/login
|
|
|
* {principal:username,credentials:password}
|
|
|
*/
|
|
|
public class LoginAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
|
|
|
|
|
|
+ private AuthenticationTokenParser authenticationTokenParser;
|
|
|
+
|
|
|
@Override
|
|
|
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
|
|
|
if (!ServletUtil.METHOD_POST.equals(request.getMethod())) {
|
|
|
throw new AuthenticationServiceException(
|
|
|
"Authentication method not supported: " + request.getMethod());
|
|
|
}
|
|
|
- String type = obtainParameter(request, OAuth2Utils.GRANT_TYPE);
|
|
|
-
|
|
|
- AbstractAuthenticationToken authRequest = null;
|
|
|
-
|
|
|
String requestBody = getStringFromStream(request);
|
|
|
|
|
|
if (StrUtil.isBlank(requestBody)) {
|
|
|
throw new AuthenticationServiceException("无法获取输入信息");
|
|
|
}
|
|
|
-
|
|
|
- // 小程序通过code登陆
|
|
|
- if(SecurityConstants.SPRING_SECURITY_RESTFUL_TYPE_MINI_APP.equals(type)){
|
|
|
- authRequest = Json.parseObject(requestBody, MiniAppAuthenticationToken.class);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // 账号密码登陆
|
|
|
- else if (SecurityConstants.SPRING_SECURITY_RESTFUL_TYPE_ADMIN.equals(type)) {
|
|
|
- authRequest = Json.parseObject(requestBody, AdminAuthenticationToken.class);
|
|
|
- }
|
|
|
-
|
|
|
- if (authRequest == null) {
|
|
|
- throw new UnknownGrantTypeExceptionBase("未知的grant_type");
|
|
|
- }
|
|
|
+ AbstractAuthenticationToken authRequest = authenticationTokenParser.parse(requestBody);
|
|
|
|
|
|
// Allow subclasses to set the "details" property
|
|
|
setDetails(request, authRequest);
|
|
@@ -81,12 +60,6 @@ public class LoginAuthenticationFilter extends UsernamePasswordAuthenticationFil
|
|
|
authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
|
|
|
}
|
|
|
|
|
|
- private String obtainParameter(HttpServletRequest request, String parameter) {
|
|
|
- String result = request.getParameter(parameter);
|
|
|
- return result == null ? "" : result;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
private String getStringFromStream(HttpServletRequest req) {
|
|
|
ServletInputStream is;
|
|
|
try {
|
|
@@ -106,4 +79,8 @@ public class LoginAuthenticationFilter extends UsernamePasswordAuthenticationFil
|
|
|
return "";
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void setAuthenticationTokenParser(AuthenticationTokenParser authenticationTokenParser) {
|
|
|
+ this.authenticationTokenParser = authenticationTokenParser;
|
|
|
+ }
|
|
|
}
|