我正在尝试访问{4}中SecurityContext
对象,该对象由Spring 4应用程序中的@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