基于Jersey使用Session
原文: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的更多相关文章
- nginx负载均衡基于ip_hash的session粘帖
nginx负载均衡基于ip_hash的session粘帖 nginx可以根据客户端IP进行负载均衡,在upstream里设置ip_hash,就可以针对同一个C类地址段中的客户端选择同一个后端服务器,除 ...
- 项目分布式部署那些事(1):ONS消息队列、基于Redis的Session共享,开源共享
因业务发展需要现在的系统不足以支撑现在的用户量,于是我们在一周之前着手项目的性能优化与分布式部署的相关动作. 概况 现在的系统是基于RabbitHub(一套开源的开发时框架)和Rabbit.WeiXi ...
- .Net分布式架构(二):基于Redis的Session共享
一:Session简介 Session是什么呢?简单来说就是服务器给客户端的一个编号.当一台web服务器运行时,可能有若干个用户浏览正在运正在这台服务器上的网站.当每个用户首次与这台web服务器建立连 ...
- 使用redis进行基于shiro的session集群共享
之前写过一篇nginx多tomcat负载均衡,主要记录了使用nginx对多个tomcat 进行负载均衡,其实进行负载均衡之前还有一个问题没有解决,那就是集群间的session共享,不然用户在登录网站之 ...
- 基于jersey和Apache Tomcat构建Restful Web服务(二)
基于jersey和Apache Tomcat构建Restful Web服务(二) 上篇博客介绍了REST以及Jersey并使用其搭建了一个简单的“Hello World”,那么本次呢,再来点有趣的东西 ...
- 基于jersey和Apache Tomcat构建Restful Web服务(一)
基于jersey和Apache Tomcat构建Restful Web服务(一) 现如今,RESTful架构已然成为了最流行的一种互联网软件架构,它结构清晰.符合标准.易于理解.扩展方便,所以得到越来 ...
- ASP.NET 在 Windows Azure 环境中使用基于 SQLServer 的 Session
Session 嘛,占一点儿服务器资源,但是总归比 ViewState 和 Cookie 安全点儿,所以还是要用的. Windows Azure 环境中的 Web 服务器经由负载均衡调度,根本无法保证 ...
- Tomcat7基于Redis的Session共享实战二
目前,为了使web能适应大规模的访问,需要实现应用的集群部署.集群最有效的方案就是负载均衡,而实现负载均衡用户每一个请求都有可能被分配到不固定的服务器上,这样我们首先要解决session的统一来保证无 ...
- 【Jersey】基于Jersey构建Restful Web应用
环境说明 java: 1.6: tomcat: 6.0.48: Jersey:1.18: Jersey介绍 主要用于构建基于Restful的Web程序: 构建基于Maven的Javaweb程序 说明: ...
随机推荐
- 2016年3月10日Android实习日记
待解决问题: *1:内部ScrollView与外部手势事件滑动冲突问题. *2:Linearlayout+View+LinearLayout横向排列,这其中两个LinearLayout内部各有3个竖向 ...
- Minor GC 与Full GC有什么不一样
新生代GC(Minor GC):指发生在新生代的垃圾收集动作,因为java对象大多都具备朝生夕灭的特性,所以Minor GC非常频繁,一般回收速度也非常快 老年代GC(Major GC/Full GC ...
- C++学习笔记43:STL
STL简介(standard Template Library) STL的基本组件:容器(container),迭代器(iterator),函数对象(function object) 算法(algor ...
- Object类--equals方法
equals方法 1.比较的是对象引用的是否指向同一块内存地址 public static void main(String[] args) { HuaWei huawei=new HuaWei(); ...
- 使用 IntraWeb (36) - TIWServerControllerBase
每个应用须有且只有一个 Server Controller. TIWServerControllerBase 所在单元及继承链: IWServerControllerBase.TIWServerCon ...
- Linux TC(Traffic Control)框架原理解析
近日的工作多多少少和Linux的流控有点关系.自打几年前知道有TC这么一个玩意儿而且多多少少理解了它的原理之后,我就没有再动过它,由于我不喜欢TC命令行,实在是太繁琐了.iptables命令行也比較繁 ...
- SpringBoot 日志框架
默认的日志框架 logback SpringBoot使用Logback作为默认的日志框架.logback 是log4j框架的作者开发的新一代日志框架,它效率更高.能够适应诸多的运行环境,同时天然支持S ...
- Java多线程学习(吐血超详细总结)(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 写在前面的话:此文只能说是java多线程的一个入门,其实Java里头线程完全可以写一本书了,但 ...
- 【Mac使用系列】Mac锁屏及快捷键
mac锁屏办法,我有所尝试,可用系统自带锁屏快捷键:Ctrl + Command + Q或者参考方法2,直接设置TouchBar. 这两种办法,亲测可用.我直接设置了TouchBar,锁屏解锁离得很近 ...
- PPPoE图解