JonnyRaa
JonnyRaa

Reputation: 8038

Providing anonymous access to request-mapping/controller in spring

I'm struggling to figure out what the problem is.

I'm working on a spring web application that also has a mobile application. The mobile app needs to talk to our server to refresh the web clients.

I thought the easiest way to do this would be using a controller set up with a request mapping.

The mobile app has it's own authentication system so access to the url needs to be open/anonymous.

I've got the following set up:

@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 "";
    }
}

In my security context I have tried both :

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

and

<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>

If I try and curl the url it gives me a 404 Not found.

In the logs when the server runs I get this message:

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

Which would seem to indicate that the request mapping is working as expected.

Before switching off security on that request I was getting the login form as a response to curling. So that seems to indicate the security switchoff worked aswell.

I'm a bit out of ideas! Help would be appreciated. Cheers

Update

I'm using the following curl:

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

The dispatcher-servlet.xml looks like this:

<?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>

the dispatcher servlet is also referenced in here:

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>

Im fairly new to spring and haven't had to mess around with the web.xml so far but the thing that sticks out there is:

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

What is that saying?

Upvotes: 1

Views: 1655

Answers (1)

geoand
geoand

Reputation: 64059

Since you have deployed the Spring Dispatcher Servlet under /api/* all your Spring controller URLs will be under that. So in your test case just do:

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

Upvotes: 1

Related Questions