- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Spring-MVC 应用程序(即我正在使用 Spring 的调度程序 servlet)。我还使用 Spring Security 来验证用户身份。由于我使用 Spring 的调度程序 servlet,因此我不必声明
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
在我的 web.xml 中,以便能够使用 RequestContextHolder
(如果我正确理解文档)。
我的问题涉及我的接口(interface)org.springframework.security.web.authentication.AuthenticationSuccessHandler
的实现:
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
int timeout = 60*60;
//does work
request.getSession().setMaxInactiveInterval(timeout); //60 minutes
System.out.println("Session timeout of user: " + authentication.getName() + " has been set to: " + timeout + " seconds.");
/*
//does not work
session().setMaxInactiveInterval(timeout); //60 minutes
System.out.println("Session timeout of user: " + request.getUserPrincipal().getName() + " has been set to: " + timeout + " seconds.");
*/
//now restore the default work flow (SavedRequestAwareAuthenticationSuccessHandler is the default AuthenticationSuccessHandler that Spring uses,
// see: http://static.springsource.org/spring-security/site/docs/3.0.x/reference/core-web-filters.html#form-login-flow-handling )
(new SavedRequestAwareAuthenticationSuccessHandler()).onAuthenticationSuccess(request, response, authentication);
}
public static HttpSession session() {
ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
return attr.getRequest().getSession(true); // true == allow create
}
}
您能解释一下为什么在上述代码中,RequestContextHolder.currentRequestAttributes()
和 HttpServletRequest.getUserPrincipal()
不起作用(它们在 Controller 内工作)?
谢谢!
最佳答案
Spring security 是基于过滤器的。这就是为什么您需要定义 RequestContextListener,因为当 spring-security 事件发生并且 spring 请求上下文尚未设置时,DispatcherServlet 还不会被调用。
关于spring - 从 AuthenticationSuccessHandler 访问 RequestContextHolder 和 HttpServletRequest.getUserPrincipal(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7605285/
我已经创建了一个 ContainerRequestfilter 并成功触发了它。现在我想访问我之前在 grizzly HttpServerProbe 中设置的 UserPrincipal,如下所示:
我对 Java EE 有点陌生,正在尝试学习它。所以我不清楚 request.getUserPrincipal() 到底是如何工作的。它从哪里获取Principal对象?该对象何时以及如何创建? 因此
我在前端使用 AngularJS,在后端使用 Java EE。我创建了两种使用 RESTful 服务登录和注销的方法: @POST @Path("login") @Produces("applicat
我在 websphere application server 7.0 上部署了 web 应用程序。用户使用/j_security_check 登录。当 session 超时发生时 session i
用户已成功通过身份验证,但在我转到下一个 Controller 时进行身份验证后,我得到了 request.getUserPrincipal() null。我正在使用 websphere 7,我的应用
在我的 WebService 中,我需要记录调用者的 ID,但 wsContext.getUserPrincipal() 返回 null,即使用户是使用带密码身份验证的 WS-Security 进行身
以下是我的代码,对我来说似乎没问题,但在运行时出现错误HTTP Status 500。 protected void doPost(HttpServletRequest request, Ht
大家好,我正在尝试在我的 Web 服务和客户端应用程序之间使用纯 SSL。它们都在 GlassFish 2.1.1 中运行,并且各自位于不同的域中。客户端应用程序本身就是 Web 应用程序,我添加了
我正在创建一个使用相互身份验证的 Web 服务(和测试客户端)。我用过 this guide创建 CA 和 key /信任库,但我没有为客户端创建 .p12 文件,而是使用“设置您的 Web 服务器”
有人知道为什么 UserPrincipal 的 getName() 方法有时会提供一个空字符串吗?大多数时候它会返回正确的用户名,但并非每次都如此。 这种行为是随机发生的。我可以启动应用程序,运行命令
在我们的应用程序中观察到一个非常奇怪的情况(Spring Boot 1.5.6 具有所有默认的 BOM 依赖项):您可以完美登录(使用 AbstractPreAuthenticatedProcessi
我需要将现有应用程序与 Acegi/Spring 安全性连接起来。 为了开始,我正在寻找一条简单的信息:在这种情况下,从我的应用程序调用的 HttpServletRequest.getUserPrin
本文整理了Java中org.apache.tomcat.websocket.WsSession.getUserPrincipal()方法的一些代码示例,展示了WsSession.getUserPrin
我正在尝试在 Tomcat 7 应用程序上设置相互身份验证。我的 server.xml 具有以下连接器: 当我尝试使用类似于“https://testserver:8443/myapp/LoadU
这两个人似乎在做同样的事情。谁能解释两者之间的主要区别?你什么时候使用一个与另一个? HttpServletRequest.getRemoteUser() HttpServletRequest.get
我有一个 Spring-MVC 应用程序(即我正在使用 Spring 的调度程序 servlet)。我还使用 Spring Security 来验证用户身份。由于我使用 Spring 的调度程序 se
我们在使用更高版本和最新版本的 Tomcat 时遇到问题,这会阻止我们升级到比 7.0.22 更高的版本。 我们将 FormBasedAuthentication 与自定义领域一起使用。 这是在 Wi
我的环境是 Jboss AS 7.1.0。我正在使用 JUnit 和 Arquillian 进行单元测试。我的 ejb 中有使用 getUserPrincipal() 和 isCallerInRole
我是一名优秀的程序员,十分优秀!