@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. 记录 shell学习过程(2) read的用法

    echo -n "login:"read username  #read后面直接使用一个变量用于接收输入的数据 echo -n "password:"read ...

  2. python os 模块详解

    os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix'. os.getcwd:得 ...

  3. Laravel中如何做数据库迁移

    总的来说,做一次独立数据库迁移只需要三步,分别是创建迁移文件.修改迁移文件.运行迁移 1.创建数据库迁移文件php artisan make:migration create_articles_tab ...

  4. CentOS7.5升级OpenSSH

    实验环境 OS:CentOS 7.5 当前openssh版本:OpenSSH_7.4p1 升级后的openssh版本:OpenSSH_8.0p1 开通telnet 为了防止升级过程中ssh断连,保险起 ...

  5. 03-Java基础语法【 流程控制语句】

    重要知识记录: 1.流程控制 顺序结构:根据编写的顺序,从上到下进行运行. 2.判断语句 1)判断语句1--if if(判断条件){ 执行语句: } 2)判断语句2--if...else if(判断条 ...

  6. Vue中父组件向子组件echarts传值问题

    原文链接:https://blog.csdn.net/Uookic/article/details/80638883?utm_source=copy 问题:当父组件传值给子组件echarts时,发现子 ...

  7. analog filter

    理想的filter如下: 但是实际的filter如下: 在实际应用中,我们更多的是用Fo和Q这两个parameter来design analog filter. Low-Pass Filter tra ...

  8. 【C语言】用指针作为形参完成数据的升序排列

    #include<stdio.h> void sort(int *x,int n); int main() { ] = { ,,,,,,,,, },i; sort(arr, ); prin ...

  9. JavaWeb项目忘记添加依赖

    有的时候我们建项目的时候忘记添加项目的依赖了,这里示范一个提示错误,就是 The superclass "javax.servlet.http.HttpServlet" was n ...

  10. 【NS-3学习】ns3-模拟基础:关键概念,日志,命令行参数

    前言 本篇博客先介绍在仿真过程中会使用到的一些关键概念,然后介绍便于调试仿真脚本的常用技术:日志.命令行参数. 关键概念 节点 在因特网术语中,主机(终端)是指任何一台连接到网络的计算设备.ns-3并 ...