在春季提供对请求映射/控制器的匿名访问



我很难弄清楚问题出在哪里。

我正在开发一个springweb应用程序,它也有一个移动应用程序。移动应用程序需要与我们的服务器通信以刷新网络客户端。

我认为最简单的方法是使用一个带有请求映射的控制器。

移动应用程序有自己的身份验证系统,因此访问url需要是开放/匿名的。

我已经设置了以下内容:

@Controller
public class UpdateService
{
    @Autowired
    private Config _appConfig;
    @Autowired
    private SubscriptionsHandler _subscriptionsHandler;
    @RequestMapping(value = "/events/update")
    @ResponseBody
    public String UpdateEventList(String token) throws SQLException
    {
        ExternalUpdateHandler updateHandler = new ExternalUpdateHandler(_appConfig, _subscriptionsHandler);
        updateHandler.MaybeUpdateBasedOn(token);
        return "";
    }
}

在我的安全环境中,我尝试了两种:

<http pattern="/events/update" security="none" />

<http auto-config="true" entry-point-ref="authenticationEntryPoint">
    <!-- Allow access to the login page by unauthenticated users -->
    <intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <intercept-url pattern="/events/update" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <intercept-url pattern="/**" access="ROLE_USER" />
    ....
</http>

如果我尝试卷曲url,它会给我一个404"未找到"。

在服务器运行的日志中,我收到以下消息:

2014-06-25 17:23:40,693 [RMI TCP Connection(3)-127.0.0.1] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/events/update],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String ...UpdateService.UpdateEventList(java.lang.String) throws java.sql.SQLException

这似乎表明请求映射正在按预期工作。

在关闭该请求的安全性之前,我收到了登录表单作为对卷曲的响应。因此,这似乎表明安全开关也起了作用。

我有点没主意!我们将不胜感激。干杯

更新

我使用以下卷曲:

curl -i http://localhost:8100/events/update

dispatcher-servlet.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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
</beans>

这里还引用了调度器servlet:

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app id="compass"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">
    <display-name>compass</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <async-supported>true</async-supported>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- Default session timeout -->
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
</web-app>

我对spring还很陌生,到目前为止还没有处理过web.xml,但最突出的是:

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

这是怎么说的?

由于您已经在/api/*下部署了Spring Dispatcher Servlet,因此您的所有Spring控制器URL都将位于该Servlet下。因此,在您的测试用例中,只需执行:

http://localhost:8100/api/events/update

最新更新