例如:
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!-- 登錄頁(yè)面 ,用戶 登錄不成功自動(dòng) 返回該頁(yè)面 -->
<property name="loginUrl" value="/login"/>
<!-- 登錄成功頁(yè)面,登錄成功后跳轉(zhuǎn)到該頁(yè)面 -->
<property name="successUrl" value="/index"/>
<!-- 無(wú)權(quán)訪問(wèn)跳轉(zhuǎn)頁(yè)面 -->
<property name="unauthorizedUrl" value="permNo"/>
<!-- 自定義權(quán)限頁(yè)面設(shè)置url的訪問(wèn)權(quán)限。anon表示不用驗(yàn)證,
都可以訪問(wèn)。anthc:authc filter 監(jiān)聽(tīng),不登陸不能訪問(wèn)。logout:logout filter監(jiān)聽(tīng)。
沒(méi)有列出的常用配置:perms["remote:invoke"] :需要角色romote 和權(quán)限invoke才能訪問(wèn)。roles["admin"]需要角色admin才能訪問(wèn)。設(shè)置可用“,”隔開(kāi),
如:/admin/test = authc,roles[admin] -->
<property name="filterChainDefinitions">
<value>
<!-- 無(wú)參,表示需認(rèn)證才能使用 -->
/home=authc
/resources/**=anon
</value>
</property>
</bean>
另外一種是基于注解:
例如:
RequiresAuthentication注解要求在訪問(wèn)或調(diào)用被注解的類/實(shí)例/方法時(shí),Subject在當(dāng)前的session中已經(jīng)被驗(yàn)證。
@RequiresAuthentication
public void updateAccount(Account userAccount) {
//this method will only be invoked by a
//Subject that is guaranteed authenticated
...
}
RequiresGuest注解要求當(dāng)前Subject是一個(gè)“訪客”,也就是,在訪問(wèn)或調(diào)用被注解的類/實(shí)例/方法時(shí),他們沒(méi)有被認(rèn)證或者在被前一個(gè)Session記住。
@RequiresGuest
public void signUp(User newUser) {
//this method will only be invoked by a
//Subject that is unknown/anonymous
...
}
RequiresPermissions 注解要求當(dāng)前Subject在執(zhí)行被注解的方法時(shí)具備一個(gè)或多個(gè)對(duì)應(yīng)的權(quán)限。
@RequiresPermissions("account:create")
public void createAccount(Account account) {
//this method will only be invoked by a Subject
//that is permitted to create an account
...
}
RequiresPermissions 注解要求當(dāng)前Subject在執(zhí)行被注解的方法時(shí)具備所有的角色,否則將拋出AuthorizationException異常。
@RequiresRoles("administrator")
public void deleteUser(User user) {
//this method will only be invoked by an administrator
...
}
如果在Controller中如果直接使用上面標(biāo)簽是不起作用的,需要開(kāi)啟shiro注解
bean id="myRealm" class="com.controller.MyRealm"/>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="myRealm"/>
</bean>
<!--========================-如果使用注解方式驗(yàn)證將下面代碼放開(kāi)===============================-->
<!-- 保證實(shí)現(xiàn)了Shiro內(nèi)部lifecycle函數(shù)的bean執(zhí)行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<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>
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!--登錄-->
<prop key="org.apache.shiro.authz.UnauthenticatedException">
redirect:/login
</prop>
<!--授權(quán)-->
<prop key="org.apache.shiro.authz.UnauthorizedException">
redirect:/admin/common/exceptionLog
</prop>
</props>
</property>
<property name="defaultErrorView" value="error/genericView"/>
</bean>
其中com.controller.MyRealm類是我自定義的繼承自AuthorizingRealm的類
新聞名稱:shiro啟用注解方式-創(chuàng)新互聯(lián)
URL分享:http://www.chinadenli.net/article26/dpdjjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁(yè)設(shè)計(jì)公司、定制網(wǎng)站、微信公眾號(hào)、響應(yīng)式網(wǎng)站、網(wǎng)站制作、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容