/*
 * JwtFilter.java
 *
 * Copyright (C) 2017 [ A Legge Up ]
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

package com.aleggeup.confagrid.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.impl.DefaultClaims;

public class JwtFilter extends OncePerRequestFilter {

    private static final Logger LOGGER = LoggerFactory.getLogger(JwtFilter.class);

    public static final String ATTRIBUTE_CLAIMS = "claims";
    public static final String BEARER_PREFIX = "Bearer ";
    public static final String HEADER_AUTHORIZATION = "Authorization";
    public static final String HEADER_CLAIMS_SUBJECT = "X-Claims-Subject";
    public static final String SECRET_KEY = "secretkey";

    @Override
    protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain)
            throws ServletException, IOException {

        final String authHeader = request.getHeader(HEADER_AUTHORIZATION);
        Claims claims = null;
        SecurityContextHolder.getContext().setAuthentication(null);
        if (authHeader == null || !authHeader.startsWith(BEARER_PREFIX)) {
            LOGGER.info("Missing or invalid Authorization header.");
            claims = new DefaultClaims().setSubject("anonymous");
        } else {
            try {
                final String token = authHeader.substring(BEARER_PREFIX.length());
                claims = Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token).getBody();
            } catch(final JwtException e) {
                LOGGER.warn("Unable to get claims from bearer token", e);
                claims = new DefaultClaims().setSubject("anonymous");
            }
        }

        if (claims != null) {
            response.setHeader(HEADER_CLAIMS_SUBJECT, claims.getSubject());
        }

        chain.doFilter(request, response);
    }
}

@@ -1211,16 +1211,80 @@
 retkey%22;
+%0A    public static final String SUBJECT_ANONYMOUS = %22anonymous%22;
 %0A%0A    @O
@@ -1839,35 +1839,41 @@
 .setSubject(
-%22anonymous%22
+SUBJECT_ANONYMOUS
 );%0A        %7D
@@ -2253,27 +2253,33 @@
 Subject(
-%22anonymous%22
+SUBJECT_ANONYMOUS
 );%0A     

Put anonymous string into a constant