SpringBoot 1.5.x + Security + OAuth2
我有一个带有OAuth2安全性的Spring Boot REST API。
今天我将spring-boot-starter-parent
从1.4.2
升级到1.5.2
。
变化让我很困惑。
之前,我可以使用Postman测试我的REST API。 当我的访问令牌不正确或我没有特定资源的权限时,服务器响应如下:
{ "error": "access_denied", "error_description": "Access is denied" }
现在它一直将我重定向到/login
页面…当我登录时 – 它显示我的资源而没有任何OAuth2身份validation…
我试图禁用它,我发现了这个神奇的属性:
security.oauth2.resource.filter-order = 3
该行关闭重定向到登录页面。
但是,我的问题是:
- 这两个版本在安全方面发生了什么?
- 这个“奇怪”的行是唯一有效的修复方法吗?
- 这个登录页面的目的是什么以及它正在使用什么身份validation(我检查了Google Chrome中的请求和响应,我看不到任何访问令牌和oauth2的东西,所以它只使用用户存储库?)
我的代码中一些更重要的部分:
的pom.xml
org.springframework.boot spring-boot-starter-parent 1.5.2.RELEASE 2.1.0.RELEASE org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-actuator org.springframework.boot spring-boot-starter-security org.springframework.security.oauth spring-security-oauth2 ${spring-security-oauth.version}
application.properties
#other properties security.oauth2.resource.filter-order = 3
OAuth2.java
public class OAuth2 { @EnableAuthorizationServer @Configuration @ComponentScan public static class AuthorizationServer extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManagerBean; @Autowired private UserDetailsService userDetailsService; @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("trusted_client") .authorizedGrantTypes("password", "refresh_token") .scopes("read", "write"); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManagerBean).userDetailsService(userDetailsService); } @Override public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { security.allowFormAuthenticationForClients(); } } @EnableResourceServer @Configuration @ComponentScan public static class ResourceServer extends ResourceServerConfigurerAdapter { @Autowired private RoleHierarchy roleHierarchy; private SecurityExpressionHandler webExpressionHandler() { DefaultWebSecurityExpressionHandler defaultWebSecurityExpressionHandler = new DefaultWebSecurityExpressionHandler(); defaultWebSecurityExpressionHandler.setRoleHierarchy(roleHierarchy); return defaultWebSecurityExpressionHandler; } @Override public void configure(HttpSecurity http) throws Exception { http .authorizeRequests().expressionHandler(webExpressionHandler()) .antMatchers("/api/**").hasRole("DEVELOPER"); } } }
Security.java
@EnableWebSecurity @Configuration @ComponentScan public class Security extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Bean public JpaAccountDetailsService userDetailsService(AccountsRepository accountsRepository) { return new JpaAccountDetailsService(accountsRepository); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Bean public PasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } }
好的,我现在明白了。
@Cleto Gadelha向我指出了非常有用的信息。
但是我觉得发行说明很不清楚或遗漏了一些信息。 除了OAuth2资源filter从3更改为SecurityProperties.ACCESS_OVERRIDE_ORDER - 1
,关键信息是默认WebSecurityConfigurerAdapter
顺序为100 (源) 。
因此,在1.5.x版之前,OAuth2资源服务器顺序为3,其优先级高于 WebSecurityConfigurerAdapter
。
在1.5.x版本之后,OAuth2资源服务器顺序设置为SecurityProperties.ACCESS_OVERRIDE_ORDER - 1
(我认为是Integer.MAX_VALUE - 8
),它现在的优先级明显低于基本的WebSecurityConfigurerAdapter
顺序。
这就是为什么从1.4.x迁移到1.5.x后,我会看到登录页面的原因
因此,更优雅和类似Java的样式解决方案是在WebSecurityConfigurerAdapter
类上设置@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
第一个和第二个问题的答案是Spring Boot 1.5发行说明 :
OAuth 2资源filter
OAuth2资源filter的默认顺序已从3更改为SecurityProperties.ACCESS_OVERRIDE_ORDER – 1.这将其置于执行器端点之后但在基本身份validationfilter链之前。 可以通过设置security.oauth2.resource.filter-order = 3来恢复默认值
/ login页面只是一个弹出重定向未授权用户的路径。 由于您没有使用自定义登录表单并且您的Oauth2filter处于错误位置,因此可能使用的是基本身份validation。