SpringBoot 03 Shiro
shiro是apache的一个开源框架,是一个权限管理的框架,实现认证、授权、加密、会话管理。
shiro优势举例
- 易用:相当于其他安全框架,shiro比较简单易用。
- 使用非常广泛,资料好找。
- 灵活:可以工作在很多环境 。
- web支持:对web的支持好, 如thymeleaf标签支持。
- 支持:应用广泛,是Apache软件基金会成员(有维护更新保证)
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-starter</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
</dependency>
二、realm
package com.realm;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.springframework.stereotype.Component;
//realm连数据库
//认证
@Component("myRealm")
public class MyRealm extends AuthenticatingRealm {
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
return null;
}
}
三、shiro相关配置
package com;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.realm.MyRealm;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import java.util.LinkedHashMap;
import java.util.Map;
@SpringBootApplication
@MapperScan("com.mapper")
public class SpringBoot02Application {
public static void main(String[] args) {
ApplicationContext ac = SpringApplication.run(SpringBoot02Application.class, args);
System.out.println("访问:http://localhost:8080");
}
/*添加分页插件*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
/* shiro相关配置*/
@Bean //密码匹配器
public SimpleCredentialsMatcher simpleCredentialsMatcher(){//简单的匹配规则,没有加密配置
SimpleCredentialsMatcher simpleCredentialsMatcher =new SimpleCredentialsMatcher();
return simpleCredentialsMatcher;
}
//会话管理器
@Bean
public DefaultWebSessionManager defaultWebSessionManager(){
DefaultWebSessionManager defaultWebSessionManager = new DefaultWebSessionManager();
return defaultWebSessionManager;
}
@Bean // 安全管理器
public DefaultWebSecurityManager defaultWebSecurityManager(SimpleCredentialsMatcher simpleCredentialsMatcher,
MyRealm realm, DefaultWebSessionManager defaultWebSessionManager) {
DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
realm.setCredentialsMatcher(simpleCredentialsMatcher);
defaultWebSecurityManager.setRealm(realm);
defaultWebSecurityManager.setSessionManager(defaultWebSessionManager);
return defaultWebSecurityManager;
}
//过滤法则
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
Map<String,String> filterChainDefinitionMap = new LinkedHashMap<>(); //有顺序 不用无序的HashMap
//放行
//静态资源放行
filterChainDefinitionMap.put("/favicon.ico","anon");
filterChainDefinitionMap.put("/css/**","anon");
filterChainDefinitionMap.put("/js/**","anon");
filterChainDefinitionMap.put("/img/**","anon");
//动态资源放行
filterChainDefinitionMap.put("/","anon");
filterChainDefinitionMap.put("/a","anon");
//不放行
filterChainDefinitionMap.put("/**","authc");
//未认证跳往地址
shiroFilterFactoryBean.setLoginUrl("/");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}
}