AuthUtils.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package com.gihon.common.auth;
  2. import java.util.Enumeration;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.springframework.web.context.request.RequestAttributes;
  6. import org.springframework.web.context.request.RequestContextHolder;
  7. import org.springframework.web.context.request.ServletRequestAttributes;
  8. /**
  9. * 当前用户认证工具
  10. *
  11. * @author baihe
  12. *
  13. */
  14. public class AuthUtils {
  15. public final static String ROLE_SUPER_ADMIN = "superadmin";// 超级管理员
  16. public final static String ROLE_COMPANY_ADMIN = "companyadmin";// 公司管理员
  17. public final static String REQ_USER = "_req_authuser_";
  18. public final static String REQ_TOKEN = "_req_token_";
  19. public final static String REQ_BLANK = "_req_blank_";// 参数中传递的token
  20. public final static Long TOKEN_EXPIRE_TIME = 7200L;// 3个小时
  21. public static ServletRequestAttributes getRequestAttributes() {
  22. RequestAttributes r = RequestContextHolder.currentRequestAttributes();
  23. return (ServletRequestAttributes) r;
  24. }
  25. public static HttpServletRequest getRequest() {
  26. ServletRequestAttributes r = getRequestAttributes();
  27. return r == null ? null : r.getRequest();
  28. }
  29. public static HttpServletResponse getResponse() {
  30. ServletRequestAttributes r = getRequestAttributes();
  31. return r == null ? null : r.getResponse();
  32. }
  33. public static String getToken() {
  34. HttpServletRequest request = getRequest();
  35. if (request == null) {
  36. return null;
  37. }
  38. return (String) request.getAttribute(AuthUtils.REQ_TOKEN);
  39. }
  40. public static Long getUserId() {
  41. AuthUser user = getUser();
  42. if (user != null) {
  43. return user.getId();
  44. }
  45. return null;
  46. }
  47. public static AuthUser getUser() {
  48. HttpServletRequest request = getRequest();
  49. if (request == null) {
  50. return null;
  51. }
  52. AuthUser user = null;
  53. Object o = request.getAttribute(REQ_USER);
  54. if (o != null) {
  55. user = (AuthUser) o;
  56. }
  57. return user;
  58. }
  59. public static Long getCompanyId() {
  60. AuthUser user = getUser();
  61. if (user != null) {
  62. return user.getCompanyId();
  63. }
  64. return null;
  65. }
  66. public static void setUser(AuthUser user) {
  67. getRequest().setAttribute(REQ_USER, user);
  68. }
  69. public static String parseToken(HttpServletRequest request) {
  70. // first check the header...
  71. String token = parseHeaderToken(request);
  72. // bearer type allows a request parameter as well
  73. if (token == null) {
  74. token = request.getParameter(AuthConstans.ACCESS_TOKEN);
  75. }
  76. return token;
  77. }
  78. @SuppressWarnings("unchecked")
  79. public static String parseHeaderToken(HttpServletRequest request) {
  80. Enumeration<String> headers = request.getHeaders(AuthConstans.HEADE_TOKEN);
  81. while (headers.hasMoreElements()) { // typically there is only one (most servers enforce that)
  82. String value = headers.nextElement();
  83. if ((value.toLowerCase().startsWith(AuthConstans.BEARER_TYPE.toLowerCase()))) {
  84. String authHeaderValue = value.substring(AuthConstans.BEARER_TYPE.length()).trim();
  85. int commaIndex = authHeaderValue.indexOf(',');
  86. if (commaIndex > 0) {
  87. authHeaderValue = authHeaderValue.substring(0, commaIndex);
  88. }
  89. return authHeaderValue;
  90. } else {
  91. // todo: support additional authorization schemes for different token types,
  92. // e.g. "MAC" specified by
  93. // http://tools.ietf.org/html/draft-hammer-oauth-v2-mac-token
  94. String authHeaderValue = value.trim();
  95. int commaIndex = authHeaderValue.indexOf(',');
  96. if (commaIndex > 0) {
  97. authHeaderValue = authHeaderValue.substring(0, commaIndex);
  98. }
  99. return authHeaderValue;
  100. }
  101. }
  102. return request.getHeader(AuthConstans.ACCESS_TOKEN);
  103. }
  104. }