温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Springboot中@Configuration和@bean注解怎么用

发布时间:2022-04-06 15:54:53 来源:亿速云 阅读:266 作者:iii 栏目:移动开发

本篇内容主要讲解“Springboot中@Configuration和@bean注解怎么用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Springboot中@Configuration和@bean注解怎么用”吧!

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

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

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

<!-- 配置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>

在springboot与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();
  }
}

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

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

到此,相信大家对“Springboot中@Configuration和@bean注解怎么用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI