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

Spring Security注销

诸葛砚文
2023-03-14

我在Spring Security上遇到了麻烦。我可以登录但不能注销(至少不是按预期)。

登录后,我将被重定向到/secure/home.xhtml

这里是迄今为止的代码

索引.xhtm

<form method="POST" id="loginForm" action="#{request.contextPath}/j_spring_security_check" class="form-signin" autocomplete="off">
            <div class="form-group">
                <label for="username" class="control-label">#{bundle["login.username"]}</label>
                <input type="text" name="username" id="username" class="input-block-level form-control"
                       placeholder="#{bundle['label.username']}" required="true" tabindex="1" />
                <span class="help-block"></span>
            </div>
            <div class="form-group">
                <label for="password" class="control-label">#{bundle["login.password"]}</label>
                <input type="password" class="input-block-level form-control" name="password" id="password" tabindex="2"
                       placeholder="#{bundle['label.password']}" required="true" />
                <span class="help-block"></span>
            </div>
            <button type="submit" tabindex="3" class="btn btn-success btn-block">#{bundle["login.action"]}</button>
            <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
        </form>

spring-security.xhtml, http conf

<security:http use-expressions="true" >
        <security:intercept-url pattern="/secure/**" access="hasAnyRole('USER','ADMIN')" />
        <security:intercept-url pattern="/admin/**" access="hasRole('ADMIN')" />
        <!--<security:access-denied-handler error-page="/404.xhtml" />-->
        <security:form-login 
            login-page="/index.xhtml" 
            default-target-url="/secure/home.xhtml" 
            authentication-failure-url="/index.xhtml?error"
            username-parameter="username" 
            password-parameter="password" />
        <security:logout logout-url="/logout" logout-success-url="/index.xhtml?logout" invalidate-session="true" delete-cookies="JSESSIONID" />
        <security:csrf />
    </security:http>

这是我试图在stackoverflow上实现有关其他答案的注销的方式:

<a href="#{request.contextPath}/logout">logout</a>
                    <h:outputLink value="#{request.contextPath}/logout">Logout</h:outputLink>

但这两个链接都不起作用。我得到了404。我还读到你应该替换请求。带有pageContext.request的contextPath。contextPath,但这也不起作用。(不是localhost:8080/myContext/logout,而是链接将我重定向到localhost:8080/logout)

一个教程告诉我,注销也可以通过以下方式实现:

<form method="POST" id="loginForm" action="#{request.contextPath}/logout" class="form-signin" autocomplete="off">
                        <button type="submit" tabindex="3" class="btn btn-success btn-block">#{bundle["logout.action"]}</button>
                        <input type="hidden" name="#{_csrf.parameterName}" value="#{_csrf.token}" />
                    </form>

起初,它似乎解决了我的问题,但在向安全部分添加更多页面后,例如“profile.xhtml ”,我遇到了不希望的行为,即在页面加载后我被注销。因此,如果我在home.xhtml中添加如上所示的注销表单,即使我没有点击logout,我也似乎被注销了。如果我刷新页面,即使我没有点击注销,我也会被重定向到index.xhtml(登录)。因此,如果我点击profile.xhtml的链接,我自然会被重定向到index.xhtml,因为spring认为我注销了。没有这个表单,我可以保持登录,但是不能注销!真是一团糟!

啊,如果我点击表单上的注销按钮,我得到以下错误:

HTTP Status 403 - Expected CSRF token not found. Has your session expired?

type Status report

messageExpected CSRF token not found. Has your session expired?

descriptionAccess to the specified resource has been forbidden.

