shiro启用注解方式

摘要:
设置可用“,”隔开,如:/admin/test=authc,roles[admin]--˃          /home=authc/resources/**=anon另外一种是基于注解:例如:RequiresAuthentication注解RequiresAuthentication注解要求在访问或调用被注解的类/实例/方法时,Subject在当前的session中已经被验证。@RequiresPermissionspublicvoidcreateAccount{//thismethodwillonlybeinvokedbyaSubject//thatispermittedtocreateanaccount...}RequiresRoles注解RequiresPermissions注解要求当前Subject在执行被注解的方法时具备所有的角色,否则将抛出AuthorizationException异常。@RequiresRolespublicvoiddeleteUser{//thismethodwillonlybeinvokedbyanadministrator...}如果在Controller中如果直接使用上面标签是不起作用的,需要开启shiro注解bean/˃˂!

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

例如:

 <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 voidupdateAccount(Account userAccount) {

//this method will only be invoked by a

//Subject that is guaranteed authenticated
...

}
RequiresGuest注解

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

@RequiresGuest

public voidsignUp(User newUser) {

//this method will only be invoked by a

//Subject that is unknown/anonymous
...

}
RequiresPermissions 注解

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

@RequiresPermissions("account:create")

public voidcreateAccount(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 voiddeleteUser(User user) {

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

}

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

bean    />
<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启用注解方式》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇gotoTop返回顶部 JS家谱树(信息学奥赛一本通 1351)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

IHS代理遇到404的问题

因为工作的原因最近使用IBM Http server (IHS)做代理服务器。IHS和Apache相似度很高,有不明白的地方可以查看Apache的相关文档做参考资料。(这里不得不吐槽一下IHS相关资料真的好少!) 我们的程序访问流程是如下图。我们有一台提供服务的服务器A,IHS作为A的代理服务器接受所有客户端的访问,然后再将访问转给服务器A。服务器A返回信...

springboot启动报错

springboot启动报错信息 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of resource dependencies failed; nested excepti...

Tomcat8源码编译及导入Eclipse中研究

最近因为需求需要修改Tomcat中的某些功能,无奈只能研究那部分源码然后稍作修改。 目前Tomcat最新版是8.0,下载了源码包,编译并导入Eclipse中研究比较方便。 1. Tomcat8源码编译步骤 1.1 先到Apache Tomcat 8.0官网下载最新版Source Code Distributions,解压到某个目录下,如D:apache-t...

【转】使用XFire+Spring构建Web Service

XFire是与Axis 2并列的新一代Web Service框架,通过提供简单的API支持Web Service各项标准协议,帮助你方便快速地开发Web Service应用。 相 对于Axis来说,目前XFire相对受欢迎,加上其提供了和Spring集成的支持,在目前的Web Service开源社区拥有众多的追随者。并且因为XFire为Spring提供的支...

apache+php+mysql配置

---------------------------------------------------- ★软件工具:(下载时注意下载相应版本,不同版本安装细节可能会有差异!!) 1>httpd-2.4.18-win64-VC14.zip下载地址:http://www.apachelounge.com/download/ 2>php-5.6.16...

XAMPP重要文件目录及配置

一、XAMPP 的安装过程 1:下载XAMPP 的 Linux 版 (1.7.4)http://www.apachefriends.org/en/xampp-linux.html#374 2:安装(XAMPP 被安装在 /opt/lampp 目录下) tar xvfz xampp-linux-1.7.4.tar.gz -C /opt 卸载可用: rm...