博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于Jersey使用Session
阅读量:7016 次
发布时间:2019-06-28

本文共 2630 字,大约阅读时间需要 8 分钟。

原文: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")@Singletonpublic static class MyResource {    // Jersey will inject proxy of Security Context    @Context    SecurityContext securityContext;    @GET    public String getUserPrincipal() {        return securityContext.getUserPrincipal().getName();    }}
SecurityContext

 

转载于:https://www.cnblogs.com/dajianshi/p/7009769.html

你可能感兴趣的文章
[原创]隐藏用户名出现在Windows XP欢迎画面
查看>>
[SDOI2010]星际竞速——费用流
查看>>
C#开发串口总结,并提炼串口辅助类到公用类库中
查看>>
【个人笔记】《知了堂》MySQL中的数据类型
查看>>
Java “Unhandled exception type Exception”错误提示 (转)
查看>>
PHP源码之explode分析
查看>>
怪叔叔 一路走好 下辈子我们再一起玩KOF
查看>>
B.华华教月月做数学
查看>>
python 如何自动发送测试报告
查看>>
网络流24题4
查看>>
【第一组】第十一次例会纪要
查看>>
Hamming Weight的算法分析
查看>>
yii2 数据库操作详解(转载)
查看>>
JS函数
查看>>
c语言判断用户是否输入-非阻塞函数kbhit
查看>>
Hive基础
查看>>
(转)Sharepoint学习笔记—Debug--寻找 WSS_Logging下的ULSTraceLog
查看>>
Markdown anywhere
查看>>
内置函数了解一下
查看>>
Java:关于implements Serializable的警告问题
查看>>