基于Spring框架的Shiro配置方法
一、在web.xml中添加shiro過濾器
<!-- Shiro filter--> <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的applicationContext.xml中添加shiro配置
1、添加shiroFilter定義
<!-- 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定義
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="myRealm" />
</bean>
3、添加realm定義
<bean id=" myRealm" class="com...MyRealm" />
三、實現(xiàn)MyRealm:繼承AuthorizingRealm,并重寫認證授權(quán)方法
public class MyRealm extends AuthorizingRealm{ private AccountManager accountManager; public void setAccountManager(AccountManager accountManager) { this.accountManager = accountManager; } /** * 授權(quán)信息 */ protected AuthorizationInfo doGetAuthorizationInfo( PrincipalCollection principals) { String username=(String)principals.fromRealm(getName()).iterator().next(); if( username != null ){ User user = accountManager.get( username ); if( user != null && user.getRoles() != null ){ SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); for( SecurityRole each: user.getRoles() ){ info.addRole(each.getName()); info.addStringPermissions(each.getPermissionsAsString()); } return info; } } return null; } /** * 認證信息 */ protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken authcToken ) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) authcToken; String userName = token.getUsername(); if( userName != null && !"".equals(userName) ){ User user = accountManager.login(token.getUsername(), String.valueOf(token.getPassword())); if( user != null ) return new SimpleAuthenticationInfo( user.getLoginName(),user.getPassword(), getName()); } return null; } }
相關(guān)文章
HttpClient的RedirectStrategy重定向處理核心機制
這篇文章主要為大家介紹了HttpClient的RedirectStrategy重定向處理核心機制源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10深入理解Java中的并發(fā)工具類CountDownLatch
CountDownLatch?作為?Java?中的一個同步工具類,用于在多線程間實現(xiàn)協(xié)調(diào)和控制,本文主要來和大家講解一下JUC?工具類?CountDownLatch的使用,需要的可以參考一下2023-07-07java輸入時如何通過回車(enter)來結(jié)束輸入
這篇文章主要介紹了java輸入時如何通過回車(enter)來結(jié)束輸入,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05如何利用Spring?MVC實現(xiàn)RESTful風格
這篇文章主要介紹了如何利用Spring?MVC實現(xiàn)RESTful風格,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Spring和IDEA不推薦使用@Autowired?注解原因解析
這篇文章主要為大家介紹了Spring和IDEA不推薦使用@Autowired?注解原因解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07引入mybatis-plus報 Invalid bound statement錯誤問題的解決方法
這篇文章主要介紹了引入mybatis-plus報 Invalid bound statement錯誤問題的解決方法,需要的朋友可以參考下2020-05-05