0

我正在尝试创建一个简单的 Spring Security 登录页面,所有配置对我来说似乎都是正确的(实际上尝试了数以千计的不同配置。但失败了)。启动 Tomcat 时会抛出:

java.lang.ClassNotFoundException: org.apache.jsp.WEB_002dINF.pages.welcome_jsp

检查工件后,它似乎welcome.jsp成功加载页面,但是当我尝试登录并提交时,tomcat 尝试重定向并失败:

HTTP Status 404 - /j_spring_security_check

也更改login-processing-url="/j_spring_security_check.action"login-processing-url="/j_spring_security_check"成功登录,但是当我输入错误的用户名时仍然无法重定向错误页面(浏览器上的网址显示:)http://localhost:8080/welcome?login_error=-1

HTTP Status 404 - /welcome

文件夹下存在WEB-INF/pages页面,有 2 个页面index.jspwelcome.jsp.

弹簧配置.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:security="http://www.springframework.org/schema/security"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <security:debug />

    <security:global-method-security jsr250-annotations="enabled" pre-post-annotations="enabled" secured-annotations="enabled" />

    <security:http use-expressions="true" authentication-manager-ref="authManager">
        <security:intercept-url pattern="/index" access="isAuthenticated"/>
        <security:intercept-url pattern="/welcome" access="permitAll"/>
        <security:form-login login-processing-url="/j_spring_security_check.action" login-page="/welcome" default-target-url="/index" authentication-failure-url="/welcome?login_error=-1" />
        <security:logout logout-success-url="/" delete-cookies="JSESSIONID" invalidate-session="true"/>
        <security:remember-me />
        <security:session-management invalid-session-url="/">
            <security:concurrency-control max-sessions="1" error-if-maximum-exceeded="false" />
        </security:session-management>
    </security:http>

    <security:authentication-manager id="authManager">
        <security:authentication-provider>
            <security:jdbc-user-service data-source-ref="dataSource"
                                        users-by-username-query="
                select ..."

                                        authorities-by-username-query="
              select ...  "
                    />
        </security:authentication-provider>
    </security:authentication-manager>

    <context:component-scan base-package="com.ch.core"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="..." />
        <property name="username" value="..." />
        <property name="password" value="..." />
    </bean>

    <mvc:annotation-driven />

    <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>

    <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>

    <mvc:resources mapping="/img/**" location="/img/" cache-period="31556926"/>

    <!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource
    requests to the container's default Servlet -->
    <mvc:default-servlet-handler/>

    <mvc:view-controller path="/welcome" view-name="welcome"></mvc:view-controller>

</beans>

相关welcome.jsp代码:

 <form name='f' class="navbar-form pull-right" action='/j_spring_security_check' method='POST'>
                        <input type='text' name='j_username' value=''>
                        <input type='password' name='j_password'>
                        <button name="submit" type="submit" class="btn btn-small">Log in</button>
                        <label id="rememberMe">
                            <input type="checkbox" name='_spring_security_remember_me'> <h6">Remember me</h6>
                        </label>
                    </form>
                    <c:if test="${not empty param.login_error}">
                        <div class="error">
                            Your login attempt was not successful, try again.<br />
                            Reason: ${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}
                        </div>
                    </c:if>

感谢您的帮助。

弹簧安全 3.1.2、弹簧 WEBMVC 3.2.0、Tomcat 7.0.33。

4

0 回答 0