您好,登錄后才能下訂單哦!
這篇文章主要介紹Spring如何整合Shiro做權限控制模塊,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
<!-- Spring 整合Shiro需要的依賴 --> <<</<</<</</<<</<</<</</<<</<</<</</<<</<</<</</<!-- 除此之外還有一些東西也不可少spring, spring-mvc, ibatis等 spring.3.1.2 spring-mvc.3.1.2 ibatis.2.3.4 cglib.2.2 -->
<!-- 配置shiro的核心攔截器 --> <<</<</</<<</<</</ 3. 編寫自己的UserRealm類繼承自Realm,主要實現認證和授權的管理操作package com.jay.demo.shiro;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import org.apache.shiro.authc.AuthenticationException;import org.apache.shiro.authc.AuthenticationInfo;import org.apache.shiro.authc.AuthenticationToken;import org.apache.shiro.authc.LockedAccountException;import org.apache.shiro.authc.SimpleAuthenticationInfo;import org.apache.shiro.authc.UnknownAccountException;import org.apache.shiro.authz.AuthorizationInfo;import org.apache.shiro.authz.SimpleAuthorizationInfo;import org.apache.shiro.realm.AuthorizingRealm;import org.apache.shiro.subject.PrincipalCollection;import org.springframework.beans.factory.annotation.Autowired;import com.jay.demo.bean.Permission;import com.jay.demo.bean.Role;import com.jay.demo.bean.User;import com.jay.demo.service.UserService; public class UserRealm extends AuthorizingRealm{ @Autowired private UserService userService; /** * 授權操作 */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {// String username = (String) getAvailablePrincipal(principals); String username = (String) principals.getPrimaryPrincipal(); Set<Role> roleSet = userService.findUserByUsername(username).getRoleSet(); //角色名的集合 Set<String> roles = new HashSet<String>(); //權限名的集合 Set<String> permissions = new HashSet<String>(); Iterator<Role> it = roleSet.iterator(); while(it.hasNext()){ roles.add(it.next().getName()); for(Permission per:it.next().getPermissionSet()){ permissions.add(per.getName()); } } SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); authorizationInfo.addRoles(roles); authorizationInfo.addStringPermissions(permissions); return authorizationInfo; } /** * 身份驗證操作 */ @Override protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken token) throws AuthenticationException { String username = (String) token.getPrincipal(); User user = userService.findUserByUsername(username); if(user==null){ //木有找到用戶 throw new UnknownAccountException("沒有找到該賬號"); } /* if(Boolean.TRUE.equals(user.getLocked())) { throw new LockedAccountException(); //帳號鎖定 } */ /** * 交給AuthenticatingRealm使用CredentialsMatcher進行密碼匹配,如果覺得人家的不好可以在此判斷或自定義實現 */ SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(),getName()); return info; } @Override public String getName() { return getClass().getName(); } }
1、添加shiroFilter定義
Xml代碼
<!-- Shiro Filter -->
< bean id = "shiroFilter" class = "org.apache.shiro.spring.web.ShiroFilterFactoryBean" >
< property name = "securityManager" ref = "securityManager" />
< property name = "loginUrl" value = "/login" />
< property name = "successUrl" value = "/user/list" />
< property name = "unauthorizedUrl" value = "/login" />
< property name = "filterChainDefinitions" >
< value >
/ login = anon
/user/** = authc
/role/edit/* = perms[role:edit]
/role/ save = perms [role:edit]
/role/ list = perms [role:view]
/** = authc
</ value >
</ property >
</ bean >
2、添加securityManager定義
Xml代碼
< bean id = "securityManager" class = "org.apache.shiro.web.mgt.DefaultWebSecurityManager" >
< property name = "realm" ref = "myRealm" />
</ bean >
3、添加realm定義
Xml代碼
< bean id = " myRealm" class = "com.jay.demo.shiro.
UserRealm<span class="attribute-value" style="font-size: 250, 250, 250);">"<span style="color: black; font-size: 1em; font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: rgb(250, 250, 250);"> <span class="tag" style="font-size: 0, 102, 153); font-weight: bold; background-color: rgb(250, 250, 250);">/><span >
4、配置EhCache
< bean id = "cacheManager" class = "org.apache.shiro.cache.ehcache.EhCacheManager" />
5、 保證實現了Shiro內部lifecycle函數的bean執行
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
特別注意:
如果使用Shiro相關的注解,需要在springmvc-servlet.xml中配置一下信息
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/><"org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <"securityManager" "securityManager"/></
以上是“Spring如何整合Shiro做權限控制模塊”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。