shiro启用注解方式-创新互联

shiro验证权限方式一种是基于url配置文件:shiro启用注解方式

例如:

创新互联公司是一家网站设计公司,集创意、互联网应用、软件技术为一体的创意网站建设服务商,主营产品:成都响应式网站建设公司成都品牌网站建设全网整合营销推广。我们专注企业品牌在网站中的整体树立,网络互动的体验,以及在手机等移动端的优质呈现。成都网站建设、成都做网站、移动互联产品、网络运营、VI设计、云产品.运维为核心业务。为用户提供一站式解决方案,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏析悦目的作品,网站的价值服务。
 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>

        <!-- 登录页面 ,用户 登录不成功自动 返回该页面 -->
        <property name="loginUrl" value="/login"/>

        <!-- 登录成功页面,登录成功后跳转到该页面  -->
        <property name="successUrl" value="/index"/>

        <!-- 无权访问跳转页面  -->
        <property name="unauthorizedUrl" value="permNo"/>

        <!-- 自定义权限页面设置url的访问权限。anon表示不用验证,
        都可以访问。anthc:authc filter 监听,不登陆不能访问。logout:logout filter监听。
        没有列出的常用配置:perms["remote:invoke"] :需要角色romote 和权限invoke才能访问。roles["admin"]需要角色admin才能访问。设置可用“,”隔开,
        如:/admin/test = authc,roles[admin]  -->

        <property name="filterChainDefinitions">
            <value>
                <!--  无参,表示需认证才能使用 -->
          /home=authc
                /resources/**=anon

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

另外一种是基于注解:

例如:

RequiresAuthentication注解

RequiresAuthentication注解要求在访问或调用被注解的类/实例/方法时,Subject在当前的session中已经被验证。

@RequiresAuthentication

public void updateAccount(Account userAccount) {

//this method will only be invoked by a

//Subject that is guaranteed authenticated
...

}

RequiresGuest注解

RequiresGuest注解要求当前Subject是一个“访客”,也就是,在访问或调用被注解的类/实例/方法时,他们没有被认证或者在被前一个Session记住。

@RequiresGuest

public void signUp(User newUser) {

//this method will only be invoked by a

//Subject that is unknown/anonymous
...

}

RequiresPermissions 注解

RequiresPermissions 注解要求当前Subject在执行被注解的方法时具备一个或多个对应的权限。

@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 注解要求当前Subject在执行被注解的方法时具备所有的角色,否则将抛出AuthorizationException异常。

@RequiresRoles("administrator")

public void deleteUser(User user) {

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

}

如果在Controller中如果直接使用上面标签是不起作用的,需要开启shiro注解

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

    <!--========================-如果使用注解方式验证将下面代码放开===============================-->
    <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
    <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>
                <!--授权-->
                <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启用注解方式-创新互联
URL分享:https://www.cdcxhl.com/article26/dpdjjg.html

成都网站建设公司_创新互联,为您提供网页设计公司定制网站微信公众号响应式网站网站制作企业网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

搜索引擎优化