0

I am trying to access a SecurityContext object in a @Service that is triggered by an @Schedule configuration in a Spring 4 application. This is non-functional within a @Service but works fine for @Controller configurations.

How do I load the same security context reference in my @Service?

The code I am using to access the context is simply:

System.out.println(SecurityContextHolder.getContext());

The output is pretty simple:

  • Out of @Controller:

    org.springframework.security.core.context.SecurityContextImpl@ffffffff: Null authentication

  • In @Controller:

    org.springframework.security.core.context.SecurityContextImpl@cf8b0421: Authentication: ....@cf8b0421: Principal: ....@1cd97f9; Credentials: [PROTECTED]; Authenticated: false; Details: ....t@60b45d5b; Granted Authorities: ....

My web.xml is:

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

  <display-name>MyAPp</display-name>
  <description>TBD</description>

  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
      <param-name>contextAttribute</param-name>
      <param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <servlet-name>spring</servlet-name>
  </filter-mapping>

  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

My applicationContext.xml file looks like:

<?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="...">
  <thirdp:oauth>
    <thirdp:oauthInfo endpoint="${login.endpoint}"
                      oauth-key="${login.key}"
                      oauth-secret="${login.secret}"/>
  </thirdp:oauth>

  <security:http use-expressions="true">
    <security:intercept-url pattern="/*" access="isAuthenticated()" />
  </security:http>

  <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
        p:targetClass="org.springframework.security.core.context.SecurityContextHolder"
        p:targetMethod="setStrategyName"
        p:arguments="MODE_GLOBAL" />
</beans>
el n00b
  • 1,957
  • 7
  • 37
  • 64
  • How would an `@Scheduled` method be able to authenticate a user? How would an authentication processor even know which user was trying to do something? If you can answer those questions you can probably find a way to set up the security context. – Dave Syer Jul 20 '14 at 06:47
  • I changed the model to use a home-grown solution for performing the same authentication. It was all background authentication necessary for automated processing. I just added a pre-approval process to load the OAuth2 information I needed. – el n00b Jul 24 '14 at 12:10

0 Answers0