Example usage for javax.servlet.http HttpServletRequest getHeader

List of usage examples for javax.servlet.http HttpServletRequest getHeader

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getHeader.

Prototype

public String getHeader(String name);

Source Link

Document

Returns the value of the specified request header as a String.

Usage

From source file:org.openwms.client.security.AuthenticationTokenProcessingFilter.java

/**
 * {@inheritDoc}//from   ww  w  .  j  a  v  a  2s . com
 * 
 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
 *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    if (!(request instanceof HttpServletRequest)) {
        throw new RuntimeException("Expecting a http servlet request");
    }

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    String authToken = httpRequest.getHeader(AUTH_TOKEN);

    String userName = TokenUtils.getUserNameFromToken(authToken);
    if (userName != null) {

        // The returned UserDetails object has credentials encoded, we rely
        // on two AuthenticationProviders here to
        // come around this issue, one with PasswordEncoder and one without
        UserDetails userDetails = this.userService.loadUserByUsername(userName);
        if (TokenUtils.validateToken(authToken, userDetails)) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
                    userDetails.getUsername(), userDetails.getPassword());
            authentication.setDetails(
                    new WebAuthenticationDetailsSource().buildDetails((HttpServletRequest) request));
            SecurityContextHolder.getContext()
                    .setAuthentication(this.authenticationManager.authenticate(authentication));
        }
    }
    chain.doFilter(request, response);
    SecurityContextHolder.clearContext();
}

From source file:com.hobba.hobaserver.services.security.ChallengeUtil.java

private String getHobaTBS(HttpServletRequest request) {
    String header = request.getHeader("Authorized");
    String[] headerParams = header.split("[.]");
    String kid = headerParams[0];
    kid = new String(Base64.encodeBase64(kid.getBytes()));
    String chalenge = headerParams[1];
    String nonce = headerParams[2];

    String alg = "1";
    String origin = request.getRequestURL().toString();
    origin = origin.split("/")[0] + "//" + origin.split("/")[2] + "/";
    String hobaTbs = nonce + " " + alg + " " + origin + " " + kid + " " + chalenge;
    return hobaTbs;
}

From source file:fr.mycellar.interfaces.web.security.SecurityContextTokenRepository.java

@Override
public boolean containsContext(HttpServletRequest request) {
    try {/*from   w  ww .  j  a v a 2 s.  com*/
        Object key = request.getHeader(SpringSecurityConfiguration.TOKEN_HEADER_NAME);
        if ((key != null) && (key instanceof String)) {
            Token token = keyBasedPersistenceTokenService.verifyToken((String) key);
            if (token != null) {
                return securityContexts.containsKey(token);
            }

        }
    } catch (Exception e) {
        // return false;
    }
    return false;
}

From source file:org.owasp.webgoat.AjaxAuthenticationEntryPoint.java

public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
    if (request.getHeader("x-requested-with") != null) {
        response.sendError(401, authException.getMessage());
    } else {//from w  ww.j  a  v a2s . c om
        super.commence(request, response, authException);
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.i18n.selection.LocaleSelectionController.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String referrer = req.getHeader("referer");

    String selectedLocale = req.getParameter(PARAMETER_SELECTION);

    try {//from w  w  w .  j  a v  a  2 s .c om
        processSelectedLocale(req, selectedLocale);
    } catch (Exception e) {
        log.error("Failed to process the user's Locale selection", e);
    }

    if (StringUtils.isEmpty(referrer)) {
        resp.sendRedirect(UrlBuilder.getHomeUrl());
    } else {
        resp.sendRedirect(referrer);
    }
}

From source file:com.ar.dev.tierra.hasar.api.config.security.CustomLogoutSuccessHandler.java

@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {

    String token = request.getHeader(HEADER_AUTHORIZATION);
    if (token != null && token.startsWith(BEARER_AUTHENTICATION)) {
        OAuth2AccessToken oAuth2AccessToken = tokenStore.readAccessToken(token.split(" ")[1]);
        if (oAuth2AccessToken != null) {
            tokenStore.removeAccessToken(oAuth2AccessToken);
            response.setStatus(HttpServletResponse.SC_OK);
        } else {/*from   w  ww . ja  va  2 s  . c  o  m*/
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }
}

From source file:com.thinkberg.moxo.dav.WebdavHandler.java

/**
 * Get the if header.//w  ww . j  a  v a  2  s . com
 * 
 * @param request
 *            the request
 * @return the value if the If: header.
 */
String getIf(HttpServletRequest request) {
    return request.getHeader("If");
}

From source file:ru.ttk.baloo.rest.security.oauth.Logout.java

private void removeAccess(HttpServletRequest httpServletRequest) {
    if (httpServletRequest != null) {
        String bearerAndToken = httpServletRequest.getHeader("Authorization");
        LOG.info("bearerAndToken:" + bearerAndToken);

        if (StringUtils.isNotBlank(bearerAndToken)
                && bearerAndToken.length() >= OAUTH_HEADER_VALUE_BEARER_PLUS_SPACE.length()) {
            if (bearerAndToken.contains(OAUTH_HEADER_VALUE_BEARER_PLUS_SPACE)) {
                String extractedToken = bearerAndToken
                        .substring(bearerAndToken.indexOf(OAUTH_HEADER_VALUE_BEARER_PLUS_SPACE)
                                + OAUTH_HEADER_VALUE_BEARER_PLUS_SPACE.length())
                        .trim();// ww w  . j  av a2s .c  om

                if (StringUtils.isNotBlank(extractedToken) && extractedToken.length() > 0) {
                    DefaultOAuth2AccessToken oAuth2AccessToken = new DefaultOAuth2AccessToken(extractedToken);
                    if (this.getTokenStore() != null) {
                        this.getTokenStore().removeAccessToken(oAuth2AccessToken);
                        LOG.info("Access OAuth token removed " + oAuth2AccessToken);
                    }
                }
            }
        }
    }
}

From source file:io.hops.hopsworks.common.dao.user.security.audit.AccountAuditFacade.java

private String extractUserAgent(HttpServletRequest httpServletRequest) {
    String userAgent = httpServletRequest.getHeader("User-Agent");
    if (userAgent == null || userAgent.isEmpty()) {
        return "Unknown User-Agent";
    }/* ww w .j a  v  a  2  s  .  c om*/

    return StringUtils.left(userAgent, 255);
}

From source file:it.geosolutions.geostore.services.rest.security.TokenAuthenticationFilter.java

protected void authenticate(HttpServletRequest req) {
    String authHeader = req.getHeader(tokenHeader);

    if (authHeader != null && authHeader.trim().toUpperCase().startsWith(tokenPrefix.toUpperCase())) {
        String token = authHeader.substring(tokenPrefix.length()).trim();
        Authentication auth;/*from ww  w . j a v  a  2  s  . co m*/
        try {
            auth = getCache().get(token).orNull();
            if (auth != null) {
                LOGGER.info("User authenticated using token: " + auth.getName());
                SecurityContextHolder.getContext().setAuthentication(auth);
            }
        } catch (ExecutionException e) {
            LOGGER.error("Error authenticating token", e);
        }
    }

}