spring-shiro.xml文件css
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"
default-lazy-init="true">java
<description>Shiro安全配置(SpringMVC整合Shiro,Shiro是一個強大易用的Java安全框架,提供了認證、受權、加密和會話管理等功能)</description>jquery
<!-- Shiro默認會使用Servlet容器的Session,可經過sessionMode屬性來指定使用Shiro原生Session -->
<!-- 即<property name="sessionMode" value="native"/>,詳細說明見官方文檔 -->
<!-- 這裏主要是設置自定義的單jdbcRealm應用,如有多個Realm,可以使用'realms'屬性代替 -->
<!-- Shiro's main business-tier object for web-enabled applications -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="jdbcRealm" />
<property name="cacheManager" ref="shiroEhcacheManager" />
</bean>
<!-- 繼承自AuthorizingRealm的自定義Realm,即指定Shiro驗證用戶登陸的類爲自定義的jdbcRealm.java -->
<!-- 自定義的Realm -->
<bean id="jdbcRealm" class="cn.**.JdbcRealm">
<property name="authorizationCachingEnabled" value="true" />
<property name="cacheManager" ref="shiroEhcacheManager" />
<!--<property name="credentialsMatcher" ref="hashedCredentialsMatcher" />-->
</bean>
<!-- 密碼保存方式 -->
<bean id="hashedCredentialsMatcher"
class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="MD5" />
<property name="storedCredentialsHexEncoded" value="true" />
<property name="hashIterations" value="1" />
</bean>
<!-- 用戶受權信息Cache, 採用EhCache -->
<bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:spring/ehcache-shiro.xml" />
</bean>
<!-- Shiro Filter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<!-- 未受權時要跳轉的鏈接 -->
<property name="unauthorizedUrl" value="/sys/turn403" />
<property name="filterChainDefinitions">
<value>
/login = authc
/logout = logout
/js/** = anon
/css/** = anon
/img/** = anon
/easyui/** = anon
/jquery/** = anon
/jquery-jbox/** = anon
/jquery-ztree/** = anon
/treeTable/** = anon
/user/main = authc
</value>
</property>
</bean>web
<!-- 保證明現了Shiro內部lifecycle函數的bean執行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!-- AOP式方法級權限檢查 spring aop 支持shiro的註解功能 -->
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true" />
</bean>
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>
</beans>spring
參考 jdbcRealm.java數據庫
public class jdbcRealm extends AuthorizingRealm {
@Override
public void setName(String name) {
super.setName("customRealm");
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException {apache
}安全
// 用於受權
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
String userCode = (String) principals.getPrimaryPrincipal();
//模擬從數據庫獲取到數據
List permissions = new ArrayList();
permissions.add("user:create");//用戶的建立
permissions.add("items:add");//商品添加權限
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
simpleAuthorizationInfo.addStringPermissions(permissions);session
return simpleAuthorizationInfo;
}app
}