SpringBoot采用Spring Security集成oauth2登录时,默认地址为/oauth/token,如果想要自定义地址,比如加前缀后,再次调用时就报错,报错信息如下:
程序异常:org.springframework.security.authentication.InsufficientAuthenticationException: There is no client authentication. Try adding an appropriate authentication filter.
改这个地址的起因是两个项目希望共用一个地址,如matecloud微服务模块使用mate-uaa/oauth/token调用,springboot默认就是/oauth/token方式,希望共用一个,如果直接修改,则报上述错误。
在AuthorizationServerConfigurerAdapter的设置内置端点EndPoint路径时自定义pathMapping,代码如下:
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
// token增强链
TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
// 把jwt增强,与额外信息增强加入到增强链
tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhancer(), jwtAccessTokenConverter()));
endpoints
.authenticationManager(authenticationManager)
.tokenEnhancer(tokenEnhancerChain)
.accessTokenConverter(jwtAccessTokenConverter())
.userDetailsService(userDetailsService)
.tokenGranter(tokenGranter(endpoints))
.tokenStore(redisTokenStore())
.reuseRefreshTokens(false)
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST,
HttpMethod.OPTIONS, HttpMethod.PUT, HttpMethod.PATCH, HttpMethod.DELETE)
.pathMapping("/oauth/token", "/mate-uaa/oauth/token");
}
重点关注最后两行代码。