我们可以在SpringOAuth2中重写CheckTokenEndpoint并提供定制的CheckTokenEndpoint吗?
我的应用程序有单独的授权服务器和资源服务器。授权服务器向资源服务器提供访问令牌。然后,资源服务器使用访问令牌发送受保护资源的请求 资源服务器使用我们可以在SpringOAuth2中重写CheckTokenEndpoint并提供定制的CheckTokenEndpoint吗?,spring,spring-security,spring-security-oauth2,spring-oauth2,Spring,Spring Security,Spring Security Oauth2,Spring Oauth2,我的应用程序有单独的授权服务器和资源服务器。授权服务器向资源服务器提供访问令牌。然后,资源服务器使用访问令牌发送受保护资源的请求 资源服务器使用RemoteTokenServices验证访问令牌是否正确 @Bean public RemoteTokenServices remoteTokenServices(final @Value("${auth.server.url}") String checkTokenUrl, final @Value("${auth.server.client
RemoteTokenServices
验证访问令牌是否正确
@Bean
public RemoteTokenServices remoteTokenServices(final @Value("${auth.server.url}") String checkTokenUrl,
final @Value("${auth.server.clientId}") String clientId,
final @Value("${auth.server.clientsecret}") String clientSecret)
{
final RemoteTokenServices remoteTokenServices = new RemoteTokenServices();
remoteTokenServices.setCheckTokenEndpointUrl(checkTokenUrl+"?name=value");
remoteTokenServices.setClientId(clientId);
remoteTokenServices.setClientSecret(clientSecret);
remoteTokenServices.setAccessTokenConverter(accessTokenConverter());
return remoteTokenServices;
}
application.yml
auth:
server:
url: http://localhost:9191/api/oauth/check_token/
clientId: clientid
clientsecret: secret
我想传递额外的参数,比如资源id,这样我就可以验证令牌是否被授权用于该资源
我想在org.springframework.security.oauth2.provider.endpoint.CheckTokenEndpoint
中获取该参数,
并希望覆盖下面的方法来添加一些逻辑。可能吗
@RequestMapping(value = "/oauth/check_token")
@ResponseBody
public Map<String, ?> checkToken(@RequestParam("token") String value) {
OAuth2AccessToken token = resourceServerTokenServices.readAccessToken(value);
if (token == null) {
throw new InvalidTokenException("Token was not recognised");
}
if (token.isExpired()) {
throw new InvalidTokenException("Token has expired");
}
OAuth2Authentication authentication = resourceServerTokenServices.loadAuthentication(token.getValue());
Map<String, ?> response = accessTokenConverter.convertAccessToken(token, authentication);
return response;
}
@RequestMapping(value=“/oauth/check_-token”)
@应答器
公共映射检查令牌(@RequestParam(“令牌”)字符串值){
OAuth2AccessToken token=resourceServerTokenServices.readAccessToken(值);
if(标记==null){
抛出新的InvalidTokenException(“未识别令牌”);
}
if(token.isExpired()){
抛出新的InvalidTokenException(“令牌已过期”);
}
OAuth2Authentication authentication=resourceServerTokenServices.loadAuthentication(token.getValue());
映射响应=accessTokenConverter.convertAccessToken(令牌,身份验证);
返回响应;
}
如何将一些参数发送到oauth/check_token并重写checkToken()
方法
基本上,我所做的是在生成访问令牌时,保存一些关于该令牌所允许的资源的记录
当我在资源服务器上收到对资源的请求时,我想将资源id传递给auth server,并想验证令牌是否被授权用于该资源?我刚刚创建了新的CustomCheckTokenEndpoint并复制了CheckTokenEndpoint的全部代码,然后覆盖checkToken(…)方法
@配置
@EnableAuthorizationServer
公共类CustomAuthorizationServer扩展AuthorizationServerConfigurerAdapter{
@自动连线
私人AuthenticationManager AuthenticationManager;
@凌驾
public void configure(AuthorizationServerEndpointsConfigurer端点)引发异常{
路径映射(“/oauth/check_-token”,“/my/oauth/check_-token”);
}
}
**CustomCheckTokenEndpoint.java**
公共类CustomCheckTokenEndpoint{
//复制整个CheckTokenEndpoint
@请求映射(value=“/my/oauth/check_token”)
@应答器
公共映射检查令牌(字符串值){
//您的代码将在这里
}
}
您所说的“资源id”是什么?您不能从令牌本身获取它吗?
@Configuration
@EnableAuthorizationServer
public class CustomAuthorizationServer extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.pathMapping("/oauth/check_token", "/my/oauth/check_token");
}
}
**CustomCheckTokenEndpoint.java**
public class CustomCheckTokenEndpoint {
// copy whole CheckTokenEndpoint
@RequestMapping(value = "/my/oauth/check_token")
@ResponseBody
public Map<String, ?> checkToken(String value) {
// your code will be here
}
}