package net.davidvoid.thor.lightning.security;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;

public class JwtAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

    public JwtAuthenticationFilter() {
        super("/**");
    }

    @Override
    protected boolean requiresAuthentication(HttpServletRequest request,
            HttpServletResponse response) {
        return true;
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request,
            HttpServletResponse response) throws AuthenticationException,
            IOException, ServletException {
        String header = request.getHeader("Authorization");
        
        if (header == null || !header.startsWith("Bearer ")) {
            throw new BadCredentialsException("");
        }
        
        String token = header.substring(7);
        
        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("USER"));
        UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken("david", "123", authorities);
        return user;
    }

    @Override
    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
        super.successfulAuthentication(request, response, chain, authResult);
        chain.doFilter(request, response);
    }

}

@@ -821,16 +821,86 @@
 Filter;%0D
+%0Aimport org.springframework.security.web.util.matcher.RequestMatcher;%0D
 %0A%0D%0Apubli
@@ -1022,198 +1022,256 @@
 ter(
-) %7B%0D%0A        super(%22/**%22);%0D%0A    %7D%0D%0A%0D%0A    @Override%0D%0A    protected boolean requiresAuthentication(HttpServletRequest request,%0D%0A            HttpServletResponse response) %7B%0D%0A        return true
+String defaultFilterProcessesUrl) %7B%0D%0A        super(defaultFilterProcessesUrl);%0D%0A    %7D%0D%0A%0D%0A    protected JwtAuthenticationFilter(%0D%0A            RequestMatcher requiresAuthenticationRequestMatcher) %7B%0D%0A        super(requiresAuthenticationRequestMatcher)
 ;%0D%0A 
@@ -1985,16 +1985,21 @@
 %22, %22123%22
+); //
 , author

add constructors