package com.tofly.common.oauth.config;

import com.tofly.common.core.configs.FilterPassUrlConfig;
import com.tofly.common.oauth.compent.ResourceAuthExceptionEntryPoint;
import com.tofly.common.oauth.compent.ToFlyUserAuthenticationConverter;
import com.tofly.common.oauth.compent.ToflyAccessDecisionManager;
import com.tofly.common.oauth.compent.ToflyAccessDeniedHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
import org.springframework.security.oauth2.provider.token.UserAuthenticationConverter;
import org.springframework.web.client.RestTemplate;

/**
 * @author Administrator
 * @date 2019/9/9  15:28
 */
@Slf4j
@EnableResourceServer
@Configuration
@AllArgsConstructor
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class ToflyResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    private final ResourceAuthExceptionEntryPoint resourceAuthExceptionEntryPoint;
    private final FilterPassUrlConfig filterPassUrlConfig;
    private final RemoteTokenServices remoteTokenServices;
    private final RestTemplate ehRestTemplate;
    private final ToflyAccessDeniedHandler toflyAccessDeniedHandler;
    private final ToflyAccessDecisionManager toflyAccessDecisionManager;
    //private AuthenticationTokenFilter AuthenticationTokenFilter;



    @Override
    public void configure(HttpSecurity http) throws Exception {
        /*http.addFilterBefore(AuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);*/
        http.headers().frameOptions().disable();
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>
                .ExpressionInterceptUrlRegistry registry = http
                .authorizeRequests();
        filterPassUrlConfig.getUrls()
                .forEach(url -> registry.antMatchers(url).permitAll());
        registry.anyRequest().authenticated()
//                .and().authorizeRequests()
//                .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
//                    @Override
//                    public <O extends FilterSecurityInterceptor> O postProcess(O o) {
//                        o.setAccessDecisionManager(toflyAccessDecisionManager);
//                        return o;
//                    }
//                })
                .and().csrf().disable();
//        http.csrf().disable().authorizeRequests().antMatchers(filterPassUrlConfig.getUrls().stream().toArray(String[]::new))
//                .permitAll().anyRequest().authenticated()
//            .and().authorizeRequests().withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
//                @Override
//                public <O extends FilterSecurityInterceptor> O postProcess(O o) {
//                    o.setAccessDecisionManager(toflyAccessDecisionManager);
//                    return o;
//                }
//            })
//            .and().csrf().disable();
    }


    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        /*remoteTokenServices.setRestTemplate(ehRestTemplate);
        resources.tokenServices(remoteTokenServices);
        resources.resourceId(SecurityConstants.RSOURCE_ID).stateless(true);*/

        DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
        UserAuthenticationConverter userTokenConverter = new ToFlyUserAuthenticationConverter();
        //DefaultUserAuthenticationConverter userTokenConverter = new DefaultUserAuthenticationConverter();
        accessTokenConverter.setUserTokenConverter(userTokenConverter);

        remoteTokenServices.setRestTemplate(ehRestTemplate);
        remoteTokenServices.setAccessTokenConverter(accessTokenConverter);
        resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
                .accessDeniedHandler(toflyAccessDeniedHandler)
                .tokenServices(remoteTokenServices)
                ;
    }
}
