原文:https://stackoverflow.com/questions/909185/jersey-security-and-session-management

方法一、注入HttpServletRequest,然后访问Session

     Session management is the purview of the container in which Jersey is deployed. In most production cases, it will be deployed within a container that performs session management.
The code below is a simple example of a jersey resource that gets the session object and stores values in the session and retrieves them on subsequent calls.
【译】Session属于Jersey运行所属的容器的范畴,在大多数情况下,这个容器都具有session管理功能。下面的代码是一个简单的例子,展示了一个jersey资源方法如何存贮并后续访问session内容。 @Path("/helloworld")
public class HelloWorld { @GET
@Produces("text/plain")
public String hello(@Context HttpServletRequest req) { HttpSession session= req.getSession(true);
Object foo = session.getAttribute("foo");
if (foo!=null) {
System.out.println(foo.toString());
} else {
foo = "bar";
session.setAttribute("foo", "bar");
}
return foo.toString(); }
}

HttpServletRequest getSession

方法二、注入Jersey的SecurityContext,然后访问Session

    Security information of a request is available by injecting a JAX-RS SecurityContext instance using @Context annotation. The injected security context instance provides the equivalent of the functionality available on HttpServletRequest API. The injected security context depends on the actual Jersey application deployment. For example, for a Jersey application deployed in a Servlet container, the Jersey SecurityContext will encapsulate information from a security context retrieved from the Servlet request. In case of a Jersey application deployed on a Grizzly server, the SecurityContext will return information retrieved from the Grizzly request.
【译】http请求的安全信息,可以通过使用annotation @Context 注入JAX-RS的SecurityContext来访问。注入的security context实例提供HttpServletRequest所提供的API,并且基于jersey部署的容器所提供的功能。例如, 基于Servlet容器的Jersey应用,SecurityContext 将会包装从Servlet request获取的信息。如果Jersey应用基于Grizzly server,则SecurityContext将会包装从Grizzly request所获取的信息 示例代码: @Path("basket")
public ShoppingBasketResource get(@Context SecurityContext sc) {
if (sc.isUserInRole("PreferredCustomer") {
return new PreferredCustomerShoppingBasketResource();
} else {
return new ShoppingBasketResource();
}
} 或者,注入SecurityContext 到类实例变量: @Path("resource")
@Singleton
public static class MyResource {
// Jersey will inject proxy of Security Context
@Context
SecurityContext securityContext; @GET
public String getUserPrincipal() {
return securityContext.getUserPrincipal().getName();
}
}

SecurityContext

基于Jersey使用Session的更多相关文章

  1. nginx负载均衡基于ip_hash的session粘帖

    nginx负载均衡基于ip_hash的session粘帖 nginx可以根据客户端IP进行负载均衡,在upstream里设置ip_hash,就可以针对同一个C类地址段中的客户端选择同一个后端服务器,除 ...

  2. 项目分布式部署那些事(1):ONS消息队列、基于Redis的Session共享,开源共享

    因业务发展需要现在的系统不足以支撑现在的用户量,于是我们在一周之前着手项目的性能优化与分布式部署的相关动作. 概况 现在的系统是基于RabbitHub(一套开源的开发时框架)和Rabbit.WeiXi ...

  3. .Net分布式架构(二):基于Redis的Session共享

    一:Session简介 Session是什么呢?简单来说就是服务器给客户端的一个编号.当一台web服务器运行时,可能有若干个用户浏览正在运正在这台服务器上的网站.当每个用户首次与这台web服务器建立连 ...

  4. 使用redis进行基于shiro的session集群共享

    之前写过一篇nginx多tomcat负载均衡,主要记录了使用nginx对多个tomcat 进行负载均衡,其实进行负载均衡之前还有一个问题没有解决,那就是集群间的session共享,不然用户在登录网站之 ...

  5. 基于jersey和Apache Tomcat构建Restful Web服务(二)

    基于jersey和Apache Tomcat构建Restful Web服务(二) 上篇博客介绍了REST以及Jersey并使用其搭建了一个简单的“Hello World”,那么本次呢,再来点有趣的东西 ...

  6. 基于jersey和Apache Tomcat构建Restful Web服务(一)

    基于jersey和Apache Tomcat构建Restful Web服务(一) 现如今,RESTful架构已然成为了最流行的一种互联网软件架构,它结构清晰.符合标准.易于理解.扩展方便,所以得到越来 ...

  7. ASP.NET 在 Windows Azure 环境中使用基于 SQLServer 的 Session

    Session 嘛,占一点儿服务器资源,但是总归比 ViewState 和 Cookie 安全点儿,所以还是要用的. Windows Azure 环境中的 Web 服务器经由负载均衡调度,根本无法保证 ...

  8. Tomcat7基于Redis的Session共享实战二

    目前,为了使web能适应大规模的访问,需要实现应用的集群部署.集群最有效的方案就是负载均衡,而实现负载均衡用户每一个请求都有可能被分配到不固定的服务器上,这样我们首先要解决session的统一来保证无 ...

  9. 【Jersey】基于Jersey构建Restful Web应用

    环境说明 java: 1.6: tomcat: 6.0.48: Jersey:1.18: Jersey介绍 主要用于构建基于Restful的Web程序: 构建基于Maven的Javaweb程序 说明: ...

随机推荐

  1. restful开发API

    http://blog.csdn.net/kkkloveyou/article/details/21391033 小示例http://blog.csdn.net/u011645059/article/ ...

  2. 细说firewalld和iptables

    在RHEL7里有几种防火墙共存:firewalld.iptables.ebtables,默认是使用firewalld来管理netfilter子系统,不过底层调用的命令仍然是iptables等. fir ...

  3. sql server management studio 查询的临时文件路径

    C:\Users\你的登录名称\Documents\SQL Server Management Studio\Backup Files C:\Users\你的登录名称\AppData\Local\Te ...

  4. CentOS 安装 Hadoop 手记

    Download & Install   download hadoop from http://hadoop.apache.org/releases.html#Download downlo ...

  5. 018-Go将磁盘目录实现简单的静态Web服务

    package main import( "net/http" ) func main(){ http.Handle("/", http.FileServer( ...

  6. CentOS 7安装nVIDIA显卡驱动程序

    1. 到http://www.geforce.cn/drivers,根据显卡的型号,选择下载相应的驱动程序,一般是.run文件: 2. 运行下载的.run文件,会提示X Server正在运行,不能安装 ...

  7. JNI学习小结

    Java中类型与C/C++中对应关系   Java中的类的对应   Sign签名, 用来识别对应各个方法. JDK下的javap.exe能输出签名.用法javap -s -p 完整类名     下面是 ...

  8. Memcache及telnent命令具体解释

    1.启动Memcache 经常使用參数 memcached 1.4.3 -p <num>      设置port号(默认不设置为: 11211) -U <num>      U ...

  9. 在Razor中输出Html的两种方式

    Razor中所有的Html都会自动编码,这样就不需要我们手动去编码了(安全),但在需要输出Html时就是已经转义过的Html文本了,如下所示: @{ string thisTest = "& ...

  10. javascript验证QQ号、邮箱和手机号码

    //验证QQ号码5-11位 function isQQ(qq) { var filter = /^\s*[.0-9]{5,11}\s*$/; if (!filter.test(qq)) { retur ...