Spring Boot 整合 Shiro+Thymeleaf过程解析

时间:2021-05-20

这篇文章主要介绍了Spring Boot 整合 Shiro+Thymeleaf过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.导包

<!-- springboot 与 shiro 的集成--><dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.1</version></dependency><!-- thymeleaf 与 shiro 集成--><dependency> <groupId>com.github.theborakompanioni</groupId> <artifactId>thymeleaf-extras-shiro</artifactId> <version>2.0.0</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional></dependency><dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional></dependency>

2. 编写配置类

@Configuration@ConfigurationProperties(prefix = "shiro")@Datapublic class ShiroConfig { private String loginUrl; private String unauthorizedUrl; private String successUrl; private String logoutUrl; private String[] anons; private String[] authcs; /** * 配置securityManager * @param userRealm * @return */ @Bean public SecurityManager securityManager(UserRealm userRealm){ DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(userRealm); return securityManager; } /** * 配置shiroFilter * @param securityManager * @return */ @Bean public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){ ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); shiroFilterFactoryBean.setLoginUrl(loginUrl); shiroFilterFactoryBean.setUnauthorizedUrl(unauthorizedUrl); shiroFilterFactoryBean.setSuccessUrl(successUrl); Map<String,String> filterMap = new HashMap<>(); if(null != logoutUrl){ filterMap.put(loginUrl,"logout"); } if(anons!=null && anons.length>0){ for(String anon:anons){ filterMap.put(anon,"anon"); } } if(authcs!=null && authcs.length>0){ for(String authc:authcs){ filterMap.put(authc,"authc"); } } shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap); return shiroFilterFactoryBean; } /** * 配置自定义Realm * @return */ @Bean public UserRealm userRealm(CredentialsMatcher credentialsMatcher){ UserRealm userRealm = new UserRealm(); userRealm.setCredentialsMatcher(credentialsMatcher); return userRealm; } /** * 配置凭证匹配器 * @return */ @Bean public HashedCredentialsMatcher hashedCredentialsMatcher(){ HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); hashedCredentialsMatcher.setHashAlgorithmName("MD5"); hashedCredentialsMatcher.setHashIterations(10); return hashedCredentialsMatcher; } /** * 配置ShiroDialect,用于Thymeleaf和shiro标签的使用 * @return */ @Bean public ShiroDialect shiroDialect(){ return new ShiroDialect(); }}

3. application.yml 配置 拦截链

# shiroshiro: login-url: /login.html anons: - /login.html - /index.html - doLogin authcs: - /**

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章