/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.isalnikov.config.auth;

import java.io.IOException;
import java.util.Arrays;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Service;

/**
 *
 * @author Igor Salnikov
 */
public class UserAuthorizationFilter extends UsernamePasswordAuthenticationFilter {

    public UserAuthorizationFilter() {
        setFilterProcessesUrl("/login");
        setUsernameParameter("username");
        setPasswordParameter("password");
        

    }

//    @Autowired
//    @Override
//    public void setAuthenticationManager(AuthenticationManager authenticationManager) {
//        super.setAuthenticationManager(authenticationManager); //To change body of generated methods, choose Tools | Templates.
//    }
//
//    @Autowired
//    @Override
//    public void setAuthenticationSuccessHandler(AuthenticationSuccessHandler successHandler) {
//        super.setAuthenticationSuccessHandler(successHandler);
//    }
//
//    @Autowired
//    @Override
//    public void setAuthenticationFailureHandler(AuthenticationFailureHandler failureHandler) {
//        super.setAuthenticationFailureHandler(failureHandler);
//    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        if ("POST".equals(request.getMethod())) {
            super.doFilter(req, resp, chain);
        } else {
            chain.doFilter(req, resp);

        }
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {

        String sslId = request.getHeader("SSL_CLIENT_S_DN_CN");

        if (sslId == null) {
            sslId = "1000000415";
        }

//        if (request.getHeader("Authorization") == null) {
//            return null; // no header found, continue on to other security filters
//        }
        String userName = obtainUsername(request);
        String password = obtainPassword(request);

        UserAuthorizationToken token =  new UserAuthorizationToken(userName, password, sslId ,Arrays.asList(UserAuthority.ROLE_USER));
        return token;//super.attemptAuthentication(request, response);
        
    }

}

@@ -1193,25 +1193,16 @@
 sword%22);
-%0A        
 %0A%0A    %7D%0A
@@ -1871,25 +1871,24 @@
 r);%0A//    %7D%0A
-%0A
     @Overrid
@@ -2470,82 +2470,8 @@
 );%0A%0A
-        if (sslId == null) %7B%0A            sslId = %221000000415%22;%0A        %7D%0A%0A
 //  
@@ -2722,24 +2722,57 @@
 (request);%0A%0A
+        if (sslId != null) %7B%0A    
         User
@@ -2797,17 +2797,16 @@
  token =
- 
  new Use
@@ -2854,10 +2854,10 @@
 slId
- 
 ,
+ 
 Arra
@@ -2893,16 +2893,20 @@
 USER));%0A
+    
         
@@ -2976,16 +2976,39 @@
         
+%7D%0A%0A        return null;
 %0A    %7D%0A%0A

add auth