摘要(Abstract)
			        通过使用阿里云的RAM(Resource Access Management)服务,您可以将您云账号下RDS资源的访问及管理权限授予RAM中的子用户。
目前,可以在RAM中进行授权的资源类型只有dbinstance。在通过RAM进行授权时,资源的描述方式如下:
当子用户通过API访问RDS时,RDS后台会向RAM进行权限检查,以确保调用者拥有相应权限。每个API会根据涉及到的资源以及API的语义来确定需要检查哪些资源的权限。每个API的鉴权规则如下表所示:
sts权限   加入收藏
上面章节只用到了 RAM 的子账号功能,这些子账号都是可以长期正常使用的,发生泄露后如果无法及时解除权限,会非常危险。
当开发者的 app 被用户使用之后,用户的数据要写入 ram-test-dev 这个实例。当 app 的用户数据很多时,要求能够安全地授权给众多的 app 用户上传数据,并且保证多个用户之间存储的隔离。
类似这种场景需要临时访问权限,应该使用 STS 来完成。STS 可以指定复杂的策略来对特定的用户进行限制,仅提供最小的权限。
创建角色
创建一个名为 ram_test_app 的子账号,不需要赋予任何权限,因为在扮演角色的时候会自动获得被扮演角色的所有权限。有关创建RAM用户更多详情,请参见使用示例中的步骤1至步骤7。
创建两个角色,RamTestAppReadOnly 和 RamTestAppWrite。一个用于读取等操作,一个用于上传文件的操作。
登录RAM 控制台。
选择角色管理 > 新建角色。
选择角色类型。这里选择用户角色。
填写类型信息。因为角色是被阿里云账号使用过的,因此选择默认的即可。然后单击下一步。
配置角色基本信息。本实例中角色名称填写 RamTestAppReadOnly,然后单击创建。
完成角色创建后,单击关闭。
创建完角色之后,角色是没有任何权限的,因此需要新建一个自定义的授权策略。
选择策略管理 > 新建授权策略。
选择空白模板。
填写授权策略名称。该示例中填写ram-test-app-readonly,策略内容填写如下:
          
                         
                   
                    主题(Topic)
             
			      
			     
           
                    
                  
		
			      
			      
			   
			      
			      
			      项目(Project)
			      
			         
                                         farhad3113/iot-redis-esp  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         redis  
                  
			         
                                         slowmoon/redis  
                  
			         
                                         redis-store/redis-rack  
                  
			         
                                         redis-store/redis-actionpack  
                  
			         
                                         redis-store/redis-rails  
                  
			         
                                         redis-store/redis-store  
                  
			         
                                         redis-go/redis  
                  
			                                  org.apache.poi           poi           ${poi_version}                         org.apache.poi           poi-ooxml-schemas           ${poi_version}                         org.apache.poi           poi-scratchpad           ${poi_version}                         org.apache.poi           poi-ooxml           ${poi_version}                                       org.apache.activemq           activemq-all           5.8.0                            org.apache.activemq           activemq-pool           ${activemq_version}                            org.apache.xbean           xbean-spring           3.16                                          javax.servlet           servlet-api           ${javax.servlet-api.version}           provided                         javax.servlet.jsp           jsp-api           2.1           provided                         javax.servlet           jstl           1.2                                          redis.clients           jedis           2.7.2                         org.redisson           redisson           1.0.2                                org.slf4j           jcl-over-slf4j           ${slf4j.version}                         org.slf4j           slf4j-log4j12           ${slf4j.version}                                   org.springframework.data           spring-data-redis           1.6.2.RELEASE                         org.springframework           spring-webmvc           ${spring.version}                                             commons-logging                   commons-logging                                                   org.springframework           spring-tx           ${spring.version}                         org.springframework           spring-aop           ${spring.version}                         org.springframework           spring-context-support           ${spring.version}                         org.springframework.data           spring-data-redis           1.6.2.RELEASE                         org.springframework           spring-orm           ${spring.version}                         org.springframework           spring-jms           ${spring.version}                            org.springframework.session           spring-session           ${spring.session.version}                         org.springframework           spring-core           ${spring.version}                     redis-config.xml [html] view plain copy 在CODE上查看代码片派生到我的代码片                                                                                                                                                                                                                                  redis.properties [plain] view plain copy 在CODE上查看代码片派生到我的代码片 redis.host.ip=192.168.0.101   redis.host.port=6379           redis.maxTotal=1000     redis.maxIdle=100   redis.maxWait=2000   redis.testOnBorrow=false   redis.testOnReturn=true     web.xml [html] view plain copy 在CODE上查看代码片派生到我的代码片                                    contextConfigLocation                          classpath:/spring/redis-conf.xml                                                     springSessionRepositoryFilter           org.springframework.web.filter.DelegatingFilterProxy                         springSessionRepositoryFilter           /*                         30                                encodingFilter           org.springframework.web.filter.CharacterEncodingFilter                          encoding               UTF-8                                     forceEncoding               true                                       encodingFilter           /*                            dispatcher           org.springframework.web.servlet.DispatcherServlet                          contextConfigLocation               classpath:/spring/spring-mvc.xml                      1                         dispatcher           /                                          org.springframework.web.context.ContextLoaderListener                                   sessionServlet           sample.SessionServlet                            sessionServlet           /servlet/session                            index.jsp                这边主要是一个: [html] view plain copy 在CODE上查看代码片派生到我的代码片               springSessionRepositoryFilter       org.springframework.web.filter.DelegatingFilterProxy             springSessionRepositoryFilter       /*             30      这个filter一定要写在一切filter之前  SessionController [java] view plain copy 在CODE上查看代码片派生到我的代码片 package sample;      import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;   import org.springframework.stereotype.Controller;   import org.springframework.ui.Model;   import org.springframework.web.bind.annotation.RequestMapping;      import javax.servlet.http.HttpServletRequest;   import javax.servlet.http.HttpSession;      /**   * Created by mk on 15/1/7.   */   @Controller   @EnableRedisHttpSession   public class SessionController {       @RequestMapping("/mySession")       public String index(final Model model, final HttpServletRequest request) {           if (request.getSession().getAttribute("testSession") == null) {               System.out.println("session is null");               request.getSession().setAttribute("testSession", "yeah");           } else {               System.out.println("not null");           }           return "showSession";       }      }    showSession.jsp文件  [html] view plain copy 在CODE上查看代码片派生到我的代码片 <%@ page language="java" contentType="text/html; charset=utf-8"       pageEncoding="utf-8"%>               showSession         <%       String sessionValue=(String)session.getAttribute("testSession");   %>      Session Value From Servlet is: <%=sessionValue%>
          测试 保证我们的redise-server是启动的,然后我们启动起这个web工程后使用: http://localhost:8080/webpoc/mySession访问一下这个controller  此时我们使用redis客户端工具连入查看spring session是否已经进入到了redis中去。 在redis客户端工具连入后我们可以在redis console中使用keys *来查看存入的key,LOOK,spring的session存入了redis中去了。  再来看我们的eclipse后台,由于我们是第一次访问这个controller,因此这个session为空,因此它显示如下:  我们在IE中再次访问该controller  由于之前的session已经存在于redis了,因此当用户在1800秒(30分钟)内再次访问controller,它会从session中获取该session的key testSession的值,因此eclipse后台打印为not null。 SpringRedisTemplate + Redis 讲过了spring session+redis我们来讲使用spring data框架提供的redisTemplate来访问redis service吧。说实话,spring这个东西真强,什么都可以集成,cassandra, jms, jdbc...jpa...bla...bla...bla...Spring集成Barack Hussein Obama? LOL :)  pom.xml 不用列了,上面有了 redis-conf.xml 不用列了,上面有了 web.xml 也不用列了,上面也有了 SentinelController.java 我们就先用这个名字吧,后面我们会用它来做我们的redis sentinel(哨兵)的高可用(HA)集群测试 [java] view plain copy 在CODE上查看代码片派生到我的代码片 package sample;      import java.util.HashMap;   import java.util.Map;      import org.springframework.beans.factory.annotation.Autowired;   import org.springframework.context.ApplicationContext;   import org.springframework.context.support.ClassPathXmlApplicationContext;   import org.springframework.data.redis.core.BoundHashOperations;   import org.springframework.data.redis.core.StringRedisTemplate;   import org.springframework.stereotype.Controller;   import org.springframework.ui.Model;   import org.springframework.web.bind.annotation.ExceptionHandler;   import org.springframework.web.bind.annotation.RequestMapping;      import redis.clients.jedis.Jedis;   import redis.clients.jedis.JedisSentinelPool;   import util.CountCreater;      import javax.servlet.http.HttpServletRequest;   import javax.servlet.http.HttpSession;      /**   * Created by xin on 15/1/7.   */   @Controller   public class SentinelController {          @Autowired       private StringRedisTemplate redisTemplate;          @RequestMapping("/sentinelTest")       public String sentinelTest(final Model model,               final HttpServletRequest request, final String action) {           return "sentinelTest";       }          @ExceptionHandler(value = { java.lang.Exception.class })       @RequestMapping("/setValueToRedis")       public String setValueToRedis(final Model model,               final HttpServletRequest request, final String action)               throws Exception {           CountCreater.setCount();           String key = String.valueOf(CountCreater.getCount());           Map mapValue = new HashMap();           for (int i = 0; i < 1000; i++) {               mapValue.put(String.valueOf(i), String.valueOf(i));           }           try {               BoundHashOperations boundHashOperations = redisTemplate                       .boundHashOps(key);               boundHashOperations.putAll(mapValue);               System.out.println("put key into redis");           } catch (Exception e) {               e.printStackTrace();               throw new Exception(e);           }                      return "sentinelTest";       }         }    打开IE,输入:http://localhost:8080/webpoc/setValueToRedis 观察我们的后台  然后使用redis client连入后进行查看  看。。。这个值key=1的,就是我们通过spring的redisTemplate存入进去的值,即使用下面这段代码进行存入的值: [java] view plain copy 在CODE上查看代码片派生到我的代码片  for (int i = 0; i < 1000; i++) {       mapValue.put(String.valueOf(i), String.valueOf(i));   }   try {       BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(key);       boundHashOperations.putAll(mapValue);    如何你要存入一个简单的如key=test value=hello,你可以这样使用你的redisTemplate [java] view plain copy 在CODE上查看代码片派生到我的代码片 redisTemplate.execute(new RedisCallback
			         
                                         Redis  
                  
			 		全部项目