package com.tykj.dev.device.user.config;//package com.tykj.equipmentuser.config;
//
//import com.tykj.equipmentuser.config.handler.MyAccessHandler;
//import MyFailHandler;
//import MyLogoutHandler;
//import MySuccessHandler;
//import UrlAccessDecisionManager;
//import UrlAccessDeniedHandler;
//import UrlFilterInvocationSecurityMetadataSource;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.security.config.annotation.ObjectPostProcessor;
//import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.builders.WebSecurity;
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
//import org.springframework.security.core.session.SessionRegistry;
//import org.springframework.security.core.session.SessionRegistryImpl;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
//import org.springframework.security.web.session.HttpSessionEventPublisher;
//import org.springframework.web.cors.CorsConfiguration;
//import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
//import org.springframework.web.filter.CorsFilter;
//
///**
// * @author zjm
// */
//@Configuration
//@EnableWebSecurity
//public class SecurityConfig1 extends WebSecurityConfigurerAdapter {
//
//
//    @Autowired
//    public MyProvider myProvider;
//    @Autowired
//    private MyAccessHandler accessHandler;
//    @Autowired
//    private MyEntryPoint myEntryPoint;
//
//    @Autowired
//    private MyUserDetailsServiceImpl myUserDetailsServiceImpl;
//
//    @Autowired
//    private MyLogoutHandler myLogoutHandler;
//
//    @Autowired
//    private MyFailHandler failHandler;
//
//    @Autowired
//    private MySuccessHandler successHandler;
//
//    /**
//     * 获取访问url所需要的角色信息
//     */
//    @Autowired
//    private  UrlFilterInvocationSecurityMetadataSource urlFilterInvocationSecurityMetadataSource;
//    /**
//     * 认证权限处理 - 将上面所获得角色权限与当前登录用户的角色做对比，如果包含其中一个角色即可正常访问
//     */
//    @Autowired
//    private  UrlAccessDecisionManager urlAccessDecisionManager;
//    /**
//     * 自定义访问无权限接口时403响应内容
//     */
//    @Autowired
//    private  UrlAccessDeniedHandler urlAccessDeniedHandler;
//
//    @Override
//    public void configure(HttpSecurity httpSecurity) throws Exception {
////        httpSecurity
////                .cors().and()
////                .addFilterAt(myFilter(), UsernamePasswordAuthenticationFilter.class)
////                .addFilterBefore(corsFilter(), ChannelProcessingFilter.class)
////                .authorizeRequests()
////                .antMatchers("/**").authenticated()
////                .anyRequest().authenticated()
////                .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
////                    @Override
////                    public <O extends FilterSecurityInterceptor> O postProcess(O o) {
////                        o.setSecurityMetadataSource(urlFilterInvocationSecurityMetadataSource);
////                        o.setAccessDecisionManager(urlAccessDecisionManager);
////                        return o;
////                    }
////                })
////                .and()
////                .formLogin()
////                .loginProcessingUrl("/userLogin")
////                .and()
////                .logout()
////                .logoutUrl("/userLogout")
////                //.logoutSuccessUrl("/userLogout")
////                .logoutSuccessHandler(myLogoutHandler)
////                .deleteCookies("JESSIONID")
////                .permitAll()
////                .and()
////                .exceptionHandling()
////                .accessDeniedHandler(urlAccessDeniedHandler)
////                .authenticationEntryPoint(myEntryPoint)
////                .and()
////                .csrf().disable().sessionManagement().maximumSessions(1).expiredUrl("/user/userLogout").sessionRegistry(sessionRegistry());//.expiredUrl("/userLogout")
////               httpSecurity.httpBasic();
//        httpSecurity.authorizeRequests()
//                .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
//                    @Override
//                    public <O extends FilterSecurityInterceptor> O postProcess(O o) {
//                        o.setSecurityMetadataSource(urlFilterInvocationSecurityMetadataSource);
//                        o.setAccessDecisionManager(urlAccessDecisionManager);
//                        return o;
//                    }
//                })
//                .and()
//                .formLogin()
//                .loginProcessingUrl("/userLogin")
//                .failureHandler(failHandler)
//                .successHandler(successHandler)
//                .permitAll()
//                .and()
//                .logout()
//                .logoutUrl("/user/userLogout")
//                .logoutSuccessHandler(myLogoutHandler)
//                .permitAll()
//                .and()
//                .exceptionHandling()
//                .accessDeniedHandler(urlAccessDeniedHandler)
//                .authenticationEntryPoint(myEntryPoint)
////                .accessDeniedHandler(accessHandler)
//                .and().csrf().disable().sessionManagement().maximumSessions(1).expiredUrl("/user/userLogout").sessionRegistry(new SessionRegistryImpl());
//
//
//
//    }
//
//    @Override
//    public void configure(WebSecurity web) throws Exception {
//        //swagger静态资源访问
//        web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources/**", "/configuration/security", "/swagger-ui.html", "/webjars/**","/swagger-resources/configuration/ui","/swagge‌​r-ui.html");
//    }
//
//    @Bean
//    public MyFilter myFilter() throws Exception {
//        MyFilter filter = new MyFilter();
//        filter.setAuthenticationSuccessHandler(successHandler);
//        filter.setAuthenticationFailureHandler(failHandler);
//        filter.setFilterProcessesUrl("/userLogin");
//        filter.setAuthenticationManager(this.authenticationManager());
//        return filter;
//    }
//
//    @Bean
//    public SessionRegistry getSessionRegistry(){
//        SessionRegistry sessionRegistry=new SessionRegistryImpl();
//        return sessionRegistry;
//    }
//
//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.userDetailsService(myUserDetailsServiceImpl);
//       // auth.authenticationProvider(myProvider);
//
//      //  auth.inMemoryAuthentication().withUser("root").password(bCryptPasswordEncoder().encode("root")).roles("admin").authorities("addUser","deleteUser","updateUser","system");
//    }
//
//    @Bean
//    HttpSessionEventPublisher httpSessionEventPublisher() {
//        return new HttpSessionEventPublisher();
//    }
//
//    @Bean
//    public SessionRegistry sessionRegistry(){
//        return new SessionRegistryImpl();
//    }
//
//    @Bean
//    public BCryptPasswordEncoder bCryptPasswordEncoder(){
//        return new BCryptPasswordEncoder();
//    }
//
//    @Bean
//    public CorsFilter corsFilter() {
//        final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
//        final CorsConfiguration corsConfiguration = new CorsConfiguration();
//        corsConfiguration.setAllowCredentials(true);
//        corsConfiguration.addAllowedOrigin("*");
//        corsConfiguration.addAllowedHeader("*");
//        corsConfiguration.addAllowedMethod("*");
//        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
//        return new CorsFilter(urlBasedCorsConfigurationSource);
//    }
//}
