我正在使用Spring Security OAuth2创建自己的授权服务器。就我而言,我想使Angular客户端(SPA)使用授权代码授予。
客户端可以使用oauth/authorize
端点,用户可以登录,浏览器将重定向到SPA。现在,客户希望通过oauth/token
获取令牌。但是此端点是安全的,并且需要客户端ID和客户端机密。默认情况下,Spring会启用安全性。
在docs中,我可以找到以下内容:
默认情况下,Spring OAuth在@Configuration支持中使用客户端密钥的HTTP Basic身份验证为您保护令牌终结点。在XML中不是这种情况(因此应该对其进行显式保护)。
据我所知,公共客户中不应使用客户机密。但这意味着oauth/token
端点不安全。
问题:为oauth/token
禁用身份验证是一种好习惯吗?如果没有,我该如何解决?
这是我的WebSecurityConfig:
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
@Override
protected UserDetailsService userDetailsService() {
// WARN: Do not use the default password encoder in production environments!
return new InMemoryUserDetailsManager(
User.withDefaultPasswordEncoder()
.username("user-a")
.password("password")
.roles("USER_ROLE")
.build()
);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http = http
.requiresChannel()
.anyRequest()
.requiresSecure()
.and()
.cors()
.and()
.authorizeRequests()
.antMatchers("/.well-known/**")
.permitAll()
.and();
super.configure(http);
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(singletonList("*"));
configuration.setAllowedMethods(asList("GET","POST"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}