gpt4 book ai didi

java - 无法使用 spring security 进行身份验证。输入凭据后

转载 作者:行者123 更新时间:2023-11-30 06:40:09 24 4
gpt4 key购买 nike

我使用 Spring security 4 进行身份验证,虽然我能够重定向到登录页面,但之后我无法进一步移动,出现 找不到此本地主机页面

的错误

这是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>self-thin-client</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/security.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>/rs/self/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>dspature</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dspature</servlet-name>
<url-pattern>/rs/*</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>Initializer</servlet-name>
<servlet-class>com.store.Initializer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>

这是我的 security.xml

    <beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.0.xsd">


<!-- Spring MVC based authentication, login page etc. -->
<http>
<intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')"/>
<form-login login-page="/rs/login" login-processing-url="/j_spring_security_check"
authentication-failure-url="/login" />
<logout logout-success-url="/login"/>
</http>

<!-- Declared authentication manager -->
<authentication-manager alias="authenticationManager">
<authentication-provider ref="AuthenticationManager" />
</authentication-manager>

<!-- Bean implementing AuthenticationProvider of Spring Security -->
<beans:bean id="AuthenticationManager" class="com.radix.server.login.AuthenticationManager">
</beans:bean>

</beans:beans>

这是我的 html 表单,

 <form method="post" action="/j_spring_security_check" >
<p>
<span>Username:</span>
<input type="text" name= "j_username" >
</p>
<p>
<span>Password:</span>
<input type="password" name= "j_password" >
</p>
<p>
<input type="submit" value="submit">
</p>
</form>

我认为问题可能出在 url 映射上,但我无法弄清楚我做错了什么,请帮我解决

最佳答案

web.xml 中更改此映射这样:

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

否则,您的login-processing-url和其他不会匹配 FilterChainProxy url 模式,因此它们无法正常运行。

编辑:为了避免 CSRF,请在 <security:http> 中尝试此操作元素:

<security:csrf disabled="true"/>

但是,正如我之前所说,设置 springSecurityFilterChain 的基本路径对于这样一个特定的路径/rs/self/* ,我不认为这是一个好主意。更具体地说,它根本没有任何意义。根据你的<security:http>元素,看来您想保护应用程序中的所有网址( <intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')"/> ),但您的 springSecurityFilterChain仅当 url 匹配 `/rs/self/*' 时才会触发。

请尝试这样:

Web.xml:

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

安全.xml:

<http pattern="/rs/login" security="none"/>
<http>
<intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')"/>
<form-login login-page="/rs/login" login-processing-url="/j_spring_security_check"
authentication-failure-url="/login" />
<logout logout-success-url="/login"/>
<csrf disabled="true"/>
</http>

注销成功 url 与登录表单 url 不匹配,但您似乎希望相同。如果你想要这种方式,请将其更改为 <logout logout-success-url="/rs/login" />

编辑2:截至您最后的评论,我认为最后剩下的问题是在 jsp 表单操作中。尝试以这种方式更改操作:

<form method="post" action="/your_app_context/j_spring_security_check" >

关于java - 无法使用 spring security 进行身份验证。输入凭据后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44491901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com