2019独角兽企业重金招聘Python工程师标准>>>

Servlet APi 集成

Servlet 2.5+ Integration

15.1.1 HttpServletRequest.getRemoteUser()得到用户名.

15.1.2 HttpServletRequest.getUserPrincipal()

HttpServletRequest.getUserPrincipal()得到SecurityContextHolder.getContext().getAuthentication()的信息.

Authentication auth = httpServletRequest.getUserPrincipal();
// assume integrated custom UserDetails called MyCustomUserDetails
// by default, typically instance of UserDetails
MyCustomUserDetails userDetails = (MyCustomUserDetails) auth.getPrincipal();
String firstName = userDetails.getFirstName();
String lastName = userDetails.getLastName();

15.1.3 HttpServletRequest.isUserInRole(String)

是否有角色

boolean isAdmin = httpServletRequest.isUserInRole("ADMIN");

15.2 Servlet 3+ Integration

15.2.1 HttpServletRequest.authenticate(HttpServletRequest,HttpServletResponse)

HttpServletRequest.authenticate(HttpServletRequest,HttpServletResponse) 可以保证用户被认证.如果用户没被认证,AuthenticaitonEntryPoint触发认证.

15.2.2 HttpServletRequest.login(String,String)

登陆

try {
httpServletRequest.login("user","password");
} catch(ServletException e) {
// fail to authenticate
}

15.2.3 HttpServletRequest.logout()

登出

AsyncContext.start(Runnable)

异步操作

final AsyncContext async = httpServletRequest.startAsync();
async.start(new Runnable() {public void run() {Authentication authentication = SecurityContextHolder.getContext().getAuthentication();try {final HttpServletResponse asyncResponse = (HttpServletResponse) async.getResponse();asyncResponse.setStatus(HttpServletResponse.SC_OK);asyncResponse.getWriter().write(String.valueOf(authentication));async.complete();} catch(Exception e) {throw new RuntimeException(e);}}
});

异步输出用户信息

Async Servlet Support

servlet至少是3.0

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"></web-app>

下一步添加DelegatingFilterProxy的异步支持

filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>

15.3 Servlet 3.1+ Integration

15.3.1 HttpServletRequest#changeSessionId()

可以用来对抗session固定攻击.

转载于:https://my.oschina.net/u/1590027/blog/913448

与servlet Api 的集成相关推荐

  1. 关于Struts2三种访问Servlet API方式的总结

    在开发j2ee过程中,通常我们需要访问的Servlet API是:HttpSession,HttpservletRequest,ServletContext,这三者分别对应于JSP的内置对象:sess ...

  2. Action访问Servlet API的三种方法

    一.为什么要访问Servlet API ? Struts2的Action并未与Servlet API进行耦合,这是Struts2 的一个改良,从而方便了单独对Action进行测试.但是对于Web控制器 ...

  3. Struts2中Action访问Servlet API的三种方法

    Struts2的Action并未直接与任何Servlet API耦合,这是Struts2的一个改良之处,因为Action类不再与Servlet API耦合,能更轻松的测试该Action.但如何访问? ...

  4. Struts(八)Strits2访问servlet API

    1.获得servlet API的两种方法 (1).ServletActionContext类(基本都是静态方法) public static HttpServletRequet getRequest( ...

  5. Action访问Servlet API的对象

    1,Action访问ActionContext的方式:(request session application) Struts2中的Action与Servlet API完全分离,但是在实现业务逻辑处理 ...

  6. Maven(十)通过Maven缺失servlet.api的解决方式看provide(依赖范围)

    1. Eclipse解决servlet.api缺失的方法参考此处 2. 通过配置pom.xml里依赖来添加servlet.api 在里面添加如下代码保存后错误立刻消失 <dependencies ...

  7. struts2中访问servlet API

    Struts2中的Action没有与任何Servlet API耦合,,但对于WEB应用的控制器而言,不访问Servlet API几乎是不可能的,例如需要跟踪HTTP Session状态等.Struts ...

  8. 8.ActionContext类与Servlet API解耦的访问方式

    为了避免与Servlet API耦合在一起,方便Action类做单元测试,   Struts2对HttpServletRequest.HttpSession和ServletContext进行了封装, ...

  9. 文件上传下载—servlet API实现

    servlet API实现文件上传下载需要的jar包: UploadServlet.java package com.ymw.web.servlet;import java.io.File; impo ...

最新文章

  1. MegEngine推理性能优化
  2. usb打印机命令_Hyper-V与你的虚拟机共享设备、USB设备
  3. 【Maven学习】定制库到Maven本地资源库
  4. VC中使用全局变量的2种办法及防错措施
  5. javascript的万能查询器根据网上一些朋友的代码个性后得到.
  6. Python 数据科学手册 5.5 朴素贝叶斯分类
  7. [转载] application/json 四种常见的 POST 提交数据方式
  8. Proteus十字路口交通灯
  9. PLC气动机械手控制系统毕业设计【附带仿真】
  10. 被指开除高级研究员,谷歌大神Jeff Dean回应:是她说不答应条件就离职
  11. 计算机毕业设计ssm鹊巢大连分公司分销商管理系统51g9m系统+程序+源码+lw+远程部署
  12. 追加安装sticky模块
  13. 图灵mysql_图灵学院JAVA架构师-VIP-MySQL底层实现之B+树
  14. 写自己的故事,帮别人说话
  15. Python 三维动画生成 绘制螺旋线
  16. [xueqi]吃着榨菜,轻松搞下漏洞百出的湾湾站
  17. 近端串扰与远端串扰分析
  18. Arcgis处理栅格数据中Nodata值
  19. c语言 dtr rts 编程,C# 怎样设置RTS和DTR
  20. python数字转换为大写中文_python 人民币数字转汉字大写金额

热门文章

  1. 在开发的过程如何使用源码
  2. 谈一谈周公所理解的面试
  3. JAVA web项目转客户端(nativefier)
  4. setting.xml的mirror、mirrorOf和pom.xml的repositories、repository的关系关联*
  5. OpenGL ES 详解纹理生成和纹理映射步骤以及函数
  6. ActiveMQ入门教程(二) - ActiveMQ的安装
  7. 比Spring简单的IoC容器
  8. android textview改变部分文字的颜色和string.xml中文字的替换及部分内容设置颜色、字体、超链接、图片...
  9. .Net转Java自学之路—基础巩固篇十八(正则)
  10. 一套代码小程序WebNative运行的探索01