核心代码
LogoutController.java
@RestController
public class LogoutController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@ApiOperation("管理员退出")
@PostMapping("sys/logout")
public ResponseEntity<Void> logout(HttpServletRequest request) {
//从请求头中获取token = bear jwt
String authorization = request.getHeader(GatewayConstant.AUTHORIZATION);
//获取jwt
String jwt = authorization.replaceFirst(GatewayConstant.BEARER, "");
//删除redis中指定的jwt
stringRedisTemplate.delete(GatewayConstant.TOKEN_PREFIX+jwt);
return ResponseEntity.ok().build();
}
}
其他
public interface GatewayConstant {
String TOKEN_PREFIX = "TOKEN:";
List<String> ALLOW_URLS = Arrays.asList("/oauth/token");
String AUTHORIZATION = "Authorization";
String BEARER = "bearer ";
}