public class MyShiroFilterFactoryBean extends ShiroFilterFactoryBean{
private static final String ROLE_STRING = "roles[${0}]";
private String fiterChainDefinitions;
@Override
public void setFilterChainDefinitions(String definitions) {
// TODO Auto-generated method stub
fiterChainDefinitions = definitions;
Ini ini = new Ini();
ini.load(definitions);
//did they explicitly state a 'urls' section? Not necessary, but just in case:
Ini.Section section = ini.getSection(IniFilterChainResolverFactory.URLS);
if (CollectionUtils.isEmpty(section)) {
//no urls section. Since this _is_ a urls chain definition property, just assume the
//default section contains only the definitions:
section = ini.getSection(Ini.DEFAULT_SECTION_NAME);
}
Map<String,String[]> permsMap = new HashMap<>();
permsMap.put("/dotest1", new String[]{"test"});
permsMap.put("/dotest2", new String[]{"test","guest"});
// permsMap.put("/dotest3", new String[]{"admin"});
for(String url:permsMap.keySet()){
String[] roles = permsMap.get(url);
StringBuilder sb = new StringBuilder();
for(String role:roles){
sb.append(role).append(",");
}
String str = sb.substring(0,sb.length()-1);
section.put(url, MessageFormat.format(ROLE_STRING, str));
System.out.println(url);
}
this.setFilterChainDefinitionMap(section);
}
public void update(){
synchronized (this) {
try{
AbstractShiroFilter filter = (AbstractShiroFilter) this.getObject();
PathMatchingFilterChainResolver resolver = (PathMatchingFilterChainResolver) filter.getFilterChainResolver();
DefaultFilterChainManager manager = (DefaultFilterChainManager) resolver.getFilterChainManager();
manager.getFilterChains().clear();
this.getFilterChainDefinitionMap().clear();
// this.setFilterChainDefinitions("/dotest3.html=authc,roles[admin]");
this.setFilterChainDefinitions(fiterChainDefinitions);
Map<String, String> chains = getFilterChainDefinitionMap();
if (!CollectionUtils.isEmpty(chains)) {
for (Map.Entry<String, String> entry : chains.entrySet()) {
String url = entry.getKey();
String chainDefinition = entry.getValue();
manager.createChain(url, chainDefinition);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}
需要重新实现ShiroFilterFactoryBean