Shiro之授权管理与授权的三种方式(1、编程方式,2、注解方式,3、jsp标签)-yellowcong
来源:互联网 发布:赚钱最快的方法 知乎 编辑:程序博客网 时间:2024/06/07 16:56
授权管理,三种方式,其中注解和jsp的方式在开发中用得比较的多,但是对于编程的方式完成注解,在实际开发中,用得不是特别多。这一节,讲解shiro如何完成授权的,授权操作,需要在Reaml中,实现doGetAuthorizationInfo 方法,多Reaml的情况下, 只要一个Reaml中满足条件,就会执行
源码地址
https://gitee.com/yellowcong/shior-dmeo/tree/master/test
Shiro的授权三种方式
1、编程方式
2、注解方式
3、jsp标签
授权
授权的时候,需要实现doGetAuthorizationInfo 这个方法,需要根据用户名来获取用户所属的权限和角色。
package com.yellowcong.shiro.realm;import java.util.Set;import org.apache.shiro.authc.AuthenticationException;import org.apache.shiro.authc.AuthenticationInfo;import org.apache.shiro.authc.AuthenticationToken;import org.apache.shiro.authc.SimpleAuthenticationInfo;import org.apache.shiro.authc.UnknownAccountException;import org.apache.shiro.authc.UsernamePasswordToken;import org.apache.shiro.authz.AuthorizationInfo;import org.apache.shiro.authz.SimpleAuthorizationInfo;import org.apache.shiro.realm.AuthorizingRealm;import org.apache.shiro.subject.PrincipalCollection;import org.apache.shiro.util.ByteSource;import org.springframework.beans.factory.annotation.Autowired;import com.yellowcong.service.RolePermissionService;import com.yellowcong.service.UserRoleService;import com.yellowcong.service.UserService;import com.yellowcong.shiro.model.User;/** * 创建日期:2017年9月23日 <br/> * 创建用户:yellowcong <br/> * 功能描述:用于授权操作 */public class SampleRealm extends AuthorizingRealm { @Autowired private UserService userService; @Autowired private UserRoleService userRoleService; @Autowired private RolePermissionService rolePermissionService; /** * 用户授权,当用户访问需要有权限的页面的情况,需要访问这个方法来获取权限列表 */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection paramPrincipalCollection) { //用户名称 Object username = paramPrincipalCollection.getPrimaryPrincipal(); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); // 根据用户ID查询角色(role),放入到Authorization里。 Set<String> roles = userRoleService.getRoleByName(username.toString()); info.setRoles(roles); // 根据用户ID查询权限(permission),放入到Authorization里。 Set<String> permissions = this.rolePermissionService.getPermissionByRole(roles); info.setStringPermissions(permissions); return info; } /** * 认证,用户登录 * 登陆的时候,会调用这个 */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken paramAuthenticationToken) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) paramAuthenticationToken; //数据库中,查询用户的信息 User user = userService.login(token.getUsername()); // token返回的是一个数组,将char类型转化为String类型 //这个是web前台传递过来的值 //这个密码的比对是通过Shiro自己给我们完成的 //密码是通过AuthenticatingRealm.getCredentialsMatcher 的方式来进行比对的 String pswDate = new String(token.getPassword()); //当用户为空的情况 if(user == null){ // 当没有用户的时候,抛出异常 throw new UnknownAccountException(); } //第一个参数:用户名/用户对象 String username =token.getUsername(); //第二个参数:用户的密码 String password = user.getPassword(); //第三个参数:盐值(这个盐是 username) ByteSource solt = null; //第四个参数:获取这个Realm的信息 String realmName =this.getName(); //他们拿到密码web的密码,同数据库获取到的密码进行比对操作 return new SimpleAuthenticationInfo(username, password, solt,realmName); }}
权限控制
1、基于代码实现权限管理
Subject currentUser = SecurityUtils.getSubject(); if (currentUser.hasRole("administrator")) { //拥有角色administrator } else { //没有角色处理 }
2、jsp端,权限的处理
通过jsp的shiro标签,进行权限的控制。
<shiro:authenticated>
登录之后 <shiro:notAuthenticated>
不在登录状态时 <shiro:guest>
用户在没有RememberMe时 <shiro:user>
用户在RememberMe时 <shiro:hasAnyRoles name="abc,123" >
在有abc或者123角色时 <shiro:hasRole name="abc">
拥有角色abc <shiro:lacksRole name="abc">
没有角色abc <shiro:hasPermission name="abc">
拥有权限资源abc <shiro:lacksPermission name="abc">
没有abc权限资源 <shiro:principal>
默认显示用户名称<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%--shiro 标签 --%><%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %> <!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <title>后台管理 - yellowcong</title> <script src="<%=request.getContextPath() %>/resources/plugins/jquery/jquery.min.js"></script></head><body class="layui-layout-body" ><shiro:hasRole name="admin"><a href="<%=request.getContextPath() %>/user/admin">管理员</a><br/></shiro:hasRole><shiro:hasRole name="user"><a href="<%=request.getContextPath() %>/user/user">普通用户</a><br/></shiro:hasRole><a href="<%=request.getContextPath() %>/user/loginOut">退出</a></body></html>
普通用户登录
管理员用户登录
3、基于注解的方式
案例
控制层
//只有管理员,才能执行这个方法@RequestMapping("/admin/test")public String adminTest(){ shiroService.test(); return "user/admin_test";}
只能在 service 层添加权限的注解 如果放到Controller层,就不生效。
package com.yellowcong.service;import org.apache.shiro.authz.annotation.RequiresRoles;import org.springframework.stereotype.Service;/** * 创建日期:2017/12/20<br/> * 创建时间:17:21:33<br/> * 创建用户:yellowcong<br/> * 机能概要: */@Service("shiroService")public class ShiroService { @RequiresRoles({"admin"}) public void test() { System.out.println("执行了程序"); }}
普通用户访问
管理人员正常执行
@RequiresRoles({“admin”})不生效的问题
1、注解需要写在Service层
会遇到打死都不执行注解的问题,后来发现,如果把Shiro的注解写在SpringMvc的Controller层就不好用,需要写在Service层
2、需要将shiro生命周期配置在Spring-mvc.xml的配置文件中
<!-- Shiro生命周期处理器--> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> <!-- AOP式方法级权限检查 --> <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>
阅读全文
0 0
- Shiro之授权管理与授权的三种方式(1、编程方式,2、注解方式,3、jsp标签)-yellowcong
- Shiro授权的三种方式
- Shiro第五篇【授权过滤、注解、JSP标签方式、与ehcache整合】
- Apache Shiro 注解方式授权
- Apache Shiro 标签方式授权
- Apache Shiro 标签方式授权
- Apache Shiro 标签方式授权
- Apache Shiro 标签方式授权
- Apache Shiro 标签方式授权
- 我的shiro之旅-注解方式授权
- shiro授权管理方式详解
- Apache Shiro 编程方式授权
- Shiro实现授权的三种操作方式
- Shiro学习随笔【三】授权方式
- (四)shiro注解授权和jsp标签授权
- Shiro入门9:Shiro授权流程和三种授权方式和权限标识符规则
- shiro 授权方式及其应用
- Shiro之加密方式-yellowcong
- 【Angular4学习】--初始文件目录结构解读
- Apache Tez基本知识
- error: .repo/manifests/: contains uncommitted changes
- State of the Lambda: Libraries Edition
- okhttp3Util工具类二次封装//及拦截器添加公共参数
- Shiro之授权管理与授权的三种方式(1、编程方式,2、注解方式,3、jsp标签)-yellowcong
- Spring事务配置文件方式
- Translation of Lambda Expressions
- 基于R语言的聚类分析(k-means,层次聚类)
- 洛谷P3384【模板】树链剖分
- MySQL数据库-存储过程详解
- 利用SpinWait进行工作线程同步
- Zookeeper集群简单实现
- springAOP的两种方式JdkDynamicAopProxy和Cglib2AopProxy源码解析