springboot @Configuration @bean注解作用

@Configuration注解可以达到在Spring中使用xml配置文件的作用

@Bean就等同于xml配置文件中的

 

在spring项目中我们集成第三方的框架如shiro会在spring.xml配置文件中进行配置,例如:


    "shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        
        "securityManager" ref="securityManager">
        
        "loginUrl" value="/login.jsp">
        "unauthorizedUrl" value="/unauthorized.jsp">
        
        "filterChainDefinitions">
            
                /css/**=anon
                /js/**=anon
                /validatecode.jsp*=anon
                /images/**=anon
                /login.jsp=anon
                /service/**=anon
                /**=authc
            
        
    
   
    
         
         
         
    

    
    
         
        
     

    
    
        
        
    
    
    

 

在springboot与shiro整合:

@Configuration
public class ShiroConfig {
    @Bean
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        Map filterChainDefinitionMap = new HashMap();
        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthc");
        shiroFilterFactoryBean.setSuccessUrl("/home/index");
        
        filterChainDefinitionMap.put("/*", "anon");
        filterChainDefinitionMap.put("/authc/index", "authc");
        return shiroFilterFactoryBean;
    }

    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashAlgorithmName(PasswordHelper.ALGORITHM_NAME); 
        hashedCredentialsMatcher.setHashIterations(PasswordHelper.HASH_ITERATIONS); 
        return hashedCredentialsMatcher;
    }

    @Bean
    public EnceladusShiroRealm shiroRealm() {
        EnceladusShiroRealm shiroRealm = new EnceladusShiroRealm();
        shiroRealm.setCredentialsMatcher(hashedCredentialsMatcher()); 
        return shiroRealm;
    }

    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(shiroRealm());
        return securityManager;
    }

    @Bean
    public PasswordHelper passwordHelper() {
        return new PasswordHelper();
    }
}

 

@Configuration注解可以达到在Spring中使用xml配置文件的作用。

@Bean就等同于xml配置文件中的

你可能感兴趣的:(springboot @Configuration @bean注解作用)