ink.huaxun.gateway.config.ShiroConfig Maven / Gradle / Ivy
package ink.huaxun.gateway.config;
import ink.huaxun.gateway.filter.CNInvalidRequestFilter;
import ink.huaxun.gateway.filter.JwtFilter;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* shiro配置(仅使用shiro的过滤器)
*
* @author zhaogang
* @date 2020-05-26 11:50
*/
@Configuration
public class ShiroConfig {
/**
* token的头
*/
@Value("${token.header}")
private String header;
@Bean
public ShiroFilterFactoryBean shiroFilter() {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(new DefaultWebSecurityManager());
// 过滤器
Map filters = new LinkedHashMap<>();
// 无状态过滤器
filters.put("jwt", new JwtFilter(header));
CNInvalidRequestFilter invalidRequestFilter = new CNInvalidRequestFilter();
filters.put("invalid", invalidRequestFilter);
shiroFilterFactoryBean.setFilters(filters);
ArrayList list = new ArrayList<>();
list.add("invalid");
shiroFilterFactoryBean.setGlobalFilters(list);
// 有序配置,对配置顺序有要求
Map filterChainDefinitionMap = new LinkedHashMap<>();
// anon不参与过滤器,jwt自定义过滤器
filterChainDefinitionMap.put("/login", "anon");
filterChainDefinitionMap.put("/phoneNumber", "anon");
filterChainDefinitionMap.put("/getOpenid/**", "anon");
filterChainDefinitionMap.put("/aliyun/**", "anon");
filterChainDefinitionMap.put("/captcha", "anon");
filterChainDefinitionMap.put("/mobileCaptcha", "anon");
filterChainDefinitionMap.put("/appApi/message/**", "jwt");
filterChainDefinitionMap.put("/appApi/**", "anon");
filterChainDefinitionMap.put("/log/**", "anon");
filterChainDefinitionMap.put("/image/**", "anon");
filterChainDefinitionMap.put("/code/**", "anon");
filterChainDefinitionMap.put("/druid/**", "anon");
filterChainDefinitionMap.put("/actuator/**", "anon");
filterChainDefinitionMap.put("/elasticsearch/**", "anon");
filterChainDefinitionMap.put("/alipay/**", "anon");
filterChainDefinitionMap.put("/excel/**", "anon");
filterChainDefinitionMap.put("/websocket/**", "anon");
filterChainDefinitionMap.put("/**", "jwt");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}
}