package net.davidvoid.thor.lightning.security;

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

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 {
        System.out.println("hehe");
        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 null;
    }

}

@@ -123,24 +123,59 @@
 il.List;%0D%0A%0D%0A
+import javax.servlet.FilterChain;%0D%0A
 import javax
@@ -194,32 +194,32 @@
 vletException;%0D%0A
-
 import javax.ser
@@ -911,22 +911,18 @@
 lter %7B%0D%0A
-    
 %0D%0A
+
     publ
@@ -974,17 +974,16 @@
 (%22/**%22);
- 
 %0D%0A    %7D%0D
@@ -1366,45 +1366,8 @@
  %7B%0D%0A
-        System.out.println(%22hehe%22);%0D%0A
     
@@ -1861,13 +1861,8 @@
 123%22
-); //
 , au
@@ -1889,20 +1889,355 @@
  return 
-null
+user;%0D%0A    %7D%0D%0A%0D%0A    @Override%0D%0A    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException %7B%0D%0A        super.successfulAuthentication(request, response, chain, authResult);%0D%0A        chain.doFilter(request, response)
 ;%0D%0A    %7D

return content when pass authentication