Springboot @Configuration @bean注解作用解析

网友投稿 388 2022-12-14


Springboot @Configuration @bean注解作用解析

这篇文章主要介绍了springboot @Configuration @bean注解作用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

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

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

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

/css/**=anon

/js/**=anon

/validatecode.jsp*=anon

/images/**=anon

/login.jsp=anon

/service/**=anon

/**=authc

在springboot与shiro整合:

@Configuration

public class ShiroConfig {

@Bean

public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {

ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

shiroFilterFactoryBean.setSecurityManager(securityManager);

Map filterChainDefinitionMap = new HashMap();

shiroFilterFactoryBean.setLoginUrl("/login");

shiroFilterFactoryBean.setUnauthorizedUrl("/unauthc");

shiroFilterFactoryBean.setSuccessUrl("/home/index");

filterChainDefinitionMap.put("/*", "anon");

filterChainDefinitionMap.put("/authc/index", "authc");

return sjgLtthiroFilterFactoryBean;

}

@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配置文件中的


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Logger.error打印错误异常的详细堆栈信息
下一篇:Java Arrays.sort和Collections.sort排序实现原理解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~