Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc Spring安全重定向到404页面问题_Spring Mvc_Spring Security - Fatal编程技术网

Spring mvc Spring安全重定向到404页面问题

Spring mvc Spring安全重定向到404页面问题,spring-mvc,spring-security,Spring Mvc,Spring Security,我尝试在我的webapp上应用spring安全性,请求用户在使用该应用之前创建或登录帐户。但是,该页面不能显示登录或注册页面,只能显示404页面。我一直试着四处看看,但仍然找不到问题的根源。有人能帮我检测一下这个问题吗 这是我的web.xml <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </l

我尝试在我的webapp上应用spring安全性,请求用户在使用该应用之前创建或登录帐户。但是,该页面不能显示登录或注册页面,只能显示404页面。我一直试着四处看看,但仍然找不到问题的根源。有人能帮我检测一下这个问题吗

这是我的web.xml

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring-servlet.xml,
            classpath:spring-security.xml
        </param-value>
    </context-param>

    <context-param>
        <param-name>defaultHtmlEscape</param-name>
        <param-value>true</param-value>
    </context-param>


    <!-- Ensure one session per request
    -->
    <filter>
        <filter-name>hibernateFilter</filter-name>
            <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
            <init-param>
             <param-name>sessionFactoryBeanName</param-name>
             <param-value>sessionFactory</param-value>         
        </init-param>      
    </filter>

    <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping> 

    <!-- 
    <filter>
        <filter-name>encoding-filter</filter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>

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

    <!-- 
        Enables Spring 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>

    <!-- 
        Direct Servlet Dispatcher beans
    -->
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>


    <!-- Restrict dispatcher views -->
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


    <!-- Handle NotFound Error Page -->
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/views/errors/404.jsp</location>
    </error-page>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

你能发布负责登录和注册的控制器类吗?也许这是一个愚蠢的问题,但是你在你的应用程序中写了/login页面了吗?您还可以编写login.html,然后编写html页面(不带控制器)。我刚刚用login和register controller更新了代码。我在WEB-INF/views/I下还有3页login.jsp register.jsp和dashboard.jsp,找到了原因。我忘记将ViewResolver更改为/WEB-INF/views。谢谢你抽出时间。
<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-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.2.xsd">


    <!-- -->
    <http auto-config="true"  use-expressions="true">
        <intercept-url pattern="/login" access="permitAll"/>
        <intercept-url pattern="/logout" access="permitAll"/>
        <intercept-url pattern="/register" access="permitAll"/>
        <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
        <intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" />

        <form-login login-page="/login" 
            authentication-failure-url="/login?error" 
            default-target-url="/dashboard"
            username-parameter="email"
            password-parameter="password"/>

        <access-denied-handler error-page="/login?error"/>

        <logout invalidate-session="true" 
            logout-success-url="/login" 
            logout-url="/j_spring_security_logout"
            delete-cookies="JSESSIONID"/>

        <session-management invalid-session-url="/login">
            <concurrency-control max-sessions="1" expired-url="/login"/>
        </session-management>

        <!-- 
        <remember-me token-validity-seconds="1209600"
                    remember-me-parameter="remember-me"/>
         -->            
    </http>

    <authentication-manager>
        <authentication-provider user-service-ref="loginManager" >
            <password-encoder hash="bcrypt" />
        </authentication-provider>
    </authentication-manager>


</beans:beans>
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org.schema/cache/spring-cache.xsd">



    <context:component-scan base-package="com.isad" />
    <mvc:annotation-driven />
    <mvc:default-servlet-handler/>
    <!-- 
    <mvc:resources mapping="/resources/**" location="/WEB-INF/" cache-period="31556926"/>
     -->
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <!-- 
        Initialize base viewers 
    --> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>


    <!-- 
        Enable Data Transaction to Database. 
    -->
    <bean id="sessionFactory" scope="singleton"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id ="transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name = "sessionFactory" ref = "sessionFactory"/>
    </bean>   
</beans>
@Controller
public class RegisterController {

    @Autowired private RegisterService regManager;

    @RequestMapping(value="/register", method=RequestMethod.GET)
    public String showRegisterPage() {
        return "register";
    }


    @RequestMapping(value="/register", method=RequestMethod.POST)
    public String registerForm(@RequestParam(value="email") final String email,
                        @RequestParam(value="password") final String password) {
        regManager.registerAccount(email, password);
        return "redirect:/dashboard";
    }
}


@Controller
public class LoginController {

    @RequestMapping(value="/login", method=RequestMethod.GET)
    public String showLoginForm() {
        return "login";
    }
}