Maison > Java > javaDidacticiel > le corps du texte

Comment utiliser les annotations @Configuration et @bean dans Springboot

PHPz
Libérer: 2023-05-12 14:46:13
avant
870 Les gens l'ont consulté

@L'annotation de configuration peut obtenir l'effet d'utiliser des fichiers de configuration XML dans Spring

@Bean est équivalent au dans le fichier de configuration XML

Dans le projet Spring, nous intégrons des frameworks tiers tels que Shiro et il sera utilisé au printemps Configurez dans le fichier de configuration .xml, par exemple :

<!-- 配置shiro框架提供过滤器工厂 -->
  <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <!-- 注入shiro核心组件安全管理器 -->
    <property name="securityManager" ref="securityManager"></property>
    <!-- 注入相关页面 -->
    <property name="loginUrl" value="/login.jsp"></property>
    <property name="unauthorizedUrl" value="/unauthorized.jsp"></property>
    <!-- 配置过滤器链:配置项目发出url对应拦截规则:指定什么url要求具有什么样权限 -->
    <property name="filterChainDefinitions">
      <value>
        /css/**=anon
        /js/**=anon
        /validatecode.jsp*=anon
        /images/**=anon
        /login.jsp=anon
        /service/**=anon
        /**=authc
      </value>
    </property>
  </bean>
  <!-- 配置安全管理器 -->
  <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
     <property name="realms" ref="bosRealm"></property>
     <!-- 使用缓存 -->
     <property name="cacheManager" ref="cacheManager"></property>
  </bean>

  <!-- 配置缓存管理器-->
  <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
     <!-- 加载ehcache的配置文件,指定缓存策略 -->
    <property name="cacheManager" ref="ehcacheManager"></property>
  </bean> 

  <!-- 开启shiro注解支持 -->
  <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
    <!-- 强制使用cglib代理 -->
    <property name="proxyTargetClass" value="true"></property>
  </bean>
  <!-- 配置切面 目的验权,判断当前用户是否有权限调用service层方法 -->
  <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"></bean>
Copier après la connexion

Intégrez springboot avec shiro :

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

    Map<String, String> filterChainDefinitionMap = new HashMap<String, String>();
    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();
  }
}
Copier après la connexion

@L'annotation de configuration peut obtenir l'effet d'utiliser des fichiers de configuration XML au printemps.

@Bean est équivalent à

dans le fichier de configuration XML

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:yisu.com
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!