don*_*gPB 4 spring spring-security logout http-status-code-404
我正在尝试使用spring security v.4为我的应用添加登录功能.登录工作正常,但当我尝试注销时出现错误404.Spring Security参考说默认注销URL是/ logout.我的应用程序部署在/ app URL下,我尝试按照URL的localhost:8080/app/logout和localhost:8080/app/json/logout.我在堆栈上发现了一些类似的问题,但它们是关于使用CSRF保护并且我没有使用它的情况.这是我的web.xml文件的一部分
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/json-servlet.xml,
/WEB-INF/applicationContext.xml</param-value>
</context-param>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>json</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>json</servlet-name>
<url-pattern>/json/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
和我的json-servlet.xml在哪里是spring安全配置:
<context:component-scan base-package="test" />
<mvc:annotation-driven />
<security:http>
<security:intercept-url pattern="/**" access="hasRole('USER')" />
<security:form-login />
<security:logout />
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="test" password="1" authorities="ROLE_USER, ROLE_ADMIN" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
Run Code Online (Sandbox Code Playgroud)
提前致谢.
小智 10
根据pring版本4开始的文档安全性CSRF默认启用,默认情况下唯一支持的方法是POST,因此可以使用如下形式调用它:
<form method="post" action="${pageContext.request.contextPath}/logout" id="form-logout">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>
Run Code Online (Sandbox Code Playgroud)
或者明确禁用csrf保护
<http>
<!-- ... -->
<csrf disabled="true"/>
</http>
Run Code Online (Sandbox Code Playgroud)
从文档
如果你真的想在退出时使用HTTP GET,你可以这样做,但是请记住,这通常不推荐.例如,以下Java配置将执行注销,并使用任何HTTP方法请求URL/logout:
@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5083 次 |
| 最近记录: |