結合網上搜尋的信息、參考各個網站shiro配置,寫了一套適應咱們本身的系統的權限系統框架,作下記錄,畢竟是新學習的一個框架系統,其中還有許多漏洞,也但願各位前輩指正其中的不足之處
第一步:
導入 jar 到pom.xml文件
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.4.0</version>
</dependency>web
第二步:
配置shiro信息到web.xml配置文件中,其實shiro也能夠說是一個Fileter算法
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> spring
第三步:
添加spring-shiro.xml到web.xml文件中默認加載,這個須要注意的是配置在 Spring-mvc.xml 配置以後、否則的話Controller加載時找不到註解注入的Service
spring-shiro.xml文件代碼以下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/goLogin.do"/> <!--登陸頁面 -->
<property name="successUrl" value="/sysIndex.do"/><!--登陸成功頁面,若是本身設置了返回頁面,則不跳轉-->
<property name="unauthorizedUrl" value="/erro.do"/> <!-- 沒有權限跳轉的地址 -->
<property name="filterChainDefinitions">
<value>
/goLogin.do=anon <!--表示均可以訪問-->
/login.do=anon <!--表示均可以訪問-->
/logout.do=anon <!--表示均可以訪問-->
/error=authc
/static/** = anon
/views/include/** = anon
<!--/home=perms[home] perms表示須要該權限才能訪問的頁面-->
<!-- /admin=roles["admin"] roles["admin,user"] 只有擁有admin角色的用戶纔可訪問,同時須要擁有多個角色的話,用引號引發來,中間用逗號隔開-->
<!--/admin=anon-->
/** = authc <!-- authc表示須要認證才能訪問的頁面-->
</value>
</property>
</bean>apache
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="MatcherRealm"/>
</bean>spring-mvc
<!-- 自定義Realm -->
<bean id="MatcherRealm" class="com.cn.zx.filter.MatcherRealm">
<!-- 定義憑證匹配器 -->
<property name="credentialsMatcher" ref="credentialsMatcher"></property>
</bean>
<!-- 憑證匹配器 org.apache.shiro.authc.credential.HashedCredentialsMatcher-->
<bean id="credentialsMatcher" class="com.cn.zx.filter.MatcherPass">
<!-- 密碼加密驗證方式 -->
<property name="hashAlgorithmName" value="MD5"></property>
</bean>
</beans>mvc
第四步:
自定義Realm憑證匹配器,下面是根據我本身的系統需求更改的匹配信息,其中包含兩部分{
第一部分:加入權限和角色信息到SimpleAuthorizationInfo類中
第二部分:加入登陸用戶信息到SimpleAuthenticationInfo類中
}app
/**
* 自定義憑證匹配器
* @author Maochao-zhu
*
*/
public class MatcherRealm extends AuthorizingRealm {
@Resource
UserService userService;
@Resource
RoleService roleService;
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//獲取登陸的用戶信息
User user = (User)principalCollection.getPrimaryPrincipal();
if(user!=null){
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
//獲取用戶角色、和菜單權限
if(null!=user && null!=user.getRoleId()){
Role role = roleService.getRoleById(user.getRoleId());
info.addRole(role.getId().toString());//加入角色
//添加菜單和按鈕權限、
info = addPermission(user, info);
}else{
info.addRole("-1000");//加入角色
info.addStringPermission("-1000"); //加入權限
}
return info;
}
return null;
}框架
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authenticationToken) throws AuthenticationException {
// TODO Auto-generated method stub
UsernamePasswordToken token=(UsernamePasswordToken)authenticationToken;
//經過表單接收的用戶名
String username=token.getUsername();
if(username!=null&&!"".equals(username)){
User user=userService.getUserByLogin(username);
if(user!=null){
return new SimpleAuthenticationInfo(user,user.getPwd(),getName());
}
}
System.out.println("認證失敗");
return null;
}
/**
* 添加權限
* @param username
* @param info
* @return
*/
private SimpleAuthorizationInfo addPermission(User user,SimpleAuthorizationInfo info) {
Role role = roleService.getRoleById(user.getRoleId());
if(null!=role && null!=role.getMenuControl() && !"".equals(role.getMenuControl())){
String []menuArr=role.getMenuControl().split(",");
//獲取菜單和按鈕權限、
for(String m:menuArr){
info.addStringPermission(m);
}
//添加按鈕權限
String []operArr=role.getOperControl().split(",");
for(String m:operArr){
info.addStringPermission(m);
}
if((menuArr ==null ||menuArr.length==0) && (operArr ==null || operArr.length==0)){
info.addStringPermission("-1000");
}
}else{
info.addStringPermission("-1000");
}
return info;
}
}ide
第五步:自定義密碼校驗匹配器或者用默認密碼匹配器
5.1默認密碼驗證匹配器:
<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<!-- 加密算法名稱-->
<property name="hashAlgorithmName" value="MD5"></property>
<!-- 散列次數-->
<property name="hashIterations" value="2"></property>
</bean>
5.2 自定義密碼驗證匹配器:
配置
<bean id="credentialsMatcher" class="com.cn.zx.filter.MatcherPass">
<!-- 密碼加密驗證方式 -->
<property name="hashAlgorithmName" value="MD5"></property>
</bean>
自定義密碼匹配器其實就是重寫了默認密碼匹配器的HashedCredentialsMatcher裏的方法,我這裏是根據咱們的系統密碼驗證規則更改的方法:
@Override
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
String username = (String) token.getPrincipal();
UsernamePasswordToken autoken = (UsernamePasswordToken) token;
String password= String.copyValueOf(autoken.getPassword());
String accountCredentials = String.valueOf(getCredentials(info));
boolean match = Md5Util.isEqualPassWord(password,accountCredentials);
return match;
}
學習