Java Code Examples for org.springframework.web.context.request.NativeWebRequest#getHeader()
The following examples show how to use
org.springframework.web.context.request.NativeWebRequest#getHeader() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AuthMethodArgumentResolver.java From springboot-restful-starter with MIT License | 6 votes |
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { // 从Header取出AccessToken String token = webRequest.getHeader("Access-Token"); // 判断Token是否为空 if (token != null && token != "") { try { // 从Token中获取Id并捕获异常 return tokenService.parse(TokenSubject.ACCESS, token).getBody().get("id"); } catch (TokenException e) { // Token失效,抛出认证异常 throw new AuthException(); } } else if (!parameter.getParameterAnnotation(Auth.class).required()) { // Token为空,判断Token是否可以为空 return null; } else { // Token不能为空,抛出未登录异常 throw new UnauthorizedException(); } }
Example 2
Source File: HeaderContentNegotiationStrategy.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} * @throws HttpMediaTypeNotAcceptableException if the 'Accept' header * cannot be parsed. */ @Override public List<MediaType> resolveMediaTypes(NativeWebRequest request) throws HttpMediaTypeNotAcceptableException { String header = request.getHeader(HttpHeaders.ACCEPT); if (!StringUtils.hasText(header)) { return Collections.emptyList(); } try { List<MediaType> mediaTypes = MediaType.parseMediaTypes(header); MediaType.sortBySpecificityAndQuality(mediaTypes); return mediaTypes; } catch (InvalidMediaTypeException ex) { throw new HttpMediaTypeNotAcceptableException( "Could not parse 'Accept' header [" + header + "]: " + ex.getMessage()); } }
Example 3
Source File: InitializrWebConfig.java From initializr with Apache License 2.0 | 6 votes |
@Override public List<MediaType> resolveMediaTypes(NativeWebRequest request) { String path = this.urlPathHelper .getPathWithinApplication(request.getNativeRequest(HttpServletRequest.class)); if (!StringUtils.hasText(path) || !path.equals("/")) { // Only care about "/" return MEDIA_TYPE_ALL_LIST; } String userAgent = request.getHeader(HttpHeaders.USER_AGENT); if (userAgent != null) { Agent agent = Agent.fromUserAgent(userAgent); if (agent != null) { if (AgentId.CURL.equals(agent.getId()) || AgentId.HTTPIE.equals(agent.getId())) { return Collections.singletonList(MediaType.TEXT_PLAIN); } } } return Collections.singletonList(MediaType.APPLICATION_JSON); }
Example 4
Source File: LoginUserHandlerMethodArgumentResolver.java From BigDataPlatform with GNU General Public License v3.0 | 5 votes |
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container, NativeWebRequest request, WebDataBinderFactory factory) throws Exception { // return new Integer(1); String token = request.getHeader(LOGIN_TOKEN_KEY); if (token == null || token.isEmpty()) { return null; } return UserTokenManager.getUserId(token); }
Example 5
Source File: LoginUserHandlerMethodArgumentResolver.java From dts-shop with GNU Lesser General Public License v3.0 | 5 votes |
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container, NativeWebRequest request, WebDataBinderFactory factory) throws Exception { // return new Integer(1); String token = request.getHeader(LOGIN_TOKEN_KEY); if (token == null || token.isEmpty()) { return null; } return UserTokenManager.getUserId(token); }
Example 6
Source File: LoginUserHandlerMethodArgumentResolver.java From mall with MIT License | 5 votes |
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container, NativeWebRequest request, WebDataBinderFactory factory) throws Exception { // return new Integer(1); String token = request.getHeader(LOGIN_TOKEN_KEY); if (token == null || token.isEmpty()) { return null; } return UserTokenManager.getUserId(token); }
Example 7
Source File: LoginUserHandlerMethodArgumentResolver.java From litemall with MIT License | 5 votes |
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container, NativeWebRequest request, WebDataBinderFactory factory) throws Exception { // return new Integer(1); String token = request.getHeader(LOGIN_TOKEN_KEY); if (token == null || token.isEmpty()) { return null; } return UserTokenManager.getUserId(token); }
Example 8
Source File: TokenToUserMethodArgumentResolver.java From spring-boot-projects with Apache License 2.0 | 5 votes |
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { if (parameter.getParameterAnnotation(TokenToUser.class) instanceof TokenToUser) { AdminUser adminUser = null; String token = webRequest.getHeader("token"); if (null != token && !"".equals(token) && token.length() == 32) { adminUser = adminUserService.getAdminUserByToken(token); } return adminUser; } return null; }
Example 9
Source File: AccountAccessTokenArgumentResolver.java From kaif with Apache License 2.0 | 5 votes |
@Override public AccountAccessToken resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { String token = webRequest.getHeader(AccountAccessToken.HEADER_KEY); // we only verify in memory for all request http method // service layer should decide check database if mutation is critical return accountService.tryDecodeAccessToken(token).orElseThrow(AccessDeniedException::new); }