【发布时间】:2018-11-23 05:57:16
【问题描述】:
在我的 Spring Security 应用程序中,我能够使用邮递员从我的“/oauth/token”端点成功获取 JWT 令牌。但是,我希望任何希望进行身份验证的人都可以访问 /oauth/endpoint,而无需 clientId 和 secret。
我的安全服务器
@Configuration
@EnableWebSecurity
public class GatewaySecurityConfigurer extends WebSecurityConfigurerAdapter {
@Autowired
private GatewayUserDetailsService gatewayUserDetailsService;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public DaoAuthenticationProvider daoAuthenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
daoAuthenticationProvider.setUserDetailsService(gatewayUserDetailsService);
return daoAuthenticationProvider;
}
@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) {
authenticationManagerBuilder.authenticationProvider(daoAuthenticationProvider());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/oauth/token").permitAll()
.antMatchers("actuator/**").fullyAuthenticated();
}
}
和我的资源服务器
@EnableResourceServer
@Configuration
@Import(GatewaySecurityConfigurer.class)
public class GatewayResourceServerConfigurer extends ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer config) {
config.tokenServices(tokenServices());
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/oauth/token").permitAll()
.antMatchers("actuator/**").fullyAuthenticated();
}
@Bean
public TokenStore tokenStore() {
TokenStore store = new JwtTokenStore(accessTokenConverter());
return store;
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey("123");
return converter;
}
@Bean
public DefaultTokenServices tokenServices() {
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(tokenStore());
defaultTokenServices.setSupportRefreshToken(true);
return defaultTokenServices;
}
}
正如您在这两个类中看到的那样,我尝试实现一个 HttpSecurity 配置,该配置将允许对 /oauth/token 的所有请求,这些请求具有需要验证的其他路径。但是,当我在没有 clientId 和秘密的情况下发布时,我的邮递员请求中出现了 401 空 http 响应。
我怎样才能让我的代码不需要 clientId 和 Secret 进行身份验证?
【问题讨论】:
标签: java spring authentication spring-security oauth