shiro啟用注解方式-創(chuàng)新互聯(lián)

shiro驗證權(quán)限方式一種是基于url配置文件:shiro啟用注解方式

例如:

創(chuàng)新互聯(lián)公司是一家網(wǎng)站設(shè)計公司,集創(chuàng)意、互聯(lián)網(wǎng)應(yīng)用、軟件技術(shù)為一體的創(chuàng)意網(wǎng)站建設(shè)服務(wù)商,主營產(chǎn)品:成都響應(yīng)式網(wǎng)站建設(shè)公司、成都品牌網(wǎng)站建設(shè)全網(wǎng)整合營銷推廣。我們專注企業(yè)品牌在網(wǎng)站中的整體樹立,網(wǎng)絡(luò)互動的體驗,以及在手機等移動端的優(yōu)質(zhì)呈現(xiàn)。成都網(wǎng)站建設(shè)、成都做網(wǎng)站、移動互聯(lián)產(chǎn)品、網(wǎng)絡(luò)運營、VI設(shè)計、云產(chǎn)品.運維為核心業(yè)務(wù)。為用戶提供一站式解決方案,我們深知市場的競爭激烈,認(rèn)真對待每位客戶,為客戶提供賞析悅目的作品,網(wǎng)站的價值服務(wù)。
 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>

        <!-- 登錄頁面 ,用戶 登錄不成功自動 返回該頁面 -->
        <property name="loginUrl" value="/login"/>

        <!-- 登錄成功頁面,登錄成功后跳轉(zhuǎn)到該頁面  -->
        <property name="successUrl" value="/index"/>

        <!-- 無權(quán)訪問跳轉(zhuǎn)頁面  -->
        <property name="unauthorizedUrl" value="permNo"/>

        <!-- 自定義權(quán)限頁面設(shè)置url的訪問權(quán)限。anon表示不用驗證,
        都可以訪問。anthc:authc filter 監(jiān)聽,不登陸不能訪問。logout:logout filter監(jiān)聽。
        沒有列出的常用配置:perms["remote:invoke"] :需要角色romote 和權(quán)限invoke才能訪問。roles["admin"]需要角色admin才能訪問。設(shè)置可用“,”隔開,
        如:/admin/test = authc,roles[admin]  -->

        <property name="filterChainDefinitions">
            <value>
                <!--  無參,表示需認(rèn)證才能使用 -->
          /home=authc
                /resources/**=anon

            </value>
        </property>
    </bean>

另外一種是基于注解:

例如:

RequiresAuthentication注解

RequiresAuthentication注解要求在訪問或調(diào)用被注解的類/實例/方法時,Subject在當(dāng)前的session中已經(jīng)被驗證。

@RequiresAuthentication

public void updateAccount(Account userAccount) {

//this method will only be invoked by a

//Subject that is guaranteed authenticated
...

}

RequiresGuest注解

RequiresGuest注解要求當(dāng)前Subject是一個“訪客”,也就是,在訪問或調(diào)用被注解的類/實例/方法時,他們沒有被認(rèn)證或者在被前一個Session記住。

@RequiresGuest

public void signUp(User newUser) {

//this method will only be invoked by a

//Subject that is unknown/anonymous
...

}

RequiresPermissions 注解

RequiresPermissions 注解要求當(dāng)前Subject在執(zhí)行被注解的方法時具備一個或多個對應(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
...

}

RequiresRoles 注解

RequiresPermissions 注解要求當(dāng)前Subject在執(zhí)行被注解的方法時具備所有的角色,否則將拋出AuthorizationException異常。

@RequiresRoles("administrator")

public void deleteUser(User user) {

//this method will only be invoked by an administrator
...

}

如果在Controller中如果直接使用上面標(biāo)簽是不起作用的,需要開啟shiro注解

bean id="myRealm" class="com.controller.MyRealm"/>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="myRealm"/>
    </bean>

    <!--========================-如果使用注解方式驗證將下面代碼放開===============================-->
    <!-- 保證實現(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://bm7419.com/article26/dpdjjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司、定制網(wǎng)站、微信公眾號、響應(yīng)式網(wǎng)站、網(wǎng)站制作企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化