package com.example.myshiro.config;

import com.example.myshiro.realm.MyRealm;
import com.example.myshiro.realm.MyRealm2;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authc.pam.AllSuccessfulStrategy;
import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy;
import org.apache.shiro.authc.pam.FirstSuccessfulStrategy;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class ShiroConfig {
//    @Bean
//    public DefaultWebSecurityManager securityManager(JdbcRealm jdbcRealm){
//        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        securityManager.setRealm(jdbcRealm);
//        return securityManager;
//    }
//    @Bean
//    public JdbcRealm jdbcRealm(DataSource dataSource){
//        JdbcRealm jdbcRealm = new JdbcRealm();
//        jdbcRealm.setDataSource(dataSource);
//        return jdbcRealm;
//    }

    // Realm管理者
    @Bean
    public ModularRealmAuthenticator modularRealmAuthenticator(){
        ModularRealmAuthenticator modularRealmAuthenticator = new ModularRealmAuthenticator();
        //设置认证策略1.只要有一个Realm验证成功即可，返回所有成功的认证信息
//        modularRealmAuthenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());
        //设置认证策略2.只要有一个Realm验证成功即可，只返回第一个成功的认证信息，其他的忽略
//        modularRealmAuthenticator.setAuthenticationStrategy(new FirstSuccessfulStrategy());
        //设置认证策略3.所有Realm验证成功才算成功，如果有一个失败则认证失败
        modularRealmAuthenticator.setAuthenticationStrategy(new AllSuccessfulStrategy());
        return modularRealmAuthenticator;
    }

//配置了自定义Realm之后在shiro配置类里也需要做改变
    @Bean
    public DefaultWebSecurityManager securityManager(MyRealm myRealm){ //,MyRealm2 myRealm2
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
         // 设置Realm管理者（需要在设置Realm之前）
        securityManager.setAuthenticator(modularRealmAuthenticator());
        //        securityManager.setRealm(myRealm);
//        List<Realm> realms=new ArrayList<>();
        // Realm放入SecurityManager中
//        realms.add(myRealm);
//        realms.add(myRealm2);
//        securityManager.setRealms(realms);
        securityManager.setRealm(myRealm);
        return securityManager;
    }
    // 配置加密算法
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //加密算法
//        hashedCredentialsMatcher.setHashAlgorithmName("Md5Hash");
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        //加密的次数
        hashedCredentialsMatcher.setHashIterations(5);
        return hashedCredentialsMatcher;
    }


    // Realm
    @Bean
    public MyRealm MyRealm(HashedCredentialsMatcher hashedCredentialsMatcher) {
        MyRealm myRealm = new MyRealm();
        // 设置加密算法
        myRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return myRealm;
    }
//
//    @Bean
//    public MyRealm myRealm(){
//        return new MyRealm();
//    }
    @Bean
    public MyRealm2 myRealm2(){
        return new MyRealm2();
    }
}
