Spring Boot 整合 Shiro+Thymeleaf过程解析
这篇文章主要介绍了SpringBoot整合Shiro+Thymeleaf过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.导包
org.apache.shiro shiro-spring 1.4.1 com.github.theborakompanioni thymeleaf-extras-shiro 2.0.0 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-configuration-processor true org.projectlombok lombok true
2.编写配置类
@Configuration @ConfigurationProperties(prefix="shiro") @Data publicclassShiroConfig{ privateStringloginUrl; privateStringunauthorizedUrl; privateStringsuccessUrl; privateStringlogoutUrl; privateString[]anons; privateString[]authcs; /** *配置securityManager *@paramuserRealm *@return */ @Bean publicSecurityManagersecurityManager(UserRealmuserRealm){ DefaultWebSecurityManagersecurityManager=newDefaultWebSecurityManager(); securityManager.setRealm(userRealm); returnsecurityManager; } /** *配置shiroFilter *@paramsecurityManager *@return */ @Bean publicShiroFilterFactoryBeanshiroFilterFactoryBean(SecurityManagersecurityManager){ ShiroFilterFactoryBeanshiroFilterFactoryBean=newShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); shiroFilterFactoryBean.setLoginUrl(loginUrl); shiroFilterFactoryBean.setUnauthorizedUrl(unauthorizedUrl); shiroFilterFactoryBean.setSuccessUrl(successUrl); MapfilterMap=newHashMap<>(); if(null!=logoutUrl){ filterMap.put(loginUrl,"logout"); } if(anons!=null&&anons.length>0){ for(Stringanon:anons){ filterMap.put(anon,"anon"); } } if(authcs!=null&&authcs.length>0){ for(Stringauthc:authcs){ filterMap.put(authc,"authc"); } } shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap); returnshiroFilterFactoryBean; } /** *配置自定义Realm *@return */ @Bean publicUserRealmuserRealm(CredentialsMatchercredentialsMatcher){ UserRealmuserRealm=newUserRealm(); userRealm.setCredentialsMatcher(credentialsMatcher); returnuserRealm; } /** *配置凭证匹配器 *@return */ @Bean publicHashedCredentialsMatcherhashedCredentialsMatcher(){ HashedCredentialsMatcherhashedCredentialsMatcher=newHashedCredentialsMatcher(); hashedCredentialsMatcher.setHashAlgorithmName("MD5"); hashedCredentialsMatcher.setHashIterations(10); returnhashedCredentialsMatcher; } /** *配置ShiroDialect,用于Thymeleaf和shiro标签的使用 *@return */ @Bean publicShiroDialectshiroDialect(){ returnnewShiroDialect(); } }
3.application.yml配置拦截链
#shiro shiro: login-url:/login.html anons: -/login.html -/index.html -doLogin authcs: -/**
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。