当前位置: 首页 > 知识库问答 >
问题:

Spring Security LDAP:登录后立即清除会话

尉迟浩思
2023-03-14

我有一个使用Spring安全LDAP的登录页面。身份验证本身工作正常。在我将表单从常规 HTML 更改为主要字体/ JSF 标记后,发生了此问题。用户通过身份验证后,该页不会重定向到索引页,而是在第二次尝试后。由于某种原因,会话被立即清除。我已经阅读了很多关于使用JSF登录的帖子和教程,以及类似的问题,但到目前为止,它们都没有奏效。

以下是我的一些配置

登录.xhtml 在首字母/JSF之前

<form action='#{request.contextPath}/j_spring_security_check' method='POST' id="loginForm">
...
</form>

login.xhtml后表面/JSF

<h:form id="loginForm">         
    <h:outputLabel for="j_username" value="User" /> 
    <p:inputText id="j_username" required="true" value="#{loginBean.username}"></p:inputText>

    <h:outputLabel for="j_password" value="Password" /> 
    <p:password id="j_password" required="true" value="#{loginBean.password}"></p:password>             

    <h:commandButton type="submit" id="loginButton" action="#{loginBean.doLogin}" value="LOGIN" />              
</h:form>

securityContext.xml

<security:http use-expressions="true">
    <security:intercept-url pattern="/login.xhtml" access="isAnonymous()" />
    <security:intercept-url pattern="/index.xhtml" access="isAuthenticated()" />

    <security:form-login login-processing-url="/j_spring_security_check" 
                         login-page="/login.xhtml"          
                         authentication-failure-handler-ref="authenticationFailureHandler"  
                         default-target-url="/index.xhtml"      
                         always-use-default-target="true" />
     ...
</security:http>

LoginBean.java

public String doLogin() throws IOException, ServletException {

       try {

           ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
           RequestDispatcher dispatcher = ((ServletRequest)context.getRequest()).getRequestDispatcher("/j_spring_security_check");              
           dispatcher.forward((ServletRequest)context.getRequest(), (ServletResponse)context.getResponse());
           FacesContext.getCurrentInstance().responseComplete();

           return "/index?faces-redirect=true";          

       } 

       ...

}

网站.xml

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

faces-config.xml

<managed-bean>
    <managed-bean-name>loginBackingBean</managed-bean-name>
    <managed-bean-class>my.project.jsf.beans.LoginBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>    
</managed-bean> 

堆栈跟踪的一部分

<SecurityContext is empty or anonymous - context will not be stored in HttpSession. >
SEVERE: Servlet.service() for servlet Faces Servlet threw exception
java.lang.IllegalStateException
    at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435)

共有1个答案

伍捷
2023-03-14

解决。问题与登录后页面的重定向有关。我使用了以下代码行:

FacesContext.getCurrentInstance().getExternalContext().redirect("inicio.xhtml");

阅读这篇文章后,我得到了这个想法

LoginBean。java看起来如下:

public String doLogin() throws IOException, ServletException {

       try {

           ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
           RequestDispatcher dispatcher = ((ServletRequest)context.getRequest()).getRequestDispatcher("/j_spring_security_check");              
           dispatcher.forward((ServletRequest)context.getRequest(), (ServletResponse)context.getResponse());
           FacesContext.getCurrentInstance().responseComplete();
           FacesContext.getCurrentInstance().getExternalContext().redirect("inicio.xhtml");   

           return;

       } 

       ...

}
 类似资料:
  • 这是views.py的内容: 在转到“/”时,我被重定向到“/accounts/login”,它将我带到登录页面。我输入用户名和密码后,在“验证”打印语句printd“登录”到终端。 到目前为止还不错。现在,我不再被重定向到“/”,而是再次被重定向到“/accounts/login”,并再次显示输入用户名和密码的页面。为什么?

  • (注意:这与我发布的问题有关,但由于我最初的问题得到了回答,而我现在遇到了一个不同的问题,所以我将此作为一个新问题发布。) 我正在设置注册和登录一个使用DynamoDB和AWS Cognito的iOS应用程序。我最终让注册登录过程正常工作,但我注意到,每当我注销并立即尝试重新登录时,应用程序都会失败,我会收到错误消息

  • 问题内容: 今天尝试升级詹金斯。它不再开始了。 即使我尝试删除其目录,它也会干净地重新创建它,但是随后出现以下日志崩溃。 詹金斯1.512在Glassfish 3.1.2.2上 编辑:版本1.421起作用。版本1.422失败。即使擦除了jenkins目录,这也是一致的。 问题答案: 找到了这个,我有两个虚拟服务器: https://gist.github.com/andrewg4153/36935

  • 我的应用程序使用了一个键控窗口,该窗口由时间戳函数键控。这意味着一旦该特定窗口被激发和处理,保持该键活动就没有用了,因为该特定键不会再次出现。因为这个用例涉及到持续扩展键,所以我想在一个键完成处理后立即清除它的状态,而不必配置计时器。 在每个键控窗口完成处理后,这是否可以在方法或方法中实现?

  • 我的应用程序通过Facebook/Firebase进行身份验证。整个应用程序由片段和ofc一个活动组成。 案例(简单地): 有2个片段:和。 我在清除了缓存并从firebase和facebook注销后卸载了该应用程序。 在< code>howToFragment中,我检查是否已经有用户登录。 然后我在调试模式下安装应用程序。我看到该用户已登录并导航到主碎片。当我检查用户的ID时,我看到它是我的主要

  • 我已经创建了一个默认的表单登录身份验证,下面是我的配置。 身份验证工作正常,但最近我在应用程序中添加了会话超时。我的应用程序每5秒轮询一次服务器,因此默认超时不起作用。我在谷歌上找到了下面的过滤器解决方案,并实现了它。 另外,2)我没有得到会话超时错误消息,因为它被重定向到这个页面,一次又一次地转到登录页面。我不能保存这个错误消息,直到用户再次登录。 有什么指示吗?