0

我正在尝试访问由Spring 4 应用程序中的配置触发的SecurityContext对象。这在 a 中是非功能性的,但适用于配置。@Service@Schedule@Service@Controller

如何在我的 ? 中加载相同的安全上下文引用@Service

我用来访问上下文的代码很简单:

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

输出非常简单:

  • @Controller

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

  • @Controller

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

web.xml的是:

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

我的applicationContext.xml文件看起来像:

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

0 回答 0