AuthorizationHeaderUtils 优化

This commit is contained in:
MaxKey
2023-03-03 11:07:52 +08:00
parent c63f6f96d7
commit 39673103fb
3 changed files with 53 additions and 54 deletions

View File

@@ -13,37 +13,35 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.maxkey.util; package org.maxkey.util;
public class AuthorizationHeaderCredential { public class AuthorizationHeaderCredential {
public static class Credential{ public static class Credential {
public static final String BASIC = "Basic "; public static final String BASIC = "Basic ";
public static final String BEARER = "Bearer "; public static final String BEARER = "Bearer ";
} }
String credentialType = Credential.BASIC; String credentialType = Credential.BASIC;
String username; String username;
String credential; String credential;
String authorization; String authorization;
public AuthorizationHeaderCredential(String bearer) { public AuthorizationHeaderCredential(String bearer) {
super(); super();
this.credential = bearer; this.credential = bearer;
this.credentialType = Credential.BEARER; this.credentialType = Credential.BEARER;
} }
public AuthorizationHeaderCredential(String username, String credential) { public AuthorizationHeaderCredential(String username, String credential) {
super(); super();
this.username = username; this.username = username;
this.credential = credential; this.credential = credential;
} }
public String getCredentialType() { public String getCredentialType() {
return credentialType; return credentialType;
} }
@@ -63,24 +61,27 @@ public class AuthorizationHeaderCredential {
public String getCredential() { public String getCredential() {
return credential; return credential;
} }
public void setCredential(String credential) { public void setCredential(String credential) {
this.credential = credential; this.credential = credential;
} }
public String transform() { public String transform() {
if(credentialType.equalsIgnoreCase(Credential.BASIC)) { if (credentialType.equalsIgnoreCase(Credential.BASIC)) {
return AuthorizationHeaderUtils.createBasic(username, credential); return AuthorizationHeaderUtils.createBasic(username, credential);
}else { } else {
return AuthorizationHeaderUtils.createBearer(credential); return AuthorizationHeaderUtils.createBearer(credential);
} }
} }
public boolean isBasic() {
return credentialType.equals(Credential.BASIC) ? true : false;
}
@Override @Override
public String toString() { public String toString() {
return "AuthorizationHeaderCredential [credentialType=" + credentialType + ", username=" + username return "AuthorizationHeaderCredential [credentialType=" + credentialType + ", username=" + username
+ ", credential=" + credential + "]"; + ", credential=" + credential + "]";
} }
} }

View File

@@ -17,8 +17,6 @@
package org.maxkey.util; package org.maxkey.util;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.maxkey.crypto.Base64Utils; import org.maxkey.crypto.Base64Utils;
@@ -43,6 +41,15 @@ public class AuthorizationHeaderUtils {
String encodedAuthUserPass = Base64Utils.encode(authUserPass); String encodedAuthUserPass = Base64Utils.encode(authUserPass);
return AuthorizationHeaderCredential.Credential.BASIC + encodedAuthUserPass; return AuthorizationHeaderCredential.Credential.BASIC + encodedAuthUserPass;
} }
public static String createBearer(String bearer) {
return AuthorizationHeaderCredential.Credential.BEARER + bearer;
}
public static AuthorizationHeaderCredential resolve(HttpServletRequest request) {
String authorization = resolveBearer(request);
return resolve(authorization);
}
public static AuthorizationHeaderCredential resolve(String authorization) { public static AuthorizationHeaderCredential resolve(String authorization) {
if (StringUtils.isNotBlank(authorization) && isBasic(authorization)) { if (StringUtils.isNotBlank(authorization) && isBasic(authorization)) {
@@ -54,34 +61,6 @@ public class AuthorizationHeaderUtils {
} }
} }
public static boolean isBasic(String basic) {
if (basic.startsWith(AuthorizationHeaderCredential.Credential.BASIC)) {
return true;
} else {
return false;
}
}
public static String createBearer(String bearer) {
return AuthorizationHeaderCredential.Credential.BEARER + bearer;
}
public static String resolveBearer(String bearer) {
if (StringUtils.isNotBlank(bearer) && isBearer(bearer)) {
return bearer.split(" ")[1];
} else {
return bearer;
}
}
public static boolean isBearer(String bearer) {
if (bearer.toLowerCase().startsWith(AuthorizationHeaderCredential.Credential.BEARER.toLowerCase())) {
return true;
} else {
return false;
}
}
public static String resolveBearer(HttpServletRequest request) { public static String resolveBearer(HttpServletRequest request) {
String authorization = String authorization =
StringUtils.isNotBlank(request.getHeader(HEADER_Authorization)) ? StringUtils.isNotBlank(request.getHeader(HEADER_Authorization)) ?
@@ -92,10 +71,30 @@ public class AuthorizationHeaderUtils {
return null; return null;
} }
public static HashMap<String,String> authorization(String authorization) { public static boolean isBasic(String basic) {
HashMap<String,String> authorizationMap = new HashMap<String,String>(); if (basic.startsWith(AuthorizationHeaderCredential.Credential.BASIC)) {
authorizationMap.put(HEADER_Authorization, authorization); return true;
return authorizationMap; } else {
return false;
}
} }
static String resolveBearer(String bearer) {
if (StringUtils.isNotBlank(bearer) && isBearer(bearer)) {
return bearer.split(" ")[1];
} else {
return bearer;
}
}
static boolean isBearer(String bearer) {
if (bearer.toLowerCase().startsWith(AuthorizationHeaderCredential.Credential.BEARER.toLowerCase())) {
return true;
} else {
return false;
}
}
} }

View File

@@ -58,9 +58,8 @@ public class Oauth20ApiPermissionAdapter implements AsyncHandlerInterceptor {
@Override @Override
public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception { public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
_logger.trace("Oauth20ApiPermissionAdapter preHandle"); _logger.trace("Oauth20ApiPermissionAdapter preHandle");
String authorization = request.getHeader(AuthorizationHeaderUtils.HEADER_Authorization); String accessToken = AuthorizationHeaderUtils.resolveBearer(request);
String accessToken = AuthorizationHeaderUtils.resolveBearer(authorization);
OAuth2Authentication authentication = oauth20TokenServices.loadAuthentication(accessToken); OAuth2Authentication authentication = oauth20TokenServices.loadAuthentication(accessToken);
//判断应用的accessToken信息 //判断应用的accessToken信息