【问题标题】:j_security_check is not availablej_security_check 不可用
【发布时间】:2012-01-20 00:19:43
【问题描述】:

我正在使用 spring 3.0 并试图让我的登录页面正常工作,但我收到此错误

The requested resource (/myapp/j_spring_security_check) is not available.

我发布了我所有与安全有关的文件的内容。希望这就足够了。如果需要,我可以展示更多。

这是我的 web.xml

    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<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>*.html</url-pattern>
</filter-mapping>

我的登录页面:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Login Page</title>
<style>
.errorblock {
color: #ff0000;
background-color: #ffEEEE;
border: 3px solid #ff0000;
padding: 8px;
margin: 16px;
}
</style>
</head>
<body onload='document.f.j_username.focus();'>
<h3>Login with Username and Password (Custom Page)</h3>

<c:if test="${not empty error}">
    <div class="errorblock">
        Your login attempt was not successful, try again.<br /> Caused :
        ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
    </div>
</c:if>

<form name='f' action="<c:url value='j_spring_security_check' />"
    method='POST'>

    <table>
        <tr>
            <td>User:</td>
            <td><input type='text' name='j_username' value=''>
            </td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input type='password' name='j_password' />
            </td>
        </tr>
        <tr>
            <td colspan='2'><input name="submit" type="submit"
                value="submit" />
            </td>
        </tr>
        <tr>
            <td colspan='2'><input name="reset" type="reset" />
            </td>
        </tr>
    </table>

</form>

和我的安全上下文

<beans:bean id="passwordEncoder"
    class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">
    <beans:constructor-arg value="256" />
</beans:bean>

<beans:bean id="saltSource"
    class="org.springframework.security.authentication.dao.ReflectionSaltSource">
    <beans:property name="userPropertyToUse" value="id" />
</beans:bean>

<authentication-manager alias="authenticationManager">
    <authentication-provider ref="authProvider" />
</authentication-manager>

<beans:bean id="authProvider"
    class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
    <beans:property name="userDetailsService" ref="personService" />
    <beans:property name="passwordEncoder" ref="passwordEncoder" />
    <beans:property name="saltSource" ref="saltSource" />
    <beans:property name="includeDetailsObject" value="true" />
</beans:bean>

<beans:bean id="authenticationProcessingFilterEntryPoint"
    class="org.springframework.security.web.authentication.AuthenticationProcessingFilterEntryPoint">
    <beans:property name="forceHttps" value="false" />
    <beans:property name="loginFormUrl" value="/login.html" />
</beans:bean>
<http entry-point-ref="authenticationProcessingFilterEntryPoint" auto-config="true">
        <intercept-url pattern="/**" access="ROLE_USER" />
        <intercept-url pattern="/login.html" filters="none" />
        <form-login login-page="/login.html" default-target-url="/welcome.html"
            authentication-failure-url="/loginfailed.html" />
        <logout logout-success-url="/logout" />
    </http>

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 已解决!在我的jsp中,我必须添加“.html”并重定向开始工作
  • 我相信您必须添加 .html 的原因是因为您将 spring 安全过滤器链映射到 *.html。这也意味着 spring 安全性通常只会应用于 *.html 请求而不是所有请求。如果这是你的意图,很好,如果不是,你可能想看看你的 springSecurityFilterChain 的 url-pattern

标签: java forms security spring login


【解决方案1】:

.html 绝对是过滤器的设置方式所必需的。正确的方法是将 web.xml 中的 url-pattern 声明为 /*。

如果您要将 url-pattern 保留为 *.html,可能值得考虑将 login-processing-url 属性添加到您的 form-login 元素,这样如果有人返回并修改 web.xml一切都不会因为这个怪癖而崩溃。

<form-login login-page="/login.html" default-target-url="/welcome.html"
        authentication-failure-url="/loginfailed.html" login-processing-url="j_spring_security_check.html"/>

【讨论】:

    猜你喜欢
    • 2012-06-05
    • 2016-06-15
    • 2013-05-21
    • 1970-01-01
    • 2012-05-30
    • 2012-11-17
    • 2013-05-25
    • 2010-09-12
    • 2013-03-30
    相关资源
    最近更新 更多