【发布时间】:2021-07-17 16:45:30
【问题描述】:
我想知道,是否有办法在 JDBCTokenStore 中添加更多信息。让我解释一下,目前,我正在使用 oauth2 身份验证通过 JDBCTokenStore 保护我的 Spring Boot REST API。
这是我正在使用的 AuthorizationServerConfiguration 类:
@EnableAuthorizationServer
@Configuration
public class AuthorizationServerConfiguration implements AuthorizationServerConfigurer {
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private DataSource dataSource;
@Autowired
private AuthenticationManager authenticationManager;
@Bean
TokenStore jdbcTokenStore() {
return new JdbcTokenStore(dataSource);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.checkTokenAccess("isAuthenticated()").tokenKeyAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.jdbc(dataSource).passwordEncoder(passwordEncoder);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(jdbcTokenStore());
endpoints.authenticationManager(authenticationManager);
}
@Bean
public FilterRegistrationBean<CorsFilter> corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<CorsFilter>(new CorsFilter(source));
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return bean;
}
}
当我通过 POSTMAN 访问 POST 方法 URL 以获取 OAUTH2 令牌 http://localhost:8082/oauth/token 时,我得到了这个 json 响应:
{
"access_token": "e71b98dd-8a53-44f4-9673-fa4c1c54a415",
"token_type": "bearer",
"refresh_token": "ac4e82ec-028a-45a6-8801-fe55fcbdd36e",
"expires_in": 3042,
"scope": "READ WRITE"
}
是否有解决方案可以在我的 Json JDBCTokenStore 中添加有关用户的更多信息(如电子邮件、性别...等)? 显然,TokenEnhancerChain 或 JWT 是我的问题的解决方案,除了我无法在不进行太多修改的情况下将它们集成到我的代码中。
提前谢谢你
【问题讨论】:
标签: mysql spring-boot maven oauth-2.0