spring 安全和 mvc

spring security and mvc

我正在使用 spring 安全和一些 mvc 控制器。我无法让它工作。以下是我得到的错误。

org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/auth/ping] in DispatcherServlet with name 'application'

如果有人能找出我的代码有什么问题就太好了

下面是我的文件

web.xml

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>Out of school network backend</display-name>


    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.properties</param-value>
    </context-param>


    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/application-context.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- CORS Filter -->
    <filter>
        <filter-name>corsFilter</filter-name>
        <filter-class>com.myproj.filters.SimpleCORSFilter</filter-class>
    </filter>

    <!-- Spring Security Filter -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

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

    <servlet>
        <servlet-name>application</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>

    <servlet-mapping>
        <servlet-name>application</servlet-name>
        <url-pattern>/</url-pattern>
     </servlet-mapping>
</web-app>

以下是我的应用上下文:

<?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:context="http://www.springframework.org/schema/context" 
        xmlns:sec="http://www.springframework.org/schema/security"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:mongo="http://www.springframework.org/schema/data/mongo"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd">


    <context:annotation-config />
    <context:component-scan base-package="com.myproj.controllers" />
    <mvc:annotation-driven />

    <!-- mongodb -->
    <mongo:db-factory id="mongoDbFactory"
                  host="<ip address>"
                  port="27017"
                  dbname="mongotest"
                  username="username"
                  password="password"/>


    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg ref="mongoDbFactory" />
    </bean>    
    <mongo:repositories base-package="com.myproj.repositories" />

    <!-- Global Security Settings -->
    <sec:global-method-security pre-post-annotations="enabled" />

    <sec:http use-expressions="true" disable-url-rewriting="true" auto-config="true"
        entry-point-ref="authenticationEntryPoint"
        authentication-manager-ref="authenticationManager">
        <sec:intercept-url pattern="/auth/*" access="isAnonymous()" />
        <sec:intercept-url method="POST" pattern="/be/user" access="isAnonymous()" />
        <sec:intercept-url pattern="/home" access="isAnonymous()" />
        <sec:intercept-url pattern="/api/*" access="hasRole('ROLE_USER')" />
        <sec:form-login login-processing-url="/auth/login"
                        default-target-url="/account" 
                        username-parameter="username" 
                        password-parameter="password" 
                        authentication-failure-url="/login?loginError"/>
         <sec:logout logout-success-url="/login?logout" /> 
    </sec:http>

    <bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint" />

    <bean id="userDetailsService" class="com.myproj.security.UserDetailsServiceImpl" />

    <sec:authentication-manager alias="authenticationManager">
        <sec:authentication-provider user-service-ref="userDetailsService" />
    </sec:authentication-manager>

下面是控制器:

package com.myproj.controllers;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpStatus;

public class PingController {

    @RequestMapping(value = "auth/ping", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public @ResponseBody String login() {
        return "unprotected";
    }

    @RequestMapping(value = "api/protected", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public @ResponseBody String getpro() {
        return "protected";
    }
}

您在 PingController class 上忘记了 @Controller