我正在尝试定义两个不同的bean(都扩展了AbstractPreAuthenticatedProcessingFilter):一个用于在“开发”配置文件处于活动状态时从请求(例如USER_ID)中获取标头,第二个在请求时从请求标头中获取JWT。 “开发”资料无效 。(尽管从概念上讲,我实际上只是在尝试基于bean本身的存在来以编程方式注册过滤器)目前,我什至没有尝试使用配置文件,因为我遇到了自动将标头自动设置的问题在相应的过滤器链中注册。
该应用程序使用配置为使用嵌入式Tomcat的Spring-Boot 2.0.0.RELEASE,并将该服务注释为@RestController。以下是我的SecurityConfig类的简化版本:
@Configuration
@EnableWebSecurity(debug=true)
@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public UserIdAuthenticationFilter UserIdAuthenticationFilter() throws Exception {
...
}
@Bean
public JwtAuthenticationFilter jwtAuthenticationFilter() throws Exception {
...
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors().and()
.csrf().disable()
.headers().frameOptions().sameOrigin()
.and()
//.addFilter(jwtAuthenticationFilter())
//.addFilter(UserIdAuthenticationFilter())
.exceptionHandling()
.and().authorizeRequests()
.antMatchers("/", "/index.html", "/css/**", "/images/**", "/js/**").permitAll()
.anyRequest()
.authenticated()
.antMatchers("/**").permitAll()
;
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我已经定义了我的过滤器bean,并且将它们应用到正确的链上时可以工作...我看到的问题是spring似乎在某个地方注册了过滤器,但是当我致电服务时端点,它从不调用我添加的过滤器代码。
运行应用程序的日志输出似乎表明过滤器正在定位...
2018-04-04 09:43:02.907 INFO 7717 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean …Run Code Online (Sandbox Code Playgroud) 我正在使用RESTEasy 2.2.1.GA作为我的JAX-RS实现来创建连接到第三方服务提供商的客户端.(如果重要的话,Education.com的REST API)
为了确保我没有错过重要的实现细节,这里有代码示例:
服务接口
@Path("/")
public interface SchoolSearch {
@GET
@Produces({MediaType.APPLICATION_XML})
Collection<SchoolType> getSchoolsByZipCode(@QueryParam("postalcode") int postalCode);
}
Run Code Online (Sandbox Code Playgroud)
打电话给班级
public class SimpleSchoolSearch {
public static final String SITE_URL = "http://api.education.com/service/service.php?f=schoolSearch&key=****&sn=sf&v=4";
SchoolSearch service = ProxyFactory.create(SchoolSearch.class, SITE_URL);
public Collection<SchoolType> getSchools() throws Exception {
Collection<SchoolType> schools = new ArrayList<SchoolType>();
Collection<SchoolType> response = service.getSchoolsByZipCode(35803);
schools.addAll(response);
return schools;
}
}
Run Code Online (Sandbox Code Playgroud)
在设置测试以进行此调用之后,我执行并看到抛出以下异常.
org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException: Unable to find JAXBContext for media type: text/html;charset="UTF-8"
Run Code Online (Sandbox Code Playgroud)
从读取RESTEasy/JAX-RS文档,据我所知,当响应返回给客户端时,在解组数据之前,确定(内容协商??)关于使用哪种机制进行解组.(我想我们在这里谈论的是一个MessageBodyReader,但我不确定.)从查看响应的主体,我看到返回的是格式正确的XML,但是内容协商(通过HTTP头内容类型是确实是text/html; charset ="UTF-8")不允许JAXB解析文本.
我认为实现是正确的行为,并且它是错误的服务,但是,我不控制服务,但仍然想要使用它.
所以说:
我是否理解为什么会抛出异常?
我该如何解决?
是否有一个简单的一行注释可以强制JAXB解组数据,还是需要实现一个自定义的MessageBodyReader?(如果这甚至是正确的类来实现).
谢谢!
跟进:
我只是想对艾登的答案发表一些变化.我使用他的代码和Resteasy …