@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注解作用的更多相关文章

  1. Spring学习(13)--- 基于Java类的配置Bean 之 @Configuration & @Bean注解

    基于Java配置选项,可以编写大多数的Spring不用配置XML,但有几个基于Java的注释的帮助下解释.从Spring3.0开始支持使用java代码来代替XML来配置Spring,基于Java配置S ...

  2. SpringBoot(15)—@Conditional注解

    SpringBoot(15)-@Conditional注解 作用 @Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件的才给容器注册Bean. 一.概述 1 ...

  3. SpringBoot学习之@Configuration注解和@Bean注解

    @Configuration 1.@Configuration注解底层是含有@Component ,所以@Configuration 具有和 @Component 的作用. 2.@Configurat ...

  4. SpringBoot自动装配原理之Configuration以及@Bean注解的使用

    Configuration以及Bean注解的使用 该知识点在Spring中应该学过,没有学过或者遗忘的的朋友需要预习或温习前置知识点.SpringBoot其实就是Spring的进一步简化,所以前置知识 ...

  5. Java框架spring Boot学习笔记(七):@Configuration,@bean注解

    @Configuration作用在类上,相当于一个xml文件 @bean作用于方法上,相当于xml配置中的<bean>标签 一个例子: 新建一个Springboot工程 新建一个User类 ...

  6. springboot系列(三) 启动类中关键注解作用解析

    一.Springboot:请求入口 @SpringBootApplication @EnableAspectJAutoProxy @EnableScheduling @EnableTransactio ...

  7. Springboot@Configuration和@Bean详解

    Springboot@Configuration和@Bean详解 一.@Configuration @Target({ElementType.TYPE}) @Retention(RetentionPo ...

  8. [转]Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域

    1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...

  9. Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域

    1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...

随机推荐

  1. IntelliJ IDEA,酷炫插件系列,提高你的工作效率【plugins】

                             今天介绍一下IDEA的一些炫酷的插件,IDEA强大的插件库.                          不仅能给我们带来一些开发的便捷,还能体 ...

  2. python3 win 建立虚拟环境(virtualenv)

    1.安装virtualenv pip3 install virtualenv 2.进入即将创建虚拟环境的目录 cd xxxx 3.创建虚拟环境 py -3 -m venv testxunihua 4. ...

  3. Apache-Tomcat-Ajp漏洞(CVE-2020-1938)漏洞复现(含有poc)

    Apache-Tomcat-Ajp漏洞(CVE-2020-1938)漏洞复现 0X00漏洞简介 Apache与Tomcat都是Apache开源组织开发的用于处理HTTP服务的项目,两者都是免费的,都可 ...

  4. 使用Powershell开机启动隐藏窗口的程序

    1.打开注册表找到键值:计算机\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run 2.添加字符串值:StartJetSer ...

  5. 【sql】sql必知必会_01

    数据: /* Navicat Premium Data Transfer Source Server : localhost_3306 Source Server Type : MySQL Sourc ...

  6. jvm(1):内存结构

    JVM内存结构 JVM内存的运行时数据区: 线程私有(在线程启动时创建) 程序计数器Program Counter Register 一块较小的内存空间,可以看作是当前线程所执行的字节码的行号指示器, ...

  7. MVC5+EF6 入门完整教程1 :从0开始

    https://www.cnblogs.com/miro/p/4030622.html 第0课 从0开始 ASP.NET MVC开发模式和传统的WebForm开发模式相比,增加了很多"约定& ...

  8. rancher版本问题引发的节点注册失败失败

    rancher版本问题引发的节点注册失败失败 待办 https://www.cnblogs.com/Me1onRind/p/11147639.html

  9. 题解【洛谷P5483】[JLOI2011]小A的烦恼

    我们可以灵活运用\(C++\)的语法来解决此题. 解释一下代码中会出现的语法: \(string::iterator\ it\)表示定义了一个\(string\)类型的迭代器\(it\),\(^*it ...

  10. react生命周期方法有哪些?

    react生命周期方法有哪些? React 16.3+ getDerivedStateFromProps:在调用render()之前调用,并在每次渲染时调用.需要使用派生状态的情况是很罕见的 comp ...