由於項目的需要,最近開始研究shiro這個框架。shiro是一個安全框架,主要是驗證和授權管理,和它類似的有spring security框架,當然,spring security框架更加強大,但是shiro更加靈活(一般小的東西都比較靈活)。
對於shiro,網上的資料可謂是少之又少。基本上算是被開濤大魔王的一個《跟我學shiro》壟斷,當然這個教程是很全面,樓主也是跟着這個教程一點點學的(沒辦法,沒有別的資料啊,很多看不懂的地方沒地方找啊,shiro官網的英文看不懂啊臥槽)。雖然過程很痛苦,還好最近對這個框架基本上了解了一些,所以現在敢記錄記錄。
這裏就不從頭開始介紹什麼ini配置了,一般都是集成在spring項目裏面的,使用maven進行項目管理,這一章先簡單的寫點基礎配置吧。代碼是學習的時候寫的,還有待修改的地方,不過學習足夠了。
- shiro實際上算是一個攔截器,當然得在web.xml中配置:
<!-- 配置shiro的核心攔截器 --> <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>
- 對於一個驗證和授權框架,我們當然得自己寫自己的驗證和授權方法。這兩個方法在一個叫Realm的類中,要我們自己來寫:
@Component public class UserRealm extends AuthorizingRealm { @Autowired private IUserService userService; @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { String username= (String) principalCollection.getPrimaryPrincipal(); User user=userService.findByUsername(username); //System.out.println("授權"+user); if (user!=null) { SimpleAuthorizationInfo info=new SimpleAuthorizationInfo(); // System.out.println(userService.findRole(username)); info.addRole(userService.findRole(username)); info.addStringPermission(userService.findPermissions(username)); // System.out.println(info); return info; } else throw new IncorrectCredentialsException(); } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { String username= (String) authenticationToken.getPrincipal(); String password=new String((char[])authenticationToken.getCredentials()); User user=userService.findByUsername(username); System.out.println(user); if(user!=null) { if(password.equals(user.getPassword())){ Session session= SecurityUtils.getSubject().getSession(); session.setAttribute("username",user.getUsername()); return new SimpleAuthenticationInfo(username,password,getName()); } else throw new UnknownAccountException(); } else { throw new UnknownAccountException(); } }
- 大家都知道spring的尿性,你用它,就得在他的配置文件裏創建相應的bean。shiro也不例外,要在他的配置文件裏面創建跟web.xml中你配置的攔截器名字一樣的bean。
分幾個小步吧:
1)最重要的你要有一個shiroFilter的bean<!--配置shiroFilter--> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <!-- 登錄路徑--> <property name="loginUrl" value="/jsp/login.jsp"/> <!--登錄成功路徑--> <property name="successUrl" value="/jsp/loginSuccess.jsp"/> <!--授權失敗路徑--> <property name="unauthorizedUrl" value="/jsp/unauthorized.jsp"/> <property name="filters"> <util:map> <entry key="authc" value-ref="formAuthenticationFilter"/> <entry key="stateless" value-ref="statelessFilter"/> <!--<entry key="ssl" value-ref="sslFilter"/>--> </util:map> </property> <!--過濾鏈定義--> <property name="filterChainDefinitions"> <value> /jsp/login.jsp=anon /jsp/loginSuccess.jsp=authc /jsp/logout.jsp=logout /jsp/success.jsp=user /static/**=anon /hello**=stateless </value> </property> </bean>
2)它還得需要一個叫安全管理器的東東:
<!--配置securityManager-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="userRealm"/>
</bean>
3)安全管理器需要一個Realm的東東,就是剛纔自己寫的那個,配置上就可以了: <!--Realm實現-->
<bean id="userRealm" class="com.pps.sps.realm.UserRealm" >
</bean>
4)shiro的生命週期管理器,人家都加了,我不加也不好看: <!-- Shiro生命週期處理器-->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
倘若需要使用註解,在springMVC的配置文件中引入這個配置文件,加上註解的配置就可以了:
<!--使用shiro註解-->
<import resource="spring-shiro.xml"/>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true"></property>
</bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"></property>
</bean>
嗯,這樣一個最基本的shiro框架就集成到spring的web項目中去了,這裏沒有使用緩存,沒有使用會話管理,沒有用到SSL。。。反正就是啥高級都沒有,就一個簡單的框架,但是基本上就可以攔截你的信息了。OK,今天先寫到這吧,回頭再寫。