具有Web安全编程配置的Spring引导Web服务
我已经编写了一个简单的SpringWeb服务,在我尝试为用户身份验证启用web安全性之前,它可以正常工作 我已经以编程方式为项目设置了所有spring配置(我第一次尝试这种方法),我使用spring boot启动web服务(也是我第一次尝试spring boot),为此,我使用以下spring API(spring boot starter ws:1.1+和spring security xxx:3.+) Web服务端点的定义如下:具有Web安全编程配置的Spring引导Web服务,spring,spring-security,spring-boot,Spring,Spring Security,Spring Boot,我已经编写了一个简单的SpringWeb服务,在我尝试为用户身份验证启用web安全性之前,它可以正常工作 我已经以编程方式为项目设置了所有spring配置(我第一次尝试这种方法),我使用spring boot启动web服务(也是我第一次尝试spring boot),为此,我使用以下spring API(spring boot starter ws:1.1+和spring security xxx:3.+) Web服务端点的定义如下: @Endpoint public class AWebServ
@Endpoint
public class AWebServiceEndpoint extends WsConfigurerAdapter {
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "aRequest")
@ResponsePayload
public AResponse request(@RequestPayload ARequest request) {
...
@Configuration
@EnableWs
@ComponentScan
public class WebServiceConfig extends WsConfigurerAdapter {
@Bean
public ServletRegistrationBean dispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
return new ServletRegistrationBean(servlet, "/ws/A/Service");
}
....
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests().antMatchers("/**").hasRole("alogin").and().httpBasic();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("testuser").password("password").roles("alogin");
}
}
Web服务配置定义如下:
@Endpoint
public class AWebServiceEndpoint extends WsConfigurerAdapter {
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "aRequest")
@ResponsePayload
public AResponse request(@RequestPayload ARequest request) {
...
@Configuration
@EnableWs
@ComponentScan
public class WebServiceConfig extends WsConfigurerAdapter {
@Bean
public ServletRegistrationBean dispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
return new ServletRegistrationBean(servlet, "/ws/A/Service");
}
....
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests().antMatchers("/**").hasRole("alogin").and().httpBasic();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("testuser").password("password").roles("alogin");
}
}
Web安全配置的定义如下:
@Endpoint
public class AWebServiceEndpoint extends WsConfigurerAdapter {
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "aRequest")
@ResponsePayload
public AResponse request(@RequestPayload ARequest request) {
...
@Configuration
@EnableWs
@ComponentScan
public class WebServiceConfig extends WsConfigurerAdapter {
@Bean
public ServletRegistrationBean dispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
return new ServletRegistrationBean(servlet, "/ws/A/Service");
}
....
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests().antMatchers("/**").hasRole("alogin").and().httpBasic();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("testuser").password("password").roles("alogin");
}
}
当我调用配置了Web安全性的Web服务时,我得到以下spring debug stacktrace:
04-Jul-2014 12:21:07084[DEBUG]HttpServletBean-初始化servlet'MessageDispatchersServlet'
2014年7月4日12:21:07085[DEBUG]可变属性源-添加搜索优先级最低的[servletConfigInitParams]属性源
2014年7月4日12:21:07085[DEBUG]可变属性源-添加搜索优先级最低的[servletContextInitParams]属性源
04-Jul-2014 12:21:07085[DEBUG]可变属性源-添加搜索优先级最低的[JNDiProperty]属性源
2014年7月4日12:21:07085[DEBUG]可变属性源-添加搜索优先级最低的[systemProperties]属性源
2014年7月4日12:21:07085[DEBUG]可变属性源-添加搜索优先级最低的[systemEnvironment]属性源
2014年7月4日12:21:07085[DEBUG]AbstractEnvironment-初始化的标准ServleteEnvironment和PropertySources[servletConfigInitParams,servletContextInitParams,JNDiProperty,systemProperties,systemEnvironment]
2014年7月4日12:21:07086[信息]FrameworkServlet-FrameworkServlet“messageDispatcherServlet”:初始化已开始
2014年7月4日12:21:07087[调试]AbstractBeanFactory-返回单例bean“messageFactory”的缓存实例
2014年7月4日12:21:07093[DEBUG]AbstractBeanFactory-返回单例bean“defaultMethodEndpointAdapter”的缓存实例
2014年7月4日12:21:07093[DEBUG]AbstractBeanFactory-返回单例bean“soapFaultAnnotationExceptionResolver”的缓存实例
2014年7月4日12:21:07093[调试]AbstractBeanFactory-返回单例bean“SimpleSapeExceptionResolver”的缓存实例
2014年7月4日12:21:07093[调试]AbstractBeanFactory-返回单例bean“payloadRootAnnotationMethodEndpointMapping”的缓存实例
04-Jul-2014 12:21:07093[DEBUG]AbstractBeanFactory-返回单例bean“soapActionAnnotationMethodEndpointMapping”的缓存实例
2014年7月4日12:21:07094[DEBUG]AbstractBeanFactory-返回单例bean“annotationActionEndpointMapping”的缓存实例
2014年7月4日12:21:07094[调试]MessageDispatcherServlet-在servlet“MessageDispatcherServlet”中未找到MessageDispatcher:使用默认设置
2014年7月4日12:21:07094[DEBUG]AbstractBeanFactory-返回单例bean的缓存实例'request'
2014年7月4日12:21:07094[调试]MessageDispatcherServlet-已发布[org.springframework.ws.wsdl.wsdl11]。DefaultWsdl11Definition@7ec50147]as request.wsdl
2014年7月4日12:21:07095[调试]AbstractBeanFactory-返回单例bean“requestSchema”的缓存实例
2014年7月4日12:21:07095[DEBUG]MessageDispatcherServlet-发布[SimpleXsdSchema]{http://www.destin8.co.uk/Chief}]as requestSchema.xsd
2014年7月4日12:21:07095[DEBUG]FrameworkServlet-已发布servlet“MessageDispatchersServlet”的WebApplicationContext,作为名为[org.springframework.web.servlet.FrameworkServlet.CONTEXT.MessageDispatchersServlet]的ServletContext属性
2014年7月4日12:21:07095[信息]FrameworkServlet-FrameworkServlet“messageDispatcherServlet”:初始化在9毫秒内完成
2014年7月4日12:21:07095[调试]HttpServletBean-Servlet“MessageDispatchersServlet”配置成功
2014年7月4日12:21:07102[调试]过滤器链Proxy$VirtualFilterChain-/ws/A/服务位于附加过滤器链中11个位置中的第1个位置;正在启动筛选器:“WebAsyncManagerIntegrationFilter”
2014年7月4日12:21:07105[调试]过滤器链Proxy$VirtualFilterChain-/ws/A/服务位于附加过滤器链中11个位置中的第2个位置;正在启动筛选器:“SecurityContextPersistenceFilter”
2014年7月4日12:21:07106[调试]HttpSessionSecurityContextRepository-目前不存在HttpSession
2014年7月4日12:21:07106[调试]HttpSessionSecurityContextRepository-HttpSession中没有可用的SecurityContext:null。将创建一个新的。
2014年7月4日12:21:07108[调试]过滤器链Proxy$VirtualFilterChain-/ws/A/服务位于附加过滤器链中11个位置中的第3个位置;触发过滤器:“HeaderWriterFilter”
2014年7月4日12:21:07108[调试]HstsHeaderWriter-未注入HSTS标头,因为它与requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter不匹配$SecureRequestMatcher@2d5260f3
2014年7月4日12:21:07108[调试]过滤器链Proxy$VirtualFilterChain-/ws/A/服务位于附加过滤器链中11个位置中的第4个位置;正在启动筛选器:“注销筛选器”
2014年7月4日12:21:07108[调试]AntPathRequestMatcher-检查请求的匹配:'/ws/a/service';针对“/注销”
2014年7月4日12:21:07108[调试]过滤器链Proxy$VirtualFilterChain-/ws/A/服务位于附加过滤器链中11个位置中的第5个位置;触发筛选器:“基本身份验证筛选器”
2014年7月4日12:21:07109[调试]过滤器链Proxy$VirtualFilterChain-/ws/A/服务位于附加过滤器链中11个位置中的第6个位置;正在启动筛选器:“RequestCacheAwarRefilter”
2014年7月4日12:21:07109[调试]过滤器链Proxy$VirtualFilterChain-/ws/A/服务位于附加过滤器链中11个位置中的第7个位置;正在启动筛选器:“SecurityContextHolderAwareRequestFilter”
2014年7月4日12:21:07110[调试]过滤器链Proxy$VirtualFilterChain-/ws/A/服务位于附加过滤器链中11个位置中的第8个位置;正在启动筛选器:“匿名身份验证筛选器”