Shiro实现的主要原理如图:
使用Spring-boot集成Shiro安全框架主要步骤:
1.建立一个Spring-boot的项目集成Mybatis-plus 和 shiro框架。
2.导入相关依赖。
下面是最主要的shiro依赖!!!!!其他依赖根据项目来导入:eg:spring-web,lombok。
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-web-starter</artifactId>
<version>1.4.1</version>
</dependency>
核心步骤:
3.修改配置类:xxx.config
@Bean public ShiroFilterChainDefinition shiroFilterChainDefinition(){ DefaultShiroFilterChainDefinition sfcd = new DefaultShiroFilterChainDefinition(); //定义某个路径 使用 哪个过滤器来处理 //警告:过滤器定义有顺序 sfcd.addPathDefinition("/","anon"); sfcd.addPathDefinition("/login","anon"); sfcd.addPathDefinition("/login.html","anon"); sfcd.addPathDefinition("/css/**","anon"); sfcd.addPathDefinition("/js/**","anon"); sfcd.addPathDefinition("/images/**","anon"); sfcd.addPathDefinition("/fonts/**","anon"); sfcd.addPathDefinition("/html/**","anon"); //logout 登出 sfcd.addPathDefinition("/logout","logout"); //其他则需要认证 sfcd.addPathDefinition("/**","user"); return sfcd; } 这段代码含义:配置某个路径,使用哪个过滤器来处理。FOR INSTANCE,一个用户登录,如果他没有通过验证(Authentication),他就不能进入下一个模块。 过滤器定义有顺序!!!!
4.在Application.yml 中配置需要如果没有验证需要跳转的进入的网页
shiro: loginUrl: /login.html
5.实现shiroRealm
进行授权验证 :
进行认证验证:
6.测试授权是否正确
7.当pom上存在starter-aop依赖时,shiro的注解会导致springmvc的注解失效。解决方案:
@Bean
public static DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator(){
DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator=new DefaultAdvisorAutoProxyCreator();
/**
* setUsePrefix(false)用于解决一个奇怪的bug。在引入spring aop的情况下。
* 在@Controller注解的类的方法中加入@RequiresRole等shiro注解,会导致该方法无法映射请求,导致返回404。
* 加入这项配置能解决这个bug
*/
defaultAdvisorAutoProxyCreator.setUsePrefix(true);
return defaultAdvisorAutoProxyCreator;
}
在任意一个config中加入这样一个bean!!
ps:以下是有一些sql联表查询的query语句: