Springboot@Configuration@bean注解作⽤解析
这篇⽂章主要介绍了springboot @Configuration @bean注解作⽤解析,⽂中通过⽰例代码介绍的⾮常详细,对⼤家的学习或者⼯作具有⼀定的参考学习价值,需要的朋友可以参考下
@Configuration注解可以达到在Spring中使⽤xml配置⽂件的作⽤
@Bean就等同于xml配置⽂件中的<bean>
在spring项⽬中我们集成第三⽅的框架如shiro会在l配置⽂件中进⾏配置,例如:
<!-- 配置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.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");springboot aop
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>
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论