因此,我终于再次回到研究这个问题,结果发现解决方案几乎和我期望的一样简单。解决方案是有两个
WebSecurityConfigurerAdapter类。此处描述:
http://docs.spring.io/spring-
security/site/docs/3.2.x/reference/htmlsingle/#multiple-
httpsecurity
执行此操作时需要注意两件事:
- 这些
WebSecurityConfigurerAdapter
类必须具有不同的@Order
值。因此,我用注释了其中一个@Order(1)
,迫使在处理HTTP请求时首先对其进行评估。在我的情况下,哪一个首先并不重要,它们必须有所不同。 - 这两种
HttpSecurity
配置需要应用于不同的URL。这是通过使用antMatcher()
每个值来完成的。鉴于提供给的值@RequestMapping
可以是一个URL数组,仍然有可能只有一个REST控制器方法来处理对两个URL的请求。
因此,它们是:
@Configuration@EnableWebSecurity@Order(1)public class APISecurityConfig extends WebSecurityConfigurerAdapter { @Override @Order(1) protected void configure(HttpSecurity http) throws Exception { http.antMatcher("/api/**") .authorizeRequests() .anyRequest().fullyAuthenticated().and() .httpBasic().and() .csrf().disable(); }}和
@Configuration@EnableWebSecuritypublic class UISecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/ui/**").authenticated(); }}


