public class SpringShiroConfig {
@Bean
public EhCacheManager ehCacheManager() {
EhCacheManager ehCacheManager = new EhCacheManager();
ehCacheManager.setCacheManagerConfigFile("classpath:ehcache.xml");
return ehCacheManager;
}
// @Bean(name = "passwordService")
// public PasswordService passwordService() {
// DefaultPasswordService passwordService = new DefaultPasswordService();
// return passwordService;
// }
@Bean(name = "jdbcUserRealm")
public AuthorizingRealm jdbcUserRealm() {
AuthorizingRealm realm = new JdbcUserRealm();
HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
credentialsMatcher.setHashAlgorithmName("md5");
credentialsMatcher.setHashIterations(2);
credentialsMatcher.setStoredCredentialsHexEncoded(true);
realm.setCredentialsMatcher(credentialsMatcher);
return realm;
}
@Bean(name = "securityManager")
public DefaultWebSecurityManager securityManager(@Qualifier("jdbcUserRealm") Realm jdbcUserRealm) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(jdbcUserRealm);
return securityManager;
}
@Bean(name = "formAuthenticationFilter")
public FormAuthenticationFilter formAuthenticationFilter() {
FormAuthenticationFilter filter = new FormAuthenticationFilter();
filter.setLoginUrl("/admin/login");
return filter;
}
@Bean(name = "shiroFilter")
public ShiroFilterFactoryBean shiroFilterFactoryBean(
@Qualifier("securityManager")SecurityManager securityManager,
@Qualifier("formAuthenticationFilter")FormAuthenticationFilter filter,
@Qualifier("permissionMapper")PermissionMapper permissionMapper) {
//ShiroFilterFactoryBean
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setLoginUrl("/admin/login");
shiroFilterFactoryBean.setSuccessUrl("/admin/main");
shiroFilterFactoryBean.setUnauthorizedUrl("/admin/unauthorized");
shiroFilterFactoryBean.setSecurityManager(securityManager);
// shiroFilterFactoryBean.setFilterChainDefinitions(
// "/admin/login = anon\n" +
// "/admin/logout = logout\n" +
// "/** = user\n" +
// "/resources/** = anon\n"
// );
Map<String, String> filterChainDefinitionMap = new HashMap<>();
filterChainDefinitionMap.put("/**", "user");
filterChainDefinitionMap.put("/resources/**", "anon");
filterChainDefinitionMap.put("/admin/login", "anon");
filterChainDefinitionMap.put("/admin/logout", "logout");
//database url permissions
List<Permission> permissionList = permissionMapper.listAll();
if(permissionList != null) {
permissionList.stream().filter(perm -> StringUtils.isNotBlank(perm.getUrl())).forEach(perm -> {
//"authc, roles[admin,user], perms[file:edit]"
String perms = perm.getPerms();
String permissionChain = genChainDefinitionString(perms, "perms");
String roles = perm.getRoles();
String roleChain = genChainDefinitionString(roles, "roles");
StringBuilder chainMerge = new StringBuilder();
boolean hasPerms = false;
if(StringUtils.isNotBlank(permissionChain)) {
chainMerge.append(permissionChain);
hasPerms = true;
}
if(StringUtils.isNotBlank(roleChain)) {
if(hasPerms) {
chainMerge.append(",");
}
chainMerge.append(roleChain);
hasPerms = true;
}
if(hasPerms) {
filterChainDefinitionMap.put(perm.getUrl(), chainMerge.toString());
}
});
}
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
//filters
Map<String, Filter> filters = new HashMap<>();
filters.put("authc", filter);
shiroFilterFactoryBean.setFilters(filters);
return shiroFilterFactoryBean;
}
/**
*
* @param permissionStr Splits by the comma-delimited
* @param filterName {@link org.apache.shiro.web.filter.mgt.DefaultFilter}
* @return
*/
private String genChainDefinitionString(String permissionStr, String filterName) {
StringBuilder chainDefinitionBuilder = new StringBuilder();
if(StringUtils.isNotBlank(permissionStr)) {
boolean isFirst = true;
for(String perm : permissionStr.split(",")) {
if(isFirst) {
isFirst = false;
} else {
chainDefinitionBuilder.append(",");
}
chainDefinitionBuilder.append(filterName).append("[").append(perm).append("]");
}
}
return chainDefinitionBuilder.toString();
}
}
java配置非常的简单,如果用xml配置还要继承DefaultFilterChainManager,配置起来有点绕(因为xml没办法拿到数据中的权限配置)
xml配置可以参考http://jinnianshilongnian.iteye.com/blog/2040929