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")
@Data
public 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 配置 拦截链

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

最新文章

  1. div仿textarea
  2. spring中context:property-placeholder/元素
  3. CodeForces - 427A (警察和罪犯 思维题)
  4. jsonObject jsonarray
  5. [Java解惑]类
  6. Python抓取页面中超链接(URL)的三中方法比较(HTMLParser、pyquery、正则表达式) &lt;转&gt;
  7. Android程序的隐藏与退出
  8. android开发步步为营之65:解决ScrollView和ListView触摸事件onInterceptTouchEvent相互冲突问题
  9. pl sql练习(1)
  10. 树莓派3 B+ 的摄像头简单使用(video-streamer)
  11. CF 208E. Blood Cousins [dsu on tree 倍增]
  12. python 单例模式获取IP代理
  13. app后端设计(7)-- 项目管理
  14. 模拟开户接口,使用shell脚本实现批量用户开通
  15. 搞懂MapReduce
  16. lambda表达式(c++11)
  17. Linux&#160;CentOS&#160;6.5&#160;下&#160;vsftpd&#160;ftp服务器搭建
  18. Android AsyncTask 源代码分析
  19. 20165207 学习基础与C语言基础调查反馈
  20. struts2--文件上传类型3

热门文章

  1. A. Srdce and Triangle--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)
  2. XML 介绍
  3. jQuery 加载事件
  4. vue-router 动态路由
  5. Javascript 面向对象之继承
  6. exe自启动的几种方式
  7. git入门基本命令
  8. mysql动态列--统计报表信息对比
  9. 不走弯路,微信小程序的快速入门?
  10. String.join() --Java8中String类新增方法