package ink.wgink.login.oauth2.client.config;


import ink.wgink.login.oauth2.client.converter.OAuth2ClientUserAccessTokenConverter;
import ink.wgink.properties.BaseProperties;
import ink.wgink.properties.oauth2.client.OAuth2ClientProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

/**
 * @ClassName: AuthClientSecurityConfig
 * @Description:
 * @Author: admin
 * @Date: 2019-07-28 13:30:01
 **/
@EnableWebSecurity
@EnableOAuth2Sso
public class OAuth2ClientConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private BaseProperties baseProperties;
    @Autowired
    private OAuth2ClientProperties oAuth2ClientProperties;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        /**
         * 默认放行配置
         */
        String[] defaultAntMatchers = {
                "/app/**",
                "/approute/**",
                "/wechat/**",
                "/wechat-miniapp/**",
                "/route/file/**",
        };
        http.formLogin()
                .defaultSuccessUrl("/authorize", true)
                .and()
                .logout().logoutSuccessUrl(oAuth2ClientProperties.getOauthLogout())
                .and()
                .authorizeRequests().antMatchers(defaultAntMatchers).permitAll()
                .and()
                .authorizeRequests()
                .anyRequest()
                .access("@accessWhiteList.hasPermission(request, authentication)")
                .and()
                .headers().frameOptions().sameOrigin()
                .and()
                .cors()
                .and()
                .csrf().disable();
    }

    @Bean
    @Primary
    public DefaultTokenServices defaultTokenServices() {
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore(jwtTokenStore());
        return defaultTokenServices;
    }

    @Bean
    public JwtTokenStore jwtTokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        jwtAccessTokenConverter.setAccessTokenConverter(new OAuth2ClientUserAccessTokenConverter());
        jwtAccessTokenConverter.setSigningKey("WGINK");
        return jwtAccessTokenConverter;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
