java代码shiro注解_Shiro - Java注解实现

Shiro提供了五种注解在后台使用以实现权限验证。

@RequiresAuthentication;

@RequiresGuest;

@RequiresPermissions;

@RequiresRoles;

@RequiresUser;

【1】@RequiresAuthentication

可以用于类/属性/方法,用于表明当前用户需是经过认证的用户。

@RequiresAuthentication

public void updateAccount(Account userAccount) {

//this method will only be invoked by a

//Subject that is guaranteed authenticated

...

}

【2】@RequiresGuest

表明该用户需为”guest”用户,即未认证(登录)或者系统未记住 。

@RequiresGuest

public voidindexPage() {

//this method will only be invoked by a

//Subject that is guaranteed authenticated

...

}

【3】@RequiresPermissions

当前用户需拥有指定权限 ;

@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({"account:create","admin"}) ;

【4】@RequiresRoles

当前用户需拥有指定角色

@RequiresRoles("user:create")

public void createUser(User user) {

//this method will only be invoked by a Subject

//that is permitted to create an account

...

}

如果多个角色都可以访问该方法,如下:

@RequiresRoles ({"user","admin"}) ;

【5】@RequiresUser

当前用户需为已认证用户或已记住用户,即已经认证(登录) 或 系统记住 。

注意:@RequiresUser == !@RequiresGuest 。

@RequiresUser

public void userList() {

//this method will only be invoked by a Subject

//that is permitted to create an account

...

}

这是一个shiro的入门Demo.. 使用了Spring MVC,mybaits等技术.. 数据库设计 : User : name--password Role : id--userid--roleName Function : id--userid--url tinys普通用户只能访问index.jsp admin用户通过添加了admin的permission,所以可以访问admin.jsp role用户通过添加了role角色,所以可以访问role.jsp 这是最基本的shiro的运用..目的是让你快速了解shiro的机制.. 这个Demo体现shiro的地方主要在两个类以及shiro.xml的配置文件 CustomRealm : 处理了登录验证以及授权.. ShiroAction : 用来传递登录时的用户数据..转换为token传递给realm...之后根据结果做相应的逻辑处理.. shiro.xml : shiro的主要配置... 规则定义在以下地方 : <!-- 过滤链定义 --> <property name="filterChainDefinitions"> <value> /login.jsp* = anon /index.jsp* = authc /index.do* = authc /admin.jsp*=authc,perms[/admin] /role.jsp*=authc,roles[role] </value> </property> 2015-10-28更新 --通过添加了以下内容来使用注解方式配置权限.... <!-- Support Shiro Annotation 必须放在springMVC配置文件中 --> <!-- 异常处理,权限注解会抛出异常,根据异常返回相应页面 --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="org.apache.shiro.authz.UnauthorizedException">unauth</prop> <prop key="org.apache.shiro.authz.UnauthenticatedException">login</prop> </props> </property> </bean> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor" /> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager" /> </bean> <!-- end --> --修改了过滤链 <!-- 过滤链定义 --> //简单的讲就是把需要特别处理的路径写到前面,越特殊写到越前 <property name="filterChainDefinitions"> <value> <!-- 注意这里需要把前缀写全.../shiro这里 --> /shiro/login.do*=anon /login.jsp* = anon /admin.jsp*=authc,perms[/admin] /role.jsp*=authc,roles[role] /** = authc </value> </property>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值