【发布时间】:2019-09-12 11:09:24
【问题描述】:
有一个 Zuul 网关作为 Spring-Security-OAuth2 客户端和授权服务器。这些位于here
Zuul 配置部分:
http
.csrf()
.disable()
.headers().cacheControl().disable()
.and()
.headers()
.cacheControl()
.disable()
.frameOptions()
.sameOrigin()
.and()
.httpBasic().disable()
.authorizeRequests()
.requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
.mvcMatchers("/uaa/**", "/login**", "/favicon.ico", "/error**").permitAll()
.anyRequest().authenticated()
.and()
.logout()
.logoutSuccessUrl("/app/Index.jsp")
.logoutRequestMatcher(new AntPathRequestMatcher("/reza"))
.addLogoutHandler(ssoLogoutHandler);
Zuul 应用的 SsoLogoutHandler 类作为 Spring-Security-OAuth2 客户端:
@Component
public class SSOLogoutHandler implements LogoutHandler {
@Override
public void logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) {
Object details = authentication.getDetails();
String token = ((OAuth2AuthenticationDetails) details).getTokenValue();
RestTemplate restTemplate = new RestTemplate();
String url = "http://192.168.10.97:9191/uaa/token/revoke?token=" + token;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers);
ResponseEntity<Boolean> result = restTemplate.exchange(url, HttpMethod.GET, requestEntity, new ParameterizedTypeReference<Boolean>() {
});
}
}
和授权服务器的RevokeTokenController config 类:
@RestController
public class RevokeTokenController {
@Autowired
private TokenStore tokenStore;
@RequestMapping(method = RequestMethod.GET, value = "/token/revoke")
@ResponseBody
public Boolean revoke(String token) throws Exception {
OAuth2AccessToken tokenObj = tokenStore.readAccessToken(token);
tokenStore.removeAccessToken(tokenObj);
tokenStore.removeRefreshToken(tokenObj.getRefreshToken());
return true;
}
}
为了您看到的上述配置,客户端的SsoLogoutHandler调用restTemplate到Authorzation Server的RevokeTokenController注销,令牌和刷新令牌被删除但客户端再次请求为/uaa/authorize ... 获取新的访问令牌并注销不会发生。
哪里错了?我想在删除令牌和刷新令牌后注销,而不是再次获取访问令牌。另一方面,我想在删除令牌后重定向到登录页面。
更新:
我在去掉token后深入到客户端请求,客户端请求像.../uaa/authorize?client_id=...,所以它响应的location属性是.../gateway/login?code=[code],因为代码,客户端未重定向到登录页面。
【问题讨论】:
标签: spring-boot oauth-2.0 microservices spring-security-oauth2