Springboot @Configuration @bean注解作用解析
這篇文章主要介紹了springboot @Configuration @bean注解作用解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
@Configuration注解可以達(dá)到在Spring中使用xml配置文件的作用
@Bean就等同于xml配置文件中的<bean>
在spring項(xiàng)目中我們集成第三方的框架如shiro會在spring.xml配置文件中進(jìn)行配置,例如:
<!-- 配置shiro框架提供過濾器工廠 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 注入shiro核心組件安全管理器 -->
<property name="securityManager" ref="securityManager"></property>
<!-- 注入相關(guān)頁面 -->
<property name="loginUrl" value="/login.jsp"></property>
<property name="unauthorizedUrl" value="/unauthorized.jsp"></property>
<!-- 配置過濾器鏈:配置項(xiàng)目發(fā)出url對應(yīng)攔截規(guī)則:指定什么url要求具有什么樣權(quán)限 -->
<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">
<!-- 強(qiáng)制使用cglib代理 -->
<property name="proxyTargetClass" value="true"></property>
</bean>
<!-- 配置切面 目的驗(yàn)權(quán),判斷當(dāng)前用戶是否有權(quán)限調(diào)用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注解可以達(dá)到在Spring中使用xml配置文件的作用。
@Bean就等同于xml配置文件中的<bean>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot項(xiàng)目編譯提示無效的源發(fā)行版17解決辦法
這篇文章主要給大家介紹了關(guān)于springboot項(xiàng)目編譯提示無效的源發(fā)行版17解決辦法,這個錯誤意味著你的Spring Boot項(xiàng)目正在使用Java 17這個版本,但是你的項(xiàng)目中未配置正確的Java版本,需要的朋友可以參考下2023-06-06
Java?GenericObjectPool?對象池化技術(shù)之SpringBoot?sftp?連接池工具類詳解
這篇文章主要介紹了Java?GenericObjectPool?對象池化技術(shù)之SpringBoot?sftp?連接池工具類詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
java 二維數(shù)組矩陣乘法的實(shí)現(xiàn)方法
java 二維數(shù)組矩陣乘法的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-03-03

