spring中request.getHeader("Authorization ")为空
时间: 2023-07-21 11:04:01 浏览: 505
如果在Spring中使用`request.getHeader("Authorization")`获取HTTP请求头Authorization的值为空,这可能是因为缺少某些配置。
首先,请确保请求中实际上包含Authorization头。您可以使用类似Postman这样的工具来发送带有Authorization头的请求,以确保它存在。
如果请求中确实存在Authorization头,那么可能是因为Spring Security拦截了请求并将Authorization头解析为安全上下文中的身份验证凭据。在这种情况下,您可以尝试通过以下方式访问身份验证凭据:
```java
SecurityContextHolder.getContext().getAuthentication().getCredentials();
```
如果仍然无法解决问题,请检查您的Spring Security配置,确保它正确地设置了安全过滤器链并正确地解析Authorization头。您可以参考Spring Security文档进行配置。
相关问题
package com.interceptor; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import com.alibaba.fastjson.JSONObject; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; import com.annotation.IgnoreAuth; import com.entity.EIException; import com.entity.TokenEntity; import com.service.TokenService; import com.utils.R; /** * 权限(Token)验证 */ @Component public class AuthorizationInterceptor implements HandlerInterceptor { public static final String LOGIN_TOKEN_KEY = "Token"; @Autowired private TokenService tokenService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //支持跨域请求 response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Allow-Headers", "x-requested-with,request-source,Token, Origin,imgType, Content-Type, cache-control,postman-token,Cookie, Accept,authorization"); response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); IgnoreAuth annotation; if (handler instanceof HandlerMethod) { annotation = ((HandlerMethod) handler).getMethodAnnotation(IgnoreAuth.class); } else { return true; } //从header中获取token String token = request.getHeader(LOGIN_TOKEN_KEY); /** * 不需要验证权限的方法直接放过 */ if(annotation!=null) { return true; } TokenEntity tokenEntity = null; if(StringUtils.isNotBlank(token)) { tokenEntity = tokenService.getTokenEntity(token); } if(tokenEntity != null) { request.getSession().setAttribute("userId", tokenEntity.getUserid()); request.getSession().setAttribute("role", tokenEntity.getRole()); request.getSession().setAttribute("tableName", tokenEntity.getTablename()); request.getSession().setAttribute("username", tokenEntity.getUsername()); return true; } PrintWriter writer = null; response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); try { writer = response.getWriter(); writer.print(JSONObject.toJSONString(R.error(401, "请先登录"))); } finally { if(writer != null){ writer.close(); } } // throw new EIException("请先登录", 401); return false; } }
这段代码是一个Spring Boot应用程序中的拦截器,用于验证用户的Token令牌是否有效。它会在每个请求之前拦截并验证Token,如果Token有效则允许请求继续进行,否则返回一个401错误并提示用户先登录。其中,@IgnoreAuth注解表示该请求不需要验证Token。这段代码还支持跨域请求,设置了一些CORS相关的header信息。
public static final String LOGIN_TOKEN_KEY = "Token"; @Autowired private TokenService tokenService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //支持跨域请求 response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Allow-Headers", "x-requested-with,request-source,Token, Origin,imgType, Content-Type, cache-control,postman-token,Cookie, Accept,authorization"); response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); // 跨域时会首先发送一个OPTIONS请求,这里我们给OPTIONS请求直接返回正常状态 if (request.getMethod().equals(RequestMethod.OPTIONS.name())) { response.setStatus(HttpStatus.OK.value()); return false; } IgnoreAuth annotation; if (handler instanceof HandlerMethod) { annotation = ((HandlerMethod) handler).getMethodAnnotation(IgnoreAuth.class); } else { return true; } //从header中获取token String token = request.getHeader(LOGIN_TOKEN_KEY);
这段代码是一个 Java Spring 框架中的拦截器,用于处理在请求中传递的 Token 信息。它首先设置了跨域请求的相关响应头信息,然后判断请求是否为 OPTIONS 请求,如果是则返回正常状态。接着,它通过 @IgnoreAuth 注解判断该请求是否需要进行身份验证,如果不需要,则直接返回 true。最后,它从请求头中获取 Token 信息。这段代码的作用是确保请求合法且有权限访问对应的资源。
阅读全文