AuthorizationHeaderUtils.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.maxkey.util;
  17. import javax.servlet.http.HttpServletRequest;
  18. import org.maxkey.crypto.Base64Utils;
  19. /**
  20. * @author Crystal.Sea
  21. *
  22. */
  23. public class AuthorizationHeaderUtils {
  24. /**
  25. * first UpperCase
  26. */
  27. public static final String HEADER_Authorization = "Authorization";
  28. /**
  29. * first LowerCase
  30. */
  31. public static final String HEADER_authorization = "authorization";
  32. public static String createBasic(String username, String password) {
  33. String authUserPass = username + ":" + password;
  34. String encodedAuthUserPass = Base64Utils.encode(authUserPass);
  35. return AuthorizationHeaderCredential.Credential.BASIC + encodedAuthUserPass;
  36. }
  37. public static String createBearer(String bearer) {
  38. return AuthorizationHeaderCredential.Credential.BEARER + bearer;
  39. }
  40. public static AuthorizationHeaderCredential resolve(HttpServletRequest request) {
  41. String authorization = resolveBearer(request);
  42. return resolve(authorization);
  43. }
  44. public static AuthorizationHeaderCredential resolve(String authorization) {
  45. if (StringUtils.isNotBlank(authorization) && isBasic(authorization)) {
  46. String decodeUserPass = Base64Utils.decode(authorization.split(" ")[1]);
  47. String []userPass =decodeUserPass.split(":");
  48. return new AuthorizationHeaderCredential(userPass[0],userPass[1]);
  49. } else {
  50. return new AuthorizationHeaderCredential(resolveBearer(authorization));
  51. }
  52. }
  53. public static String resolveBearer(HttpServletRequest request) {
  54. String authorization =
  55. StringUtils.isNotBlank(request.getHeader(HEADER_Authorization)) ?
  56. request.getHeader(HEADER_Authorization) : request.getHeader(HEADER_authorization);
  57. if(StringUtils.isNotBlank(authorization)) {
  58. return resolveBearer(authorization);
  59. }
  60. return null;
  61. }
  62. public static boolean isBasic(String basic) {
  63. if (basic.startsWith(AuthorizationHeaderCredential.Credential.BASIC)) {
  64. return true;
  65. } else {
  66. return false;
  67. }
  68. }
  69. static String resolveBearer(String bearer) {
  70. if (StringUtils.isNotBlank(bearer) && isBearer(bearer)) {
  71. return bearer.split(" ")[1];
  72. } else {
  73. return bearer;
  74. }
  75. }
  76. static boolean isBearer(String bearer) {
  77. if (bearer.toLowerCase().startsWith(AuthorizationHeaderCredential.Credential.BEARER.toLowerCase())) {
  78. return true;
  79. } else {
  80. return false;
  81. }
  82. }
  83. }