我最近开始学习 spring security 并尝试将其合并到我现有的 Web 应用程序中。该应用程序配置简单,所以我很困惑我在哪里搞砸了。
我的 web.xml
<!-- FilterChain proxy for security -->
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appname-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>nistreq</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appname-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>appname</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>mainpage.jsp</welcome-file>
</welcome-file-list>
和 security-config.xml
<security:http access-denied-page="/denied.jsp" use-expressions="true">
<security:form-login login-page="/login.jsp"
authentication-failure-url="/login.jsp?login_error=true" />
<security:intercept-url pattern="/*" access="isAuthenticated()"/>
<security:logout/>
</security:http>
我为 springSecurityFilterChain ("/")、servlet 映射的 URL 模式 ("/") 和 security:intercept-url 模式 ("/").这会导致重定向循环。我经历了无数次移动术语的变体,将内容推送到子包中以避免保护应用程序根等。我总是以 404 的抓包、重定向循环等结束。
我正在做一些非常愚蠢的事情,希望能有另一双眼睛。感谢您的任何见解...
最佳答案
我相信您只需要为您的登录页面添加一个异常(exception)。除了您在 security-config.xml 中的内容之外,添加以下内容:
<security:http pattern="/login.jsp" security="none" />
无论您尝试转到什么 URL,它都没有经过身份验证,因此它会尝试转到登录页面。但是,这也没有经过身份验证,所以它只是旋转和旋转。您的登录页面需要无需经过身份验证即可访问。
关于java - 导致重定向循环的 Spring Security 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11369467/