我真的不知道我的配置有什么问题:(!

这是我的网站.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>messages</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param> 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:/application-context.xml
            classpath:/application-security.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>

    <!-- Predefined pages -->
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    <!--    <error-page>
        <error-code>403</error-code>
        <location>/error.xhtml</location>
    </error-page>-->
    <error-page>
        <error-code>404</error-code>
        <location>/404.xhtml</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/error.xhtml</location>
    </error-page>
    <error-page>
        <exception-type>javax.faces.application.ServletException</exception-type>
        <location>/index.xhtml</location>
    </error-page>
    <error-page>
        <exception-type>java.lang.Exception</exception-type>
        <location>/error.xhtml</location>
    </error-page>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <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>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/logout</url-pattern>
    </filter-mapping>

    <!-- MIME TYPES -->
    <mime-mapping>
        <extension>css</extension>
        <mime-type>text/css</mime-type>
    </mime-mapping>
    <mime-mapping>  
        <extension>eot</extension>  
        <mime-type>application/x-font-eot</mime-type>  
    </mime-mapping>
    <mime-mapping>
        <extension>js</extension>
        <mime-type>text/javascript</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>latex</extension>
        <mime-type>application/x-latex</mime-type>
    </mime-mapping>
    <mime-mapping>  
        <extension>otf</extension>  
        <mime-type>application/x-font-opentype</mime-type>  
    </mime-mapping>   
    <mime-mapping>
        <extension>roff</extension>
        <mime-type>application/x-troff</mime-type>
    </mime-mapping>
    <mime-mapping>  
        <extension>svg</extension>  
        <mime-type>application/svg+xml</mime-type>  
    </mime-mapping>
    <mime-mapping>  
        <extension>ttf</extension>  
        <mime-type>application/x-font-ttf</mime-type>  
    </mime-mapping>
    <mime-mapping>  
        <extension>woff</extension>  
        <mime-type>application/x-font-woff</mime-type>  
    </mime-mapping> 
    <mime-mapping>  
        <extension>woff2</extension>  
        <mime-type>application/x-font-woff2</mime-type>  
    </mime-mapping>
</web-app>

使用GlassFish 4.1、spring framework版本4.1.2.RELEASE和spring security版本3.2.5.RELEASE

我将不胜感激每一个答案。此错误已经花了两天时间,没有任何解决方案:(

共有2个答案

吴高峰
2023-03-14

使用Spring url aglig或jstl url aglib写下您的url。这篇文章很适合学习Spring安全配置

翟永春
2023-03-14

您在启用了CRLF防护。

您可以尝试通过将其添加到您的表单来支持CRLF:

<input type="hidden"
    name="${_csrf.parameterName}"
    value="${_csrf.token}"/>

如果这不起作用,您可以删除CSRF,尽管这不是建议的。

更多信息:http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#csrf

 类似资料:
  • 我正在学习springsecurity(基于java的配置),我无法使注销正常工作。当我点击注销时,我看到URL更改为http://localhost:8080/logout并获取“HTTP 404-/logout”。登录功能工作正常(即使使用自定义登录表单),但问题是注销,我怀疑重定向的url“localhost:8080/logout”应该类似于“localhost:8808/springte

  • 主要内容:1.入门,2.设置用户名和密码1.入门 1.启动一个SpringBoot项目 2.导入SpringSecurity相关依赖 3.编写Controller TestController.java 用户是user 密码是刚刚的 2.设置用户名和密码 1.在配置文件中设置 2.在配置类中设置 3.自定义实现类 2.1 配置文件中设置 2.2 在配置类中设置 设置用户名为zZZ,密码为root 2.3 自定义实现类 配置类: 业务类:

  • 在WAR的情况下,它试图将请求转发到/error页面,并寻找它的处理程序方法(请参见底部的日志)。 最后我得到以下回应: 我该换什么才能得到401?

  • 1.导入jar包 web.xml spring-security.xml

  • 本文向大家介绍SpringSecurity 测试实战,包括了SpringSecurity 测试实战的使用技巧和注意事项,需要的朋友参考一下 引言 试题管理系统的安全模块使用Spring Security,代码从原华软仓库移植,在移植的过程中,发现原测试编写的不好,遂在新系统中对安全模块测试进行了重构。 Spring 测试 添加@SpringBootTest注解,意为这是一个基于SpringBoot

  • 我正在设置Angular Spring Security模块来登录和注册用户。当我注册一个用户时,一切都正常。注册后的最后一步是自动登录,但我遇到了以下错误: XMLHttpRequest无法加载超文本传输协议//localhost:8080/com-tesis/login.请求的资源上不存在“访问控制允许起源”标头。因此不允许访问起源“超文本传输协议//localhost:9000”。响应的HT

  • 问题内容: 我目前正在评估基于Java的安全框架,我是Spring 3.0用户,因此似乎似乎SpringSecurity是正确的选择,但是Spring安全性似乎受到过分复杂的困扰,它似乎并没有使安全性易于实现, Shiro似乎更加连贯,更容易理解。我正在寻找这两个框架之间的利弊清单。 问题答案: 我也同意Spring Security对我来说感觉太复杂了。当然,他们已经做了一些降低复杂性的事情,例

  • 我在这里有很多问题要解决。一直试图将上述3项技术集成到我们的WebApp中…我们要使用 null web.xml: 我的servlet-context.xml: My manager-context.xml: 编辑2 我认为主要的问题是SpringSecurity需要webapp上下文(ContextLoaderListener)才能工作,但web应用程序是在servlet上下文中运行的。控制器